[我想知道是否有人可以提供建议,以使我能够更改以下代码,本质上,我正在尝试合并具有以下功能的X2游戏场景按钮:
1)点按即可飞行(我有工作)2)点击以从玩家位置射击弹丸(我没有工作)。
我的问题是,当我触摸屏幕上的任何位置时,我当前都设置了飞行功能。我已经尝试了以下操作:
这是关于我的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中声明的按钮出现,使我的手势/触摸位置可以定位到屏幕上的该节点,而与我当前所在的整个屏幕相对。 。
[我可以说,从一开始,这听起来比实际一起编码更容易……做一场噩梦。
[任何人都可以帮助建议我在哪里可以纠正此代码?
再次感谢!汤姆
首先,以上代码没有使用键"shootAnimation"
来运行动画的任何调用,并且在stopShooting()
时没有重新运行未拍摄动画的调用以及重新运行拍摄动画的调用都没有在startShooting()
中。这些方法应包括如下所示]