如何使用按钮或滑块旋转精灵?

时间:2015-08-30 08:27:13

标签: ios swift sprite-kit

我正在使用SpriteKit在Swift中为iOS制作一款游戏,该游戏目前只是一个用剑移动的球。

我已将剑固定在剑的底部,但我需要知道如何使用2个按钮或某种滑块来控制旋转方向。

这是我的代码:

import SpriteKit

let sprite = SKSpriteNode(imageNamed:"Player")
let weapon = SKSpriteNode(imageNamed: "weapon")

class GameScene: SKScene {
  override func didMoveToView(view: SKView) {
    let initialPlayerLocation = CGPoint(x: self.frame.width/2, y:       self.frame.height/2)
    /* Setup your scene here */

    //player
    sprite.setScale(1.0)
    sprite.position = initialPlayerLocation
    sprite.zPosition = 20
    self.addChild(sprite)

    //weapon
    weapon.setScale(1.0)
    weapon.position = initialPlayerLocation
    weapon.zPosition = -20
    weapon.anchorPoint = CGPointMake(0.5,0.0);

    self.addChild(weapon)


}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        var move = SKAction.moveTo(location, duration:1.0)
        sprite.runAction(move)
        weapon.runAction(move)

    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}

1 个答案:

答案 0 :(得分:0)

好的,我想我明白你要做什么。它涉及很多代码,但理论上它非常简单。我们检测触摸是在左侧还是右侧按钮内(在此示例中,我已将它们设为SKSpriteNode s),然后为boolean方法设置update值使用和旋转weapon节点(我假设这是你所说的剑)。

我还包含一个变量,可让您设置旋转速度。由于你没有说出你想要的速度,我把它留给了你。

在程序的顶部:

let sprite = SKSpriteNode(imageNamed:"Player")
let weapon = SKSpriteNode(imageNamed: "weapon")
let leftButton = SKSpriteNode(imageNamed: "leftButton")
let rightButton = SKSpriteNode(imageNamed: "rightButton")
var leftPressed = false
var rightPressed = false
let weaponRotateSpeed = 0.01 // Change this for rotation speed of weapon

然后将touchesBegan修改为如下所示:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        if (leftButton.containsPoint(p: location))
            leftPressed = true
        else if (rightButton.containsPoint(p: location))
            rightPressed = true
        else {
            var move = SKAction.moveTo(location, duration:1.0)
            sprite.runAction(move)
            weapon.runAction(move)
        }
    }

update中,添加以下代码:

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if (leftPressed && rightPressed) {
        // Do nothing, as both buttons are pressed
    } else if (leftPressed) {
        weapon.zRotation -= weaponRotateSpeed
    } else if (rightPressed) {
        weapon.zRotation += weaponRotateSpeed
    }
}

当我们检测到手指何时从按钮上移开时,我们想要阻止武器旋转:

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        if (leftButton.containsPoint(p: location))
            leftPressed = true
        else
            leftPressed = false

        if (rightButton.containsPoint(p: location))
            rightPressed = true
        else
            rightPressed = false
    }
}

最后,我们需要检测手指何时离开屏幕:

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if (leftButton.containsPoint(p: location))
            leftPressed = false

        if (rightButton.containsPoint(p: location))
            rightPressed = false
    }
}