2015-08-30 36 views
1

我正在爲iOS遊戲在斯威夫特與SpriteKit,遊戲目前只是一個球用劍走動。如何使用按鈕或滑塊來旋轉精靈?

我已經將劍固定在劍的底部,但我需要知道如何用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

查找到'weapon.zRotation'設置旋轉(注意,這是在Radians中)。然後,您可以添加兩個(讓我們只想說,我不知道你用的按鈕找什麼)'SKShapeNode's,並在'touchesBegan'檢測是否觸摸是'SKShapeNode'裏面用'containsPoint:'。 – Gliderman

+0

我想這樣做兩個按鈕將並排旋轉武器順時針和逆時針 –

回答

0

好吧,我想我明白你正在嘗試做的。它涉及很多代碼,但理論上它非常簡單。我們檢測是否觸摸位於左側或右側按鈕(在本例中,我已將它們製作成SKSpriteNode秒),然後將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 
    } 
} 

我們希望當我們檢測到手指已經移動時,阻止武器旋轉F上的按鈕,像這樣:

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

當我添加到場景的按鈕,他們在同一個地方,當我試圖定位他們,他們沒有出現屏幕上。 –

+0

你在哪裏放置按鈕?默認情況下,'SpriteKit'使用AspectFit,這將取決於所使用的設備('SpriteKit'使用4:3的比例)切斷顯示屏的頂部和底部。如果你把按鈕放在這個切斷的區域,那麼你將無法看到它們。您可以將它們放在更高的位置,或者將顯示模式更改爲填充而不是放入視圖控制器中。 – Gliderman