需要帮助在按下精灵节点按钮时给它一个动画
Need Help giving sprite node button an animation while it is being pressed
我确定我听起来像个菜鸟,但我正在开发我的第一个 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)
}
你在addButtons
中创建的shootButton
是一个局部变量。
因此,当您在 touchesMoved 中执行 shootButton.texture = SKTexture(imageNamed: "shootPushed")
时,这肯定不是指同一个节点。
因为这显然可以编译,所以这意味着您在 class 中已经有一个 属性 shootButton
。如果您更改 addButtons
函数中的 var shootButton = SKSpriteNode(texture: buttonT1)
,问题很可能会得到解决。
它应该简单地初始化 shootButton
-属性 而不是创建一个新的 local 变量:
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)
}
我确定我听起来像个菜鸟,但我正在开发我的第一个 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)
}
你在addButtons
中创建的shootButton
是一个局部变量。
因此,当您在 touchesMoved 中执行 shootButton.texture = SKTexture(imageNamed: "shootPushed")
时,这肯定不是指同一个节点。
因为这显然可以编译,所以这意味着您在 class 中已经有一个 属性 shootButton
。如果您更改 addButtons
函数中的 var shootButton = SKSpriteNode(texture: buttonT1)
,问题很可能会得到解决。
它应该简单地初始化 shootButton
-属性 而不是创建一个新的 local 变量:
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)
}