SpriteKit - 鼠标捕捉到Sprite Drag上的中心 [英] SpriteKit - Mouse Snaps to Center On Sprite Drag

查看:116
本文介绍了SpriteKit - 鼠标捕捉到Sprite Drag上的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个iOS游戏,并且我已经拥有它,以便在触摸和移动项目时将其拖动。拖动时,鼠标会捕捉到精灵的中心。我怎么能这样做才不会发生这种情况?

I'm trying to create an iOS game and I have it so that when the item is touched and moved it is dragged. When it is dragged the mouse is snapped to the center of the sprite. How could I make it so that this wouldn't happen?

这里是一个正在发生的事情的例子。

Here is an example of what is happening.

以下是处理触摸输入的函数

Here are the functions dealing with touch input

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

    var papers = 0

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        let touchedNode = nodeAtPoint(location)

        if ((touchedNode.name?.contains("paper")) != nil) {

            touchedNode.position = location
            touchedNode.position.x = CGRectGetMidX(self.frame)
        }
    }
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let touchedNode = nodeAtPoint(location)

            if ((touchedNode.name?.contains("paper")) != nil) {

                touchedNode.position = location
                touchedNode.position.x = CGRectGetMidX(self.frame)
            }
        }
    }

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let touchedNode = nodeAtPoint(location)
        if ((touchedNode.name?.contains("paper")) != nil) {
            touchedNode.zPosition = 0
            touchedNode.position.x = CGRectGetMidX(self.frame)
        }
    }
}

PS contains 是String类的扩展,用于检查字符串是否在字符串中

P.S. contains is an extension off of the String class to check if a substring is in a string

提前致谢!

推荐答案

从精灵中心而不是从精灵中心拖动精灵是相当简单的。为此,计算并存储触摸位置和子画面中心之间的差异(即偏移)。然后,在 touchesMoved 中,将精灵的新位置设置为触摸位置加上偏移量。

It's fairly straightforward to drag a sprite from the touch location instead of from the center of the sprite. To do this, calculate and store the difference (i.e., offset) between the touch location and the center of the sprite. Then, in touchesMoved, set the sprite's new position to the touch location plus the offset.

您可以选择重载 + - 运算符,以简化添加和减去 CGPoint 秒。在GameScene类之外定义:

You can optionally overload the + and - operators to simplify adding and subtracting CGPoints. Define this outside of the GameScene class:

func - (left:CGPoint,right:CGPoint) -> CGPoint {
    return CGPoint(x: right.x-left.x, y: right.y-left.y)
}

func + (left:CGPoint,right:CGPoint) -> CGPoint {
    return CGPoint(x: right.x+left.x, y: right.y+left.y)
}

在GameScene中,定义以下实例变量

In GameScene, define the following instance variable

var offset:CGPoint?

然后在 touchesBegan 中,替换

touchedNode.position = location

with

offset = location - touchedNode.position

touchesMoved ,替换

touchedNode.position = location

with

if let offset = self.offset {
    touchedNode.position = location + offset
}

我推广了解决方案以抵消精灵在 x 和<$中的位置c $ c> y 尺寸。在您的应用中,您可以简单地抵消精灵的 y 位置,因为忽略 x

I generalized the solution to offset the sprite's position in both the x and y dimensions. In your app, you can simply offset the sprite's y position since x is ignored.

这篇关于SpriteKit - 鼠标捕捉到Sprite Drag上的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆