节点与子类的联系
Contact of Nodes with Subclass
我正在创建一个 class 以将我的流星添加到场景中:
import SpriteKit
class Meteor: SKShapeNode {
var meteorNode : SKShapeNode?
var size : CGSize!
//Acoes
var acaoApagar = SKAction.removeFromParent()
var acaoAndar : SKAction!
//Numero Randomico
var numRandom : Int = 0
var textoStringMeteor : String!
//Colisoes
let CollisionShoot : UInt32 = 0x1 << 0
let CollisionHouse : UInt32 = 0x1 << 1
let CollisionMeteor : UInt32 = 0x1 << 2
init(size : CGSize, pontoMeteor: CGPoint, textoMeteor: String)
{
super.init()
//super.init(ellipseOfSize: <#CGSize#>)
self.size = size
self.textoStringMeteor = textoMeteor
self.acaoAndar = SKAction.moveTo(pontoMeteor, duration: 10)
criarMeteoro()
setarTexto(self.textoStringMeteor)
}
func setarTexto(numeroLabel : String) {
let numNode = SKLabelNode(fontNamed:"Chalkduster")
numNode.text = numeroLabel;
numNode.fontSize = 20;
numNode.position = CGPoint(x: 0, y: -10);
numNode.name = "textoNode"
meteorNode?.addChild(numNode)
}
func criarMeteoro() {
var randomX = CGFloat(Int(arc4random()) % Int(size.width))
//Meteor Node
meteorNode = SKShapeNode(circleOfRadius: CGFloat(20))
meteorNode!.physicsBody = SKPhysicsBody(circleOfRadius: 20)
meteorNode!.physicsBody!.dynamic = true
meteorNode!.fillColor = UIColor.blueColor()
meteorNode!.physicsBody!.categoryBitMask = CollisionMeteor
meteorNode!.physicsBody!.contactTestBitMask = CollisionShoot | CollisionHouse
meteorNode!.position = CGPoint(x: randomX, y: size.height + 900)
meteorNode!.name = "meteorNode"
self.addChild(meteorNode!)
meteorNode!.runAction(SKAction.sequence([acaoAndar, acaoApagar]))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在这里调用 class Meteor 并传递所有参数:
var meteor = Meteor(size: CGSize(width: size.width, height: size.height), pontoMeteor: ponto, textoMeteor: "30")
foreground.addChild(meteor)
最后我有我的联系人,看看节点之间是否有联系:
func didBeginContact(contact: SKPhysicsContact) {
var bodyA = contact.bodyA!.node!
var bodyB = contact.bodyB!.node!
if bodyA.name == "tiroCanhao" && bodyB.name == "meteorNode"
{
bodyB.removeAllActions()
bodyB.removeFromParent()
bodyA.removeFromParent()
self.pointsPlayer++
self.qtdtiros += 2
}
else if bodyA.name == "meteorNode" && bodyB.name == "tiroCanhao"
{
bodyA.removeAllActions()
bodyA.removeFromParent()
bodyB.removeFromParent()
self.pointsPlayer++
self.qtdtiros += 2
}
}
现在我的问题如下:
联系后,我想在对象的 Meteor class 上调用 setarTexto 方法并更改文本,但这不起作用。我收到此错误:
Could not cast value of type 'SKShapeNode' (0x10bb5ea88) to
'MathDefense.Meteor' (0x10af6b7b0).
我正在做的是这个(在联系的 if 语句上):
let text = bodyB as! Meteor
text.setarTexto("20")
我什至试过了
text.textoStringMeteor = "20"
但其中 none 有效。我已经做了一些研究,但没有发现任何人遇到我的问题或试图做类似的事情。
有什么解决办法吗?
谢谢。
我找到了解决方案,为了修复我的错误,我做了以下操作:
删除 SKShapenode,因为 class 已经是 SKShapenode(这是错误)。
然后在创建我的节点时,我用自己做了一切,而不是放入流星节点。这解决了所有问题。
示例:
右:self.position = CGPoint(x: randomX, y: size.height + 900)
错误:meteorNode!.position = CGPoint(x: randomX, y: size.height + 900)
如果您使用的是 SKSpritenode,要传递纹理,只需将其放在您的 superinit 上即可。
我正在创建一个 class 以将我的流星添加到场景中:
import SpriteKit
class Meteor: SKShapeNode {
var meteorNode : SKShapeNode?
var size : CGSize!
//Acoes
var acaoApagar = SKAction.removeFromParent()
var acaoAndar : SKAction!
//Numero Randomico
var numRandom : Int = 0
var textoStringMeteor : String!
//Colisoes
let CollisionShoot : UInt32 = 0x1 << 0
let CollisionHouse : UInt32 = 0x1 << 1
let CollisionMeteor : UInt32 = 0x1 << 2
init(size : CGSize, pontoMeteor: CGPoint, textoMeteor: String)
{
super.init()
//super.init(ellipseOfSize: <#CGSize#>)
self.size = size
self.textoStringMeteor = textoMeteor
self.acaoAndar = SKAction.moveTo(pontoMeteor, duration: 10)
criarMeteoro()
setarTexto(self.textoStringMeteor)
}
func setarTexto(numeroLabel : String) {
let numNode = SKLabelNode(fontNamed:"Chalkduster")
numNode.text = numeroLabel;
numNode.fontSize = 20;
numNode.position = CGPoint(x: 0, y: -10);
numNode.name = "textoNode"
meteorNode?.addChild(numNode)
}
func criarMeteoro() {
var randomX = CGFloat(Int(arc4random()) % Int(size.width))
//Meteor Node
meteorNode = SKShapeNode(circleOfRadius: CGFloat(20))
meteorNode!.physicsBody = SKPhysicsBody(circleOfRadius: 20)
meteorNode!.physicsBody!.dynamic = true
meteorNode!.fillColor = UIColor.blueColor()
meteorNode!.physicsBody!.categoryBitMask = CollisionMeteor
meteorNode!.physicsBody!.contactTestBitMask = CollisionShoot | CollisionHouse
meteorNode!.position = CGPoint(x: randomX, y: size.height + 900)
meteorNode!.name = "meteorNode"
self.addChild(meteorNode!)
meteorNode!.runAction(SKAction.sequence([acaoAndar, acaoApagar]))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在这里调用 class Meteor 并传递所有参数:
var meteor = Meteor(size: CGSize(width: size.width, height: size.height), pontoMeteor: ponto, textoMeteor: "30")
foreground.addChild(meteor)
最后我有我的联系人,看看节点之间是否有联系:
func didBeginContact(contact: SKPhysicsContact) {
var bodyA = contact.bodyA!.node!
var bodyB = contact.bodyB!.node!
if bodyA.name == "tiroCanhao" && bodyB.name == "meteorNode"
{
bodyB.removeAllActions()
bodyB.removeFromParent()
bodyA.removeFromParent()
self.pointsPlayer++
self.qtdtiros += 2
}
else if bodyA.name == "meteorNode" && bodyB.name == "tiroCanhao"
{
bodyA.removeAllActions()
bodyA.removeFromParent()
bodyB.removeFromParent()
self.pointsPlayer++
self.qtdtiros += 2
}
}
现在我的问题如下:
联系后,我想在对象的 Meteor class 上调用 setarTexto 方法并更改文本,但这不起作用。我收到此错误:
Could not cast value of type 'SKShapeNode' (0x10bb5ea88) to 'MathDefense.Meteor' (0x10af6b7b0).
我正在做的是这个(在联系的 if 语句上):
let text = bodyB as! Meteor
text.setarTexto("20")
我什至试过了
text.textoStringMeteor = "20"
但其中 none 有效。我已经做了一些研究,但没有发现任何人遇到我的问题或试图做类似的事情。
有什么解决办法吗?
谢谢。
我找到了解决方案,为了修复我的错误,我做了以下操作:
删除 SKShapenode,因为 class 已经是 SKShapenode(这是错误)。
然后在创建我的节点时,我用自己做了一切,而不是放入流星节点。这解决了所有问题。
示例:
右:self.position = CGPoint(x: randomX, y: size.height + 900)
错误:meteorNode!.position = CGPoint(x: randomX, y: size.height + 900)
如果您使用的是 SKSpritenode,要传递纹理,只需将其放在您的 superinit 上即可。