繁体   English   中英

需要左右按钮在x轴上移动精灵

[英]Need a right and left buttons to move a sprite on the x axis

我已经搜索过,但只能通过滑动/拖动来找到有关使其移动的信息。 我是一个使用Sprite-KitXcode上制作游戏的初学者,到目前为止,我的表现还不错,比预期的要好。

但是我似乎无法弄清楚如何在单击左右按钮时使精灵左右移动。 我制作了左SKSpriteNode和右SKSpriteNode (我认为这是不正确的),它们显然显示在我想要的位置,但是我不知道如何使它们受到触摸并使精灵移动。

抱歉,如果这是一个真正的业余问题,答案可能很明显,但我陷入了困境。

raywenderlich教程开始,有很多关于移动精灵的教程。 您必须为精灵设置名称并在touchesBegan函数中处理移动(使用此线程

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches) {
        let positionInScene = touch.location(in: self)
        let touchedNode = self.atPoint(positionInScene)
        if let name = touchedNode.name {
            if name == "leftButton" {
                //move to left
            }
            if name == "rightButton" {
                //move to right
               let moveAction: SKAction = SKAction.moveBy(x: 15, y: 0, duration: 1)
               yourNode.run(moveAction)
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM