Pada dasarnya saya mencoba memasukkan tombol gamescene X2 yang melakukan fungsi berikut:
1) Ketuk untuk terbang (ini berfungsi) 2) Ketuk untuk menembakkan proyektil dari posisi Pemain (saya tidak berfungsi).
Masalah saya adalah saat ini saya mengatur fungsi fly ketika disentuh di mana saja di layar. Saya sudah mencoba yang berikut ini:
Ini mengacu pada GameScene saya: Saya pikir untuk membaginya, saya memerlukan simpul di layar untuk mereferensikan fungsi ini. Ini tidak error di konsol tetapi tidak muncul di GameScene.
// Tombol untuk memicu pemotretan :
let btnTest = SKSpriteNode(imageNamed: "Crater")
btnTest.setScale(0.2)
btnTest.name = "Button"
btnTest.zPosition = 10
btnTest.position = CGPoint(x: 100, y: 200)
self.addChild(btnTest)
Selanjutnya di kelas Player saya memiliki rincian berikut ini:
var shooting = false
var shootAnimation = SKAction()
var noshootAnimation = SKAction()
Init func:
self.run(noshootAnimation, withKey: "noshootAnimation")
let projectile = SKSpriteNode(imageNamed: "Crater")
projectile.position = CGPoint (x: 100, y: 50)
projectile.zPosition = 20
projectile.name = "projectile"
// Assigning categories to Game objects:
self.physicsBody?.categoryBitMask =
PhysicsCategory.plane.rawValue
self.physicsBody?.contactTestBitMask =
PhysicsCategory.ground.rawValue
self.physicsBody?.collisionBitMask =
PhysicsCategory.ground.rawValue
self.physicsBody?.applyImpulse(CGVector(dx: 300, dy: 0))
self.addChild(projectile)
// Start the shoot animation, set shooting to true:
func startShooting() {
self.removeAction(forKey: "noshootAnimation")
self.shooting = true
}
// Stop the shoot animation, set shooting to false:
func stopShooting() {
self.removeAction(forKey: "shootAnimation")
self.shooting = false
}
Muncul node di GameScene yang terlihat menjanjikan, akhirnya saya pindah ke bit kode terakhir di GameScene sebagai berikut:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) for touch in (touches) {
let location = touch.location(in: self)
let nodeTouched = atPoint(location)
if let gameSprite = nodeTouched as? GameSprite {
gameSprite.onTap()
}
// Check the HUD buttons which I have appearing when game is over…
if nodeTouched.name == "restartGame" {
// Transition to a new version of the GameScene
// To restart the Game
self.view?.presentScene(GameScene(size: self.size), transition: .crossFade(withDuration: 0.6))
}
else if nodeTouched.name == "returnToMenu"{
// Transition to the main menu scene
self.view?.presentScene(MenuScene(size: self.size), transition: . crossFade(withDuration: 0.6))
}
}
Player.startFly()
player.startShooting()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
Player.stopFly()
player.stopShooting()
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
Player.stopFly()
player.stopShooting()
}
override func update(_ currentTime: TimeInterval) {
player.update()
}
}
Sayangnya tidak ada yang terjadi di GameScene dan node tidak menyala ketika layar ditekan, dengan kode di atas tetap ada, saya dapat mengubahnya untuk memungkinkan fungsi 'ketuk untuk terbang' + 'ketuk untuk menembak'. Saya masih tidak tahu cara agar tombol yang telah saya nyatakan di awal GameScene muncul di mana posisi gerakan/sentuhan saya dapat dilokalisasi ke simpul ini di layar, bukan ke seluruh layar yang saya miliki saat ini. .
Saya dapat mengatakan ini terdengar lebih sederhana di kepala saya daripada benar-benar membuat kode bersama.