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

Ios 如何使用按钮或滑块旋转精灵?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在用SpriteKit为iOS制作一个Swift游戏,这个游戏目前只是一个用剑移动的球 我已经将剑固定在剑的底部,但我需要知道如何用2个按钮或某种滑块控制旋转方向 这是我的密码: import SpriteKit let sprite = SKSpriteNode(imageNamed:"Player") let weapon = SKSpriteNode(imageNamed: "weapon") class GameScene: SKScene { override func d

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

我已经将剑固定在剑的底部,但我需要知道如何用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 */
}
}
导入SpriteKit
让sprite=SKSpriteNode(图像名为:“玩家”)
让武器=SKSpriteNode(图像名为:“武器”)
类游戏场景:SKScene{
覆盖func didMoveToView(视图:SKView){
让initialPlayerLocation=CGPoint(x:self.frame.width/2,y:self.frame.height/2)
/*在这里设置场景*/
//玩家
sprite.setScale(1.0)
sprite.position=初始播放位置
sprite.zPosition=20
self.addChild(精灵)
//武器
武器设置刻度(1.0)
武器位置=初始玩家位置
武器位置=-20
anchorPoint=CGPointMake(0.5,0.0);
self.addChild(武器)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
/*当触摸开始时调用*/
用于触摸输入(触摸设置为){
let location=touch.locationInNode(自)
var move=SKAction.moveTo(位置、持续时间:1.0)
精灵运行动作(移动)
武器。动作(移动)
}
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}

好的,我想我明白你想做什么。它涉及很多代码,但理论上相当简单。我们检测触摸是否在左侧或右侧按钮内(在本例中,我将其设置为
SKSpriteNode
s),然后为
update
方法设置
boolean
值,以使用和旋转
武器
节点(我假设这就是您所说的剑)

我还包含了一个变量,可以设置旋转速度。因为你没有说你想要什么速度,我把它留给你了

在程序的顶部:

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
然后修改
触摸开始
,如下所示:

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)
        }
    }
当我们检测到手指离开按钮时,我们想阻止武器旋转,如下所示:

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
    }
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
用于触摸输入(触摸设置为){
let location=touch.locationInNode(自)
if(leftButton.containsPoint(p:位置))
leftPressed=true
其他的
leftPressed=false
if(右按钮。包含点(p:位置))
rightspressed=true
其他的
rightspressed=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
    }
}
override func touchesend(touchs:Set,withEvent-event:UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
if(leftButton.containsPoint(p:位置))
leftPressed=false
if(右按钮。包含点(p:位置))
rightspressed=false
}
}

好的,我想我明白你想做什么。它涉及很多代码,但理论上相当简单。我们检测触摸是否在左侧或右侧按钮内(在本例中,我将其设置为
SKSpriteNode
s),然后为
update
方法设置
boolean
值,以使用和旋转
武器
节点(我假设这就是您所说的剑)

我还包含了一个变量,可以设置旋转速度。因为你没有说你想要什么速度,我把它留给你了

在程序的顶部:

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
然后修改
触摸开始
,如下所示:

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)
        }
    }
当我们检测到手指离开按钮时,我们想阻止武器旋转,如下所示:

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
    }
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
用于触摸输入(触摸设置为){
let location=touch.locationInNode(自)
if(leftButton.containsPoint(p:位置))
leftPressed=true
其他的
leftPressed=false
if(右按钮。包含点(p:位置))
rightspressed=true
其他的
rightspressed=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
    }
}
override func touchesend(touchs:Set,withEvent-event:UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
if(leftButton.containsPoint(p:位置))
leftPressed=false
if(右按钮。包含点(p:位置))
rightspressed=false
}
}

查看
武器.zRotation
以设置旋转(注意这是弧度)。然后您可以添加两个(比如说,我不知道您在用按钮寻找什么)
SKShapeNode
s,在
触摸开始时
检测触摸是否在
SKShapeNode
内,带有
containsPoint:
。我试图使两个按钮并排顺时针和逆时针旋转武器。zRotation设置旋转(注意这是弧度)。然后您可以添加两个(比如说,我不知道您在用按钮寻找什么)
SKShapeNode
s,在
触摸开始
检测触摸是否在
SKShapeNode
中,使用
containsPoint:
。我试图使并列的两个按钮顺时针和逆时针旋转武器,当我将按钮添加到场景中时,它们位于同一位置,当我尝试定位它们时,它们不在同一位置显示在屏幕上。你把按钮放在哪里?默认情况下,
SpriteKit
使用AspectFit,这将根据所使用的设备切断显示器的顶部和底部(
SpriteKit
使用4:3的比例)。如果你把