首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit - touchesMoved,aim十字

SpriteKit - touchesMoved,aim十字
EN

Stack Overflow用户
提问于 2015-03-01 11:09:46
回答 1查看 918关注 0票数 0

我有一个spritenode和一个交叉节点,我希望当玩家触摸spritenode和移动交叉节点时也会移动。

代码语言:javascript
复制
  override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

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


            var body = self.nodeAtPoint(location)

            if var name: String = body.name {

                if body.name == "aim-button" {

                    crossHair.position = CGPointMake(crossHair.position.x + 10, crossHair.position.y + 10)
                }

            }

        }
    }

横发确实可以移动,但只能朝一个方向移动,而且我不知道如何根据触觉移动到的空间(这显然比交叉头发在屏幕上实际移动的位置要小得多)和方向来精确地移动。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-01 11:47:40

代码语言:javascript
复制
    for touch: AnyObject in touches {
        //Get the current position in scene of the touch.
        let location = touch.locationInNode(self)
        //Get the previous position in scene of the touch.
        let previousLocation = touch.previousLocationInNode(self)
        //Calculate the translation.
        let translation = CGPointMake(location.x - previousLocation.x, location.y - previousLocation.y)
        //Get the current position in scene of the crossHair.
        let position = crossHair.position
        // Get the bode touched
        var body = self.nodeAtPoint(location)

        if var name: String = body.name {

            if body.name == "aim-button" {

                //Set the position of the crosshair to its current position plus the translation.
                crossHair.position =  CGPointMake(position.x + translation.x * 2, position.y + translation.y * 2)
                //Set the position of the body
                body.position = location
            }
        }
    }    

如果交叉的距离应该比触摸的距离更大,只需乘以一个因子(我在上面的例子中使用的是2)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28792842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档