I have written a subclass of SKSpriteNode to represent a flipper i use a number of times in my game. I am trying to set the color of these by setting a child sprites color property but this is only appears to work on one instance of my sprites, regardless of which one i am trying to set. Please can someone tell me what i am doing wrong or suggest a better way of doing this. i just want to be able to set different instances of my sprite subclass to different colors.
my SKSpriteNode sub class is as follows:
#import "RSPaddleSprite.h"
@implementation RSPaddleSprite
SKSpriteNode *sprite;
-(id)init{
if (self = [super init]) {
sprite = [SKSpriteNode spriteNodeWithImageNamed:@"paddle2.png"];
[self addChild: sprite];
}
return self;
}
-(id)initWithImageNamed:(NSString *)name{
if (self = [super init]) {
sprite = [SKSpriteNode spriteNodeWithImageNamed:name];
[self addChild: sprite];
}
return self;
}
//function applies physics body to sprite when required
-(void)applyPhysicsBody{
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(sprite.size.width/4, sprite.size.height )];
self.physicsBody.affectedByGravity = FALSE;
self.physicsBody.mass = 0.1;
self.physicsBody.dynamic = TRUE;
}
//function sets color of sprite when required
-(void) setPaddleColor:(SKColor*)color{
sprite.color = color;
sprite.colorBlendFactor = 0.9;
}
@end
i am calling the subclass functions in my scene as follows:
//initialise paddles, position them, add physics and add to scene
paddle1 = [[RSPaddleSprite alloc ]initWithImageNamed:@"paddle1.png"];
paddle1.position = CGPointMake(self.frame.size.width/5,(self.frame.size.height/2)+30);
[paddle1 applyPhysicsBody];
[self addChild:paddle1];
paddle2 = [[RSPaddleSprite alloc ]initWithImageNamed:@"paddle1.png"];
paddle2.position = CGPointMake((self.frame.size.width/5)*4,(self.frame.size.height/2)+30);
[paddle2 applyPhysicsBody];
[self addChild:paddle2];
paddle3 = [[RSPaddleSprite alloc ]initWithImageNamed:@"paddle1.png"];
paddle3.position = CGPointMake((self.frame.size.width/3),120);
[paddle3 applyPhysicsBody];
[self addChild:paddle3];
paddle4 = [[RSPaddleSprite alloc ]initWithImageNamed:@"paddle1.png"];
paddle4.position = CGPointMake((self.frame.size.width/3)*2,120);
[paddle4 applyPhysicsBody];
[self addChild:paddle4];
//set paddle to appropriate color
[paddle1 setPaddleColor:[SKColor colorWithRed:0.15 green:0.15 blue:0.55 alpha:1.0]];
[paddle2 setPaddleColor:[SKColor colorWithRed:0.15 green:0.15 blue:0.55 alpha:1.0]];
[paddle3 setPaddleColor:[SKColor colorWithRed:0.15 green:0.15 blue:0.55 alpha:1.0]];
[paddle4 setPaddleColor:[SKColor colorWithRed:0.15 green:0.15 blue:0.55 alpha:1.0]];
@implementation RSPaddleSprite
SKSpriteNode *sprite;
This makes "sprite" a global variable, so you really only have one instance referenced by sprite
.
This is the correct form to make sprite
an ivar:
@implementation RSPaddleSprite
{
SKSpriteNode *sprite;
}
More correctly stick to Cocoa naming conventions where ivars are prefixed with an underscore:
@implementation RSPaddleSprite
{
SKSpriteNode *_sprite;
}