2014-09-23 105 views
1

我有一個涉及兩個操作的SKAction序列。 1)根據變量等待時間2)產卵物體如何更改永久運行的SKAction

SKAction *sequence = [SKAction sequence:@[ 
             wait, 
             addObject]]; 

此操作設置爲永久運行。不過,我想等待時長根據更新變量發生變化,但保持不變的運行下去

[self runAction:[SKAction repeatActionForever:sequence]]; 

當我如何讓它穩步增加值,因此增加了它不會採取新的變量對象產卵率?

感謝

回答

3

解決辦法有兩個:

  1. 創建每次你需要它的時候序列或至少等待重新行動。行動應該被拋棄並經常重複使用。
  2. 如果這會造成性能問題,並且您已經有序列參考,您也可以更改其速度變量。這應該相應地改變等待時間,即如果速度爲0.5,則等待時間應該加倍。
  3. 用於解決方案1 ​​

實施例:

CGFloat waitDuration = (value to be determined by you); 
SKAction *sequence = [SKAction sequence:@[ 
             [SKAction waitForDuration:waitDuration], 
             addObject]]; 

示例溶液2:

SKAction *sequence = [SKAction sequence:@[ 
             wait, 
             addObject]]; 

// half the speed, double the wait time 
sequence.speed = 0.5; 

// or if you need to derive speed from waitDuration (which must be >0.0) 
sequence.speed = (1.0/waitDuration); 

萬一序列不會受到速度嘗試設置wait動作的速度來代替。

0

所以我遵循了LearnCocos2D的回答,但我做了一個小小的修改,我認爲這很好。它適用於我的目的,所以我想把它放在那裏。

我所做的是這樣的:

//This is the boring bits for creating the SKSpriteNode 
    headsNode = [SKSpriteNode spriteNodeWithTexture:[self.textureAtlas firstObject]size:CGSizeMake(100, 100)]; 
    headsNode.position = CGPointMake(CGRectGetMidX(self.frame)*0.5, CGRectGetMidY(self.frame)-coinSize/2); 

    headsNode.name = @"Heads"; 
    [self addChild:headsNode]; 

// I added a timer with winkWaitingTime as interval. This is the variable 
// to change. Set this to something at the beginning. 
NSTimer *timered= [NSTimer scheduledTimerWithTimeInterval:winkWaitingTime target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 
    [timered fire]; 
} 

-(void)timerFired:(NSTimer*)timer{ 
//In the NSTimer Selector, I set the timer interval/SKAction waiting interval to a random 
// number 
    winkWaitingTime =[[SharedInfo sharedManager] randomFloatBetween:3 and:8]; 
    [headsNode removeAllActions]; 
//I thought it's best to remove the actions JIC :) 
    [headsNode runAction:[self returnActionForAnimationKey:headsNode.name]]; 
    NSLog(@"winktimer: %f",winkWaitingTime); 
} 
-(SKAction*)returnActionForAnimationKey:(NSString*)animationKey{ 
    CGFloat waitDuration; 
//This is for just to prevent timer fire interfering with the SKAction 
//This will make the SKAction waiting time shorter than NSTimer Fire rate 
    if (winkWaitingTime >4) { 
     waitDuration = winkWaitingTime-3; 
    }else{ 
     waitDuration = 1; 
    } 

    SKAction *sequence = [SKAction sequence:@[ 
               [SKAction waitForDuration:waitDuration], 
               [SKAction animateWithTextures:self.textureAtlas timePerFrame:0.1f resize:NO restore:YES]]]; 

    return sequence; 
} 

正如我所說的,我的作品,並通過改變winkWaitingTime,有點防範管理,它的工作原理。

我希望你覺得它也很有用。在內存管理方面,我的Xcode顯示CPU /內存/電池使用量穩定,所以沒有增加使用。

備註:收到評論後,我認爲最好討論使用NSTimer。如果你需要做一些「外部」操作,即獨立於視圖或場景,那麼我會使用NSTimer(這是我的情況)。但是你必須爲計時器實現暫停和取消暫停。 (您必須使NSTimer無效才能停止,並重新安排新的時間,或將其作爲變量並重新計劃同一時間)。否則,請使用SKAction,您不需要暫停和取消暫停NSTimer

+0

你爲什麼在這裏使用NSTimer?您可以始終使用與NSTimer使用SKAction相同的功能。區別在於NSTimer不受場景或視圖的影響,甚至不受節點的暫停狀態的影響,所以如果您沒有爲NSTimer實現自定義暫停功能,則可能會遇到問題。使用SKA自動停止/取消暫停。 – Whirlwind 2016-03-19 13:44:00

+0

@Whirlwind是的,但對於我的項目,我想有一個「外部」定時器,做一些其他的事情(我沒有在這裏添加它,因爲它是無關緊要的),並創建了一個暫停/非暫停方法好吧,更像是驗證/無效)。我編輯了這個問題來反映這一點。 – Septronic 2016-03-19 13:57:39

+0

啊,好吧,我只是覺得你不知道NSTimer在SpriteKit項目中的行爲。 – Whirlwind 2016-03-19 15:14:44