我有自定义SkSpriteNode
子类。此类的名称为Unit1
。
在GameScene
:
var allUnit1:[Unit1]?
var enemy1 = Unit1(imageNamed: "1")
enemy1.position = CGPointMake(CGFloat(StartPointX), CGFloat(self.frame.height))
// I add this custom node to array. Its not relative with my
// question but I want to describe all of them.
if(allUnit1 == nil) {
allUnit1 = [enemy1]
}
else {
allUnit1?.append(enemy1)
}
self.addChild(enemy1)
self.getDamage2Unit(self.allUnit1!.first!)
// My function in gamescene. The problem starts with here. the parameter is AnyObject
// as you see in below
和getDamage2Unit
函数是(它也在GameScene
中);
func getDamage2Unit(val:AnyObject){
if(val.type == "Unit1")
println("this AnyObject is Unit1 objects")
}
}
此if
条件不起作用。我正在寻找类似的东西。我需要知道任何对象的真实类型。我怎么知道?
谢谢
答案 0 :(得分:1)
试试这个。
func getDamage2Unit(val:AnyObject){
if let myType = val as? Unit1 {
println("this AnyObject is Unit1 objects")
}
}
现在,如果您知道val将始终为Unit1
类型,那么请将AnyObject
更改为Unit1
或
func getDamage2Unit(val:AnyObject) {
let myType = val as Unit1?
if myType != nil {
println("this AnyObject is Unit1 objects")
}
}