如何更改 touchesBegan 中 SKSpriteNode 的颜色?
How to change the color of an SKSpriteNode in touchesBegan?
我是 SpriteKit 的新手,我正在尝试更改 touchesBegan
中 SKSpriteNode
的颜色。我的尝试失败了;任何人都可以提出原因吗?
这是我的代码:
-(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
语句被执行,因为 "my mySKSpriteNode was touched!" 在接触节点时成功打印。但是,我无法更改节点的颜色。有人可以建议这样做的方法吗?
我犯了一个错误,它没有改变颜色,因为我用纹理而不是颜色初始化精灵。
我用下面的代码修复了它:
mySKSpriteNode=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];
我是 SpriteKit 的新手,我正在尝试更改 touchesBegan
中 SKSpriteNode
的颜色。我的尝试失败了;任何人都可以提出原因吗?
这是我的代码:
-(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
语句被执行,因为 "my mySKSpriteNode was touched!" 在接触节点时成功打印。但是,我无法更改节点的颜色。有人可以建议这样做的方法吗?
我犯了一个错误,它没有改变颜色,因为我用纹理而不是颜色初始化精灵。
我用下面的代码修复了它:
mySKSpriteNode=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];