我确定我听起来像是一个完整的菜鸟,但是我正在开发我的第一个iOS游戏,它包含一个拍摄按钮,这是一个街机风格的按钮,我想通过在用户按住时交换为“ shootPressed”图像来对其进行动画处理它下来然后释放时交换回来。
在这里,我开始,感动和结束。请记住,我遗漏了所有无冲突的代码。最近两天,我一直在解决这个问题。
更新:我已经包含了用于创建按钮节点的函数
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in:self)
let node = self.atPoint(location)
if(node.name == "Shoot") {
shootButton.texture = SKTexture(imageNamed: "shootPushed")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in:self)
let node = self.atPoint(location)
if (node.name == "Shoot") {
shootBeams()
shootButton.texture = SKTexture(imageNamed: "shootUnpushed" )
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in:self)
let node = self.atPoint(location)
if node.name == ("shoot") {
shootButton.texture = SKTexture(imageNamed: "shootPushed")
}
}
}
func addButtons() {
var buttonT1 = SKTexture(imageNamed: "shootUnpushed")
var buttonT2 = SKTexture(imageNamed: "shootPushed")
var shootButton = SKSpriteNode(texture: buttonT1)
shootButton.name = "Shoot"
shootButton.position = CGPoint(x: self.frame.maxX - 130, y: leftButton.position.y)
shootButton.zPosition = 7
shootButton.size = CGSize(width: shootButton.size.width*0.2, height: shootButton.size.height*0.2)
self.addChild(shootButton)
}
最佳答案
您在shootButton
中创建的addButtons
是局部变量。
因此,当您在touchesMoved中执行shootButton.texture = SKTexture(imageNamed: "shootPushed")
时,这肯定不是在指同一节点。
因为这显然可以编译,这意味着您已经在类中拥有属性shootButton
。如果您在var shootButton = SKSpriteNode(texture: buttonT1)
功能中更改addButtons
,则该问题很可能已得到解决。
它应该简单地初始化shootButton
属性,而不是创建一个新的局部变量:
func addButtons() {
shootButton = SKSpriteNode(texture: buttonT1)
shootButton.name = "Shoot"
shootButton.position = CGPoint(x: self.frame.maxX - 130, y: leftButton.position.y)
shootButton.zPosition = 7
shootButton.size = CGSize(width: shootButton.size.width*0.2, height: shootButton.size.height*0.2)
self.addChild(shootButton)
}