简体   繁体   English

如何更改永远运行的SKAction

[英]How to change SKAction which is running forever

i have an SKAction sequence involving two actions. 我有一个涉及两个动作的SKAction序列。 1) wait for time according to variable 2) spawn object 1)根据变量2)生成对象等待时间

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

this action is set to run forever. 此操作设置为永久运行。 however, i want the wait duration to change according to updating the variable, but it stays constant as it does not take the new variable when running forever 但是,我希望等待持续时间根据更新变量而改变,但它保持不变,因为它在永久运行时不会采用新变量

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

How do i make it steadily increase the value, therefore increasing the rate of object spawning? 如何使其稳定地增加值,从而提高对象产生的速度?

Thanks 谢谢

There are two solutions: 有两种解决方案:

  1. Create the sequence or at least the wait action anew every time you need it. 每次需要时,重新创建序列或至少等待等待动作。 Actions are supposed to be discarded and re-used frequently. 应该丢弃操作并经常重复使用。
  2. If this poses a performance problem and given that you already have a reference to the sequence, you can also change its speed variable. 如果这会导致性能问题并且您已经有序列的引用,那么您也可以更改其速度变量。 This ought to alter the wait time accordingly, ie if speed is 0.5 the wait time should double. 这应该相应地改变等待时间,即如果速度为0.5,则等待时间应该加倍。

Example for solution 1: 解决方案1的示例:

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

Example for solution 2: 解决方案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);

In case the sequence isn't affected by speed try setting the wait action's speed instead. 如果序列不受速度影响,请尝试设置wait动作的速度。

So I followed LearnCocos2D's answer, but I made a slight modification, which I think works great. 所以我按照LearnCocos2D的回答,但我做了一些修改,我认为这很好。 It works for my purpose, so I thought to put it out there. 它适用于我的目的,所以我想把它放在那里。

What I did was this: 我做的是这样的:

//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;
}

As I said, it works for me, and by changing the winkWaitingTime, and a bit of prevention management, it works. 正如我所说,它适用于我,通过改变winkWaitingTime和一些预防管理,它的工作原理。

I hope you find it useful as well. 我希望你发现它也很有用。 In terms of memory management, my Xcode is showing stable CPU / Memory / Battery usage, so there's no increase in use. 在内存管理方面,我的Xcode显示出稳定的CPU /内存/电池使用率,因此使用率没有增加。

NOTE : After receiving a comment, I thought it's best to discuss the use of NSTimer . 注意 :收到评论后,我认为最好讨论NSTimer的使用。 If you need to have something done "externally", ie independent to the view or scene, then I'd use the NSTimer (that was my case). 如果您需要“外部”完成某些操作,即独立于视图或场景,那么我将使用NSTimer (这是我的情况)。 But you'd have to implement the pausing and unpausing for the timer as well. 但是你必须为计时器实现暂停和取消暂停。 (You'd have to invalidate the NSTimer to stop, and reschedule a new one, or have it as a variable and reschedule the same one). (您必须使NSTimer无效以停止,并重新安排一个新的,或将其作为变量并重新安排同一个)。 Otherwise, use the SKAction and you won't need to pause and unpause the NSTimer . 否则,请使用SKAction ,您无需暂停和取消暂停NSTimer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM