gpt4 book ai didi

ios - 在 Touches 中获取 SKSpriteNode 的属性

转载 作者:可可西里 更新时间:2023-11-01 02:11:46 25 4
gpt4 key购买 nike

我试图在 touchesBegan 方法中读取 SKSpriteNode 的属性,但该属性不存在。在其他地方创建的对象上的位置。

 let enemy = enemy(imageName: "enemy.png",force: "12")
addChild(enemy)
enemy.name = "enemy"
print (enemy.force) // 12

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let touchLocation = touch.location(in: self)
let touchedNode = self.atPoint(touchLocation) as! SKSpriteNode
if(touchedNode.name == "enemy"){
print(enemy.force) //Force property does not exist
}
}

最佳答案

知道 SKSpriteNode 没有 force 属性,您应该使用继承 SKSpriteNode 属性的类名(用于制作 敌人..)

一个例子可能是这样的:

class Enemy : SKSpriteNode {
var force: Int = 0
...
}

然后在你的游戏场景中做:

...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let touchLocation = touch.location(in: self)
let touchedNode = self.atPoint(touchLocation)
if(touchedNode.name == "enemy" && touchNode is Enemy){
// Yes, I'm absolutely sure this is an enemy node..
let enemy = touchedNode as! Enemy
print(enemy.force)
}
}

关于ios - 在 Touches 中获取 SKSpriteNode 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40784025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com