简体   繁体   English

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

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

I've searched and I can only find things about making it move by swipping/dragging. 我已经搜索过,但只能通过滑动/拖动来找到有关使其移动的信息。 I'm a beginner making a game on Xcode with Sprite-Kit , and I'm doing pretty well so far, better than expected to be honest. 我是一个使用Sprite-KitXcode上制作游戏的初学者,到目前为止,我的表现还不错,比预期的要好。

But I cant seem to figure out how to make a sprite move left and right when hitting left and right buttons. 但是我似乎无法弄清楚如何在单击左右按钮时使精灵左右移动。 I made the left SKSpriteNode and right SKSpriteNode (I don't think that's correct) and they show up where I want obviously, but I don't know how to enable them to be touched and make the sprite move. 我制作了左SKSpriteNode和右SKSpriteNode (我认为这是不正确的),它们显然显示在我想要的位置,但是我不知道如何使它们受到触摸并使精灵移动。

Sorry if this is a really amateur question, the answer will probably be obvious but I'm in a jam. 抱歉,如果这是一个真正的业余问题,答案可能很明显,但我陷入了困境。

There are a lot of tutorials about moving sprites, begin with this raywenderlich tutorial. raywenderlich教程开始,有很多关于移动精灵的教程。 You have to set name for your sprites and handle the movement in touchesBegan function (use this thread ) 您必须为精灵设置名称并在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