我有一个从skspritenode派生的对象
class PlayingCard : SKSpriteNode
{
var suit:Int = 0
var rank:Int = 0
var visible:Bool = false
init()
{
super.init(
texture: SKTexture(imageNamed: "card.png"),
color: NSColor.blackColor(),
size: SKTexture(imageNamed: "card.png").size())
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateSprite()
{
if (suit == 1 && rank ==1)
{ self.texture = SKTexture(imageNames: "ace_of_clubs.png") }
.
.
.
.
// A bunch of switches and if's to assign the right image
}
}
现在我初始化这个类并将其添加到我的场景中。一切正常,卡片出现了。除了当我在点击后寻找
self.nodeAtPoint(location)
时,我会得到一个SKNode
返回!!我所有的价值观都失去了吗?这是怎么回事?override func mouseDown(theEvent: NSEvent)
{
// Called when a mouse click starts
let location = theEvent.locationInNode(self)
let t = self.nodeAtPoint(location)
NSLog("%s",String(t.suit)) //<-------- Won't Compile
}
知道怎么做吗?尝试使用:
t as! PlayingCard
生成项目,但日志中会打印出乱七八糟的内容。以下是Enums和PlayingCards类:
enum Suits:Int
{
case Diamond, Heart, Club, Spade
}
enum Ranks:Int
{
case ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
class PlayingCards : SKSpriteNode
{
var CardRank:Ranks = Ranks.ace
var CardSuit:Suits = Suits.Club
var Visible:Bool = false
//var Sprite:SKSpriteNode
init()
{
super.init(texture: SKTexture(imageNamed: "b1fv.png"), color: NSColor.blackColor(), size: SKTexture(imageNamed: "b1fv.png").size())
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateSprite()
{
self.name = Description(Set: 2)
if self.Visible
{
self.texture = SKTexture(imageNamed: generateSpriteFilename())
} else {
self.texture = SKTexture(imageNamed: "b1fv.png")
}
}
func generateSpriteFilename() -> String
{
return self.Description(Set: 2) + ".png"
}
func Description(Set s:Int) -> String
{
var cr = ""
var cs = ""
switch self.CardRank
{
case .ace:
cr = (s == 1) ? "ace" : "1"
break
case .jack:
cr = (s == 1) ? "jack" : "j"
break
case .king:
cr = (s == 1) ? "king" : "k"
break
case .queen:
cr = (s == 1) ? "queen" : "q"
break
default:
cr = String(self.CardRank.rawValue + 1)
}
switch self.CardSuit
{
case .Club:
cs = (s == 1) ? "clubs" : "c"
break
case .Diamond:
cs = (s == 1) ? "diamonds" : "d"
break
case .Heart:
cs = (s == 1) ? "hearts" : "h"
break
case .Spade:
cs = (s == 1) ? "spades" : "s"
break
}
return (s == 1) ? cr + "_of_" + cs : cs + cr
}
}
以下是我如何填充数组:
func prepareDeck() -> [PlayingCards]
{
var deck = [PlayingCards]()
for i in 0...3
{
let s:Suits = Suits(rawValue: i)!
for j in 0...12
{
let r:Ranks = Ranks(rawValue: j)!
let c:PlayingCards = PlayingCards()
c.CardRank = r
c.CardSuit = s
deck.append(c)
}
}
return deck
}
最佳答案
th methodnodeAtPoint
会返回一个SKNode
。
当然,我们知道节点可能更具体,但在编译时,我们只能说它将是一个SKNode
。
现在,如果您认为对象比简单的sknode(如PlayingCard
)更具体,您可以尝试将节点强制转换为您的类型。
override func mouseDown(theEvent: NSEvent) {
let location = theEvent.locationInNode(self)
let node = self.nodeAtPoint(location)
if let playingCard = node as? PlayingCard {
// here you have your PayingCard object!
print(playingCard.suit)
}
}
在上面的代码中,只有当检索到的节点是a
IF
时,才会输入PlayingCard
的主体。在这种情况下,会创建一个类型为“cc”的新常量。所以在
playingCard
的主体中,可以使用在PlayingCard
类中定义的所有方法和属性。关于swift - 如何从单击返回SKSpriteNode子类对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33972095/