2015-02-08 84 views
0

我试图让一个对象持续执行一个动作,直到它被触摸,但是当被触摸时,对象只执行一次动作。
这就是我有这么远不断移动一个对象(spritenode),直到触及屏幕

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

    for touches: AnyObject in touches { 

     plane.physicsBody?.velocity = CGVectorMake(0, 0) 
     plane.physicsBody?.applyImpulse(CGVectorMake(0, 500)) 
    } 
} 

任何sugessiton如何得到它的工作。

回答

0

如果您想移动物体,最好使用SKAction.moveBy而不是冲动。目前您只有在用户触摸屏幕时才执行该操作。如果你想运行,直到用户触摸屏的动作,你必须把它放在didMoveToView方法和使用的关键,所以你可以从雪碧中删除的操作:

class YourClass: SKScene{ 
    var sprite = SKSpriteNode() 

    override func didMoveToView(view: SKView) { 

     //4 seconds for 500y 
     var neededTime:NSTimeInterval = 4 
     var action = SKAction.moveBy(CGVectorMake(0, 500), duration: neededTime) 
     var repeatAction = SKAction.repeatActionForever(action) 



     //Repeat the action forever and with a key so you can remove it on touch: 
     sprite.runAction(repeatAction, withKey: "aKey") 
    } 

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
     //remove the action 
     sprite.removeActionForKey("aKey") 
    } 
} 
相关问题