2015-09-26 59 views
1

我有一个游戏,我触摸一个节点,它运行一些代码。触摸时如何更改SKShapeNode的颜色?我尝试使用node.fillColor = SKColor.blackcolor()作为例子,但它说我不能这样做,即使这是我最初创建SKShapeNode的颜色的原因。触摸时改变SKShapeNode的颜色?

这是所有写作触摸开始的方法。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let positionOfTouch = touch.locationInNode(self) 
     let node = nodeAtPoint(positionOfTouch)   
     node.userInteractionEnabled = true 
     node.fillColor = UIColor.whiteColor() 

这是我正在使用的代码。但它一直说“SKNode类型的值没有成员”fillColor“...但”fillColor“在我的DidMoveToView中工作 谢谢

+0

我们展示的代码。你确定你正在拾取正确的节点吗? – Grimxn

+0

覆盖func touchesBegan(触及:设置,withEvent even T:的UIEvent){ 用于触摸:AnyObject在触摸{ 让positionOfTouch = touch.locationInNode(个体) 令节点= nodeAtPoint(positionOfTouch) node.userInteractionEnabled =真 node.fillColor = UIColor.whiteColor() 这是我正在使用的代码。但它一直说“SKNode类型的值没有成员”fillColor“......但”fillColor“在我的DidMoveToView中工作.. – Questions

回答

0

您可能碰到的节点可能是父节点或子节点 - SKNode ,而不是SKShapeNode。这就是nodeAtPoint回报,通用SKNode没有fillColor。你需要检查它是你被测试期待的类。喜欢的东西...

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let positionOfTouch = touch.locationInNode(self) 
     if let node = nodeAtPoint(positionOfTouch) as? SKShapeNode { 
      node.userInteractionEnabled = true 
      node.fillColor = UIColor.whiteColor() 
+0

谢谢你Grimxn!简直不能相信这是很容易解决的问题。 – Questions