【发布时间】:2020-05-25 16:37:06
【问题描述】:
基本上我正在尝试合并执行以下功能的 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 中声明的按钮出现在其中,我的手势/触摸位置可以定位到屏幕上的这个节点,而不是我目前拥有的整个屏幕。 .
我可以说这听起来比实际一起编码更简单。
【问题讨论】:
标签: xcode sprite-kit controls gesture hud