如何移动特定的精灵

时间:2015-07-23 06:25:37

标签: ios swift

在我的游戏中我有四个精灵,用户应该单独触摸每一个,这是我的代码

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

     for touch: AnyObject in touches{

     let location = touch.locationInNode(self)

     let newposition2 = CGPointMake(location.x, 15)

            self.brick2.position=newposition2

}

效果很好,但是在我添加第一个精灵后,我可以从屏幕中的任何位置控制它,我想当我触摸第一个精灵时它自己应该移动,否则,它仍然在它的位置

我希望我的话很清楚......

1 个答案:

答案 0 :(得分:0)

你可以在你的方法中这样做:

for touch in (touches as! Set<UITouch>) {

        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == yourSprite {
            //This will call when you touch sprite
            //change your sprite location

        }
}
希望这会有所帮助。