I know this topic is covered in a hundred posts here, but I'm having a lot of trouble with this particular instance and cannot figure it out.
Basically, I'm using Spritebuilder to import sprites/nodes into my game. I import a sprite of some specific class in the body of the GameScene
class, but I want to be able to define a variable inside of my sprite class, and then edit it from the GameScene
class. For example, if my sprite collects a coin inside the GameScene, I want to change the speed of my sprite inside of the update
method in the sprite class.
Below is my code, but unfortunately it isn't working. The variable increaseY
and increaseX
do not appear to be available in my GameScene
class. I know this is because I didn't instantiate the Penguin
class properly, but I don't know how to properly create an instance of this class while simultaneously importing the .ccbi file of it. The problem line is commented on and has a bunch of ** next to it to easily find it. It's in GameScene.m
. I really appreciate the help, been stuck on this for several hours.
Penguin.h
#import "CCSprite.h"
@interface Penguin : CCSprite
{
float xPosition;
float yPosition;
}
@property (nonatomic,assign) float increaseY;
@property (nonatomic,assign) float increaseX;
@end
Penguin.m
#import "Penguin.h"
@implementation Penguin
@synthesize increaseX;
@synthesize increaseY;
- (id)init {
self = [super init];
if (self) {
CCLOG(@"Penguin created");
}
return self;
}
-(void) update:(CCTime)delta
{
self.position = ccp(self.position.x + increaseX,self.position.y + increaseY);
}
@end
GameScene.h
#import "CCNode.h"
@interface GameScene : CCNode
@end
GameScene.m
#import "GameScene.h"
#import "Penguin.h"
@implementation GameScene
{
CCPhysicsNode *_physicsNode;
CCNode *_catapultArm;
CCNode *_levelNode;
CCNode *_contentNode;
}
// is called when CCB file has completed loading
- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
CCScene *level = [CCBReader loadAsScene:@"Levels/Level1"];
[_levelNode addChild:level];
}
// called on every touch in this scene
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
[self launchPenguin];
}
- (void)launchPenguin {
// loads the Penguin.ccb we have set up in Spritebuilder
CCNode* penguin = [CCBReader load:@"Penguin"];
penguin.position = ccpAdd(_catapultArm.position, ccp(16, 50));
[_physicsNode addChild:penguin];
//THE FOLLOWING LINE DOES NOT WORK********************************
penguin.increaseY = 1;
// Gives Error------Property "increaseX" not found on object of type "CCNode *"
self.position = ccp(0, 0);
CCActionFollow *follow = [CCActionFollow actionWithTarget:penguin worldBoundary:self.boundingBox];
[_contentNode runAction:follow];
}