2015-05-04 101 views
0

我是SpriteKit的新手,我试图改变touchesBeganSKSpriteNode的颜色。我的尝试失败了;任何人都可以提出一个理由,为什么?如何更改touchesBegan中SKSpriteNode的颜色?

这是我的代码: “我mySKSpriteNode被感动”

-(id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]) { 
    /* Setup your scene here */ 

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0]; 

    mySKSpriteNode=[SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImage:[UIImage imageNamed:@"bg-1.png"]] size:CGSizeMake(50, 50)]; 
    [mySKSpriteNode setPosition:CGPointMake(150, 300)]; 
    mySKSpriteNode.name = @"thisIsMySprite"; // set the name for your sprite 
    mySKSpriteNode.userInteractionEnabled = NO; 

    [self addChild:mySKSpriteNode]; 
    } 
    return self; 
} 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:location]; 
    if ([node.name isEqualToString:@"thisIsMySprite"]) 
    { 
     NSLog(@"mySKSpriteNode was touched!"); 
     [mySKSpriteNode setColor:[UIColor whiteColor]]; 
    } 

} 

if语句执行,如在触摸节点时成功打印。但是,我无法更改节点的颜色。有人可以提出一种方法吗?

+0

可能是什么错误改变颜色的了Syntex。 –

+0

你可以使用这个:http://stackoverflow.com/a/22430901/437146 – NightFury

+0

你的意思是说我必须使用SKColor而不是UIColor? –

回答

0

我犯了一个错误,它没有改变颜色,因为我用纹理初始化了精灵而不是颜色。

我用下面的代码固定它:

mySKSpriteNode=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)]; 
+1

请将您自己的答案标记为正确。这将使人们看到什么解决了问题,并且可以节省其他用户寻找未解决问题的时间。 – Andriko13

+0

我知道亲爱的......但是因为我的废话,我可以做明天.. –