Ios 如何在移动角色时停止动画重叠

Ios 如何在移动角色时停止动画重叠,ios,iphone,swift,sprite-kit,sprite,Ios,Iphone,Swift,Sprite Kit,Sprite,我的角色有两种不同的纹理重叠/显示太快 移动角色时。如何设置动画的持续时间,以便在移动角色时纹理始终以相同的速度切换 这是我的代码: override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { for touch: AnyObject in touches { let location = touch.locationInNode(self) let animat

我的角色有两种不同的纹理重叠/显示太快 移动角色时。如何设置动画的持续时间,以便在移动角色时纹理始终以相同的速度切换

这是我的代码:

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        let animatePlayerStart = SKAction.setTexture(SKTexture(imageNamed: "Player\(i).png"))


        // Determine speed for character movement
        var minDuration:CGFloat = 0.7;
        var maxDuration:CGFloat = 1.8;
        var rangeDuration:CGFloat = maxDuration - minDuration;
        var actualDuration:NSTimeInterval = NSTimeInterval((CGFloat(arc4random())%rangeDuration) + minDuration)


        let move = SKAction.moveTo(location, duration:actualDuration)

        player.runAction(SKAction.sequence([animatePlayerStart, move]))


        // i determines which texture is going to be displayed
        if(self.i == 2) {
            self.i = 1

        }
        else{
            self.i++
        }


    }
}

您正在TouchsMoved中更改纹理,这被称为“快速”,因此您当前获得的效果。要在预定义的时间段后更改纹理,可以使用以下方法:

import SpriteKit

class GameScene: SKScene {


    let hero = SKSpriteNode(imageNamed: "heroState_A")
    let textureA = SKTexture(imageNamed: "heroState_A")
    let textureB = SKTexture(imageNamed: "heroState_B")


    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        //Because hero is already initialized with textureA, start from textureB

        let animation = SKAction.animateWithTextures([textureB,textureA], timePerFrame:0.5)

        hero.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))

        addChild(hero)

        //Start animation
        hero.runAction(SKAction.repeatActionForever(animation),withKey:"heroAnimation")


        //Removing heroAnimation

        //You can stop this animation by hero.removeAllActions, but if you run animation with key, you can remove just that particular action, which gives you more control

        let stop = SKAction.runBlock({

            if(self.hero.actionForKey("heroAnimation") != nil){

                self.hero.removeActionForKey("heroAnimation")
            }
        })

        //Just an example, in real app you will do this at certain events (eg. when player stops his movement)
        runAction(SKAction.sequence([SKAction.waitForDuration(5),stop]))


    }
}