SpriteKit手機遊戲搖杆JoyStick的使用 -- by iFIERO遊戲開發教程

來源:互聯網
上載者:User

標籤:bat   手機   else   add   angular   stop   handle   spritekit   begin   

### 工欲善其事,必先利其器

有時候學習如何應用第三方庫是非常重要的,因為我們不用再自己重複造輪子,在這裡,我們就把原先利用重力感應來操控飛機改為用遊戲搖杆joystick來操控,具體的操作如下:
```
// 聲明遊戲搖杆;
private var joystick:AnalogJoystick! // 遊戲搖杆;
```
> 建立函數,建立遊戲搖杆節點,並添加到GameScene中
```
//MARK:-// 加入遊戲手柄
func createJoyStick(){
joystick = AnalogJoystick(diameter: CGFloat(250.0)) // 搖杆直徑
joystick.stick.image = UIImage(named: "jStick") // 搖杆圖
joystick.substrate.image = UIImage(named: "jSubstrate") // 搖杆背景圖
joystick.zPosition = 1
joystick.position = CGPoint(x: -380.0, y: -720.0)
self.addChild(joystick)
joystick.alpha = 0.8
// 操控剛開始
joystick.beginHandler = { [unowned self] in
guard let sprite = self.playerNode else {
return
}
print("begin",sprite);
}
// 追蹤操控
joystick.trackingHandler = {[unowned self] data in
guard let sprite = self.playerNode else {
return
}
let x = data.velocity.x * 0.12
let y = data.velocity.y * 0.12
let xPos = sprite.position.x + x
let yPos = sprite.position.y + y
sprite.position = CGPoint(x: xPos, y: yPos) // 更新飛機位置
//sprite.zRotation = data.angular // 飛機角度轉向;
}
// 停止搖杆時;
joystick.stopHandler = { [unowned self] in
guard let sprite = self.playerNode else {
return
}
print("stop",sprite);
}
}
```
>在 override func didMove(to view: SKView) {}內加入搖杆函數
```
// 遊戲手柄開始
override func didMove(to view: SKView) {
createJoyStick()
}
```
只需要上面這幾步,就可以添加酷酷的遊戲搖杆了,就這麼簡單!!!

遊戲教學:http://www.iFIERO.com
源碼傳送門:https://github.com/apiapia/SpaceBattleSpriteKitGame

SpriteKit手機遊戲搖杆JoyStick的使用 -- by iFIERO遊戲開發教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.