โดยพื้นฐานแล้ว ฉันกำลังพยายามรวมปุ่ม X2 gamescene ที่ทำฟังก์ชันต่อไปนี้:
1) แตะเพื่อบิน (ฉันทำงานอยู่) 2) แตะเพื่อยิงกระสุนปืนจากตำแหน่งผู้เล่น (ฉันไม่มีงาน)
ปัญหาของฉันคือขณะนี้ฉันได้ตั้งค่า fly func เมื่อสัมผัสที่ใดก็ได้บนหน้าจอ ฉันได้ลองทำสิ่งต่อไปนี้แล้ว:
นี่เป็นการอ้างอิงถึง GameScene ของฉัน: ฉันคิดว่าเพื่อที่จะแยกสิ่งนี้ออก ฉันจะต้องมีโหนดบนหน้าจอเพื่ออ้างอิงฟังก์ชันนี้ สิ่งนี้ไม่ได้เกิดข้อผิดพลาดในคอนโซลแต่จะไม่ปรากฏใน GameScene
// ปุ่มเพื่อเริ่มการถ่ายภาพ :
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)
ถัดไปในคลาส Player ฉันมีรายละเอียดดังต่อไปนี้:
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
}
โหนดปรากฏใน GameScene ซึ่งดูมีแนวโน้มดี ในที่สุดฉันก็ย้ายไปยังโค้ดบิตสุดท้ายใน GameScene ดังนี้:
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()
}
}
น่าเสียดายที่ไม่มีอะไรเกิดขึ้นใน GameScene และโหนดไม่ทำงานเมื่อกดหน้าจอ โดยมีโค้ดด้านบนอยู่ที่นั่นอยู่แล้ว ฉันสามารถแก้ไขสิ่งนี้เพื่อให้สามารถใช้ฟังก์ชัน 'แตะเพื่อบิน' + 'แตะเพื่อยิง' ได้ ฉันยังนึกไม่ออกว่าจะรับปุ่มที่ฉันประกาศตั้งแต่เนิ่นๆ ใน GameScene ได้อย่างไรเพื่อให้ปรากฏ โดยที่ตำแหน่งท่าทาง/สัมผัสของฉันสามารถแปลไปที่โหนดนี้บนหน้าจอได้ ซึ่งตรงข้ามกับทั้งหน้าจอที่ฉันมีอยู่ในปัจจุบัน .
ฉันสามารถพูดได้ว่านี่ฟังดูง่ายกว่าในหัวของฉันตั้งแต่เริ่มต้นมากกว่าการเขียนโค้ดด้วยกัน