如何左/右创建移动按钮?

时间:2016-01-26 22:18:10

标签: swift sprite-kit

请帮忙。我是Swift的初学者。我为玩家和左/右按钮添加了精灵。但是,如何为每个按钮添加操作并将这些操作应用到播放器?

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var player: SKSpriteNode!
    var leftMove: SKSpriteNode!
    var rightMove: SKSpriteNode!

    override func didMoveToView(view: SKView) {

        player = SKSpriteNode (imageNamed: "player")

        player.position = CGPoint(x:127, y:125)

        addChild(player)


        leftMove = SKSpriteNode (imageNamed: "leftMove")

        leftMove.position = CGPoint(x:80, y:35)

        leftMove.size = CGSize (width: 55, height: 55)

        addChild(leftMove)


        rightMove = SKSpriteNode (imageNamed: "rightMove")

        rightMove.position = CGPoint(x:160, y:35)

        rightMove.size = CGSize (width: 55, height: 55)

        addChild(rightMove)

        physicsWorld.contactDelegate = self


    }

2 个答案:

答案 0 :(得分:3)

为按钮指定一个唯一的名称,如:

leftMove.name = "Left"
rightMove.name = "Right"

在touchesBegan中实现逻辑:

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

  /* Called when a touch begins */
  for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
    let node = self.nodeAtPoint(location)

    if (node.name == "Left") {
        // Implement your logic for left button touch here:
        player.position = CGPoint(x:player.position.x-1, y:player.position.y)
    } else if (node.name == "Right") {
        // Implement your logic for right button touch here:
        player.position = CGPoint(x:player.position.x+1, y:player.position.y)
    }
  }
}

答案 1 :(得分:2)

欢迎使用StackOverflow。我的建议是,对于您希望对其执行操作的每个按钮,您将SKSpriteNode子类化为这些对象。这样,您可以单独利用每个对象的touchesBegan:,touchesMoved:和touchesEnded:函数。例如:

正如所承诺的,这是一个更强大的子类SKSpriteNode版本。另外,我在我的一个项目中测试了它,我可以证明它正常工作:

import Foundation
import SpriteKit

class SubclassedSKSpriteNode: SKSpriteNode {
    init() {
        let texture = SKTexture(imageNamed: "whateverImage.png")
        super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
        userInteractionEnabled = true
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let location = (touches.first! ).locationInNode(scene!)
        position = scene!.convertPoint(location, toNode: parent!)

        print("touchesBegan: \(location)")
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let location = (touches.first! ).locationInNode(scene!)
        position = scene!.convertPoint(location, toNode: parent!)

        print("touchesMoved: \(location)")

    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let location = (touches.first! ).locationInNode(scene!)
        position = scene!.convertPoint(location, toNode: parent!)

        print("touchesEnded: \(location)")
    }
}

此外,要将此按钮添加到SKScene(或任何其他SKNode):

let button = SubclassedSKSpriteNode()
button.position = CGPointMake(420, 300)
addChild(button)

HTH!