【问题标题】:move an object (spritenode) in swift continously until touched on the screen连续快速移动对象(精灵节点)直到触摸屏幕
【发布时间】:2015-02-08 11:33:03
【问题描述】:
我试图让一个对象连续执行一个动作,直到它被触摸,但是当被触摸时,对象只执行一次动作。
这就是我到目前为止所要做的
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))
}
}
任何建议如何让它工作。
【问题讨论】:
标签:
ios
swift
touchesbegan
touches
【解决方案1】:
如果你想移动一个物体,你最好使用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")
}
}