简体   繁体   中英

spritekit SKAction.resizeToHeight is not repeating

I want to increase the height of in touch events, it's same like stick hero is doing. I am trying following code

      func changeHeight(){


    let action = SKAction.resizeToHeight(self.leadherNode!.size.height+50, duration: 0.5);
    let seq = SKAction.repeatActionForever(action)
    self.leadherNode?.runAction(seq, withKey: "height")


}

but unfortunately it just increase the height of node for first time and it never repeats. How can I achieve this?

I dont know swift. But I can write objective-c version.

CGFloat currentSpriteSize;//Create private float
SKSpriteNode *sprite;//Create private sprite

currentSpriteSize = sprite.size.height;//Into your start method

//And your Action
SKAction *seq = [SKAction sequence:@[[SKAction runBlock:^{
        currentSpriteSize += 50;
    }], [SKAction resizeToHeight:currentSpriteSize duration:0.5]]];
    [sprite runAction:[SKAction repeatActionForever:seq]];

Changes to an argument of an SKAction after it has started will have no effect on the action. You will need to create a new action with the updated value at each step. Here's one way to do that:

Define and initialize the height and max height properties

var spriteHeight:CGFloat = 50.0;
let maxHeight:CGFloat = 500.0

Call this from didMoveToView

resizeToHeight()

This function creates an SKAction that resizes a sprite to a specific height. After the completion of the action, the function updates the height value and then calls itself.

func resizeToHeight() {
   self.leadherNode?.runAction(
        SKAction.resizeToHeight(self.spriteHeight, duration: 0.5),
        completion:{
            // Run only after the previous action has completed
            self.spriteHeight += 50.0
            if (self.spriteHeight <= self.maxHeight) {
                self.resizeToHeight()
            }
        }
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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