简体   繁体   English

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

[英]How do I use buttons or a slider to rotate a sprite?

I am making a game for iOS in Swift with SpriteKit, the game is currently just a ball moving around with a sword. 我正在使用SpriteKit在Swift中为iOS制作游戏,该游戏目前只是一个用剑四处移动的球。

I have anchored the sword to the bottom of the sword, but I need to know how to control the direction of rotation with 2 buttons or a slider of some sort. 我已经将剑固定在剑的底部,但是我需要知道如何使用2个按钮或某种滑块来控制旋转方向。

Here is my code: 这是我的代码:

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 */
}
}

Okay, I think I understand what you are trying to do. 好吧,我想我知道您要做什么。 It involves a lot of code, but in theory it is pretty simple. 它涉及很多代码,但从理论上讲非常简单。 We detect if the touch is inside the left or right buttons (which in this example, I've made them SKSpriteNode s), and then set boolean values for the update method to use and rotate the weapon node (I'm assuming this is the sword you were talking about). 我们检测触摸是否在左右按钮内(在此示例中,我将它们设置为SKSpriteNode ),然后为update方法设置boolean值以使用和旋转weapon节点(我假设这是您正在谈论的剑)。

I've also included a variable that lets you set the speed of rotation. 我还包括一个变量,可让您设置旋转速度。 As you did not say what speed you were looking for, I left it up to you. 正如您没有说出您想要的速度一样,我让您自己决定。

At the top of the program: 在程序顶部:

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

And then modify touchesBegan to look like this: 然后修改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)
        }
    }

In update , add this code: 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
    }
}

We want to stop the weapon from rotating when we detect when the finger has moved off the button like so: 当我们检测到手指何时从按钮移开时,我们想停止武器旋转,如下所示:

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
    }
}

And at last, we need to detect when your finger has left the screen: 最后,我们需要检测您的手指何时离开屏幕:

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
    }
}

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

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