Hey guys, this should be an easy one to figure out, but I am a novice and need help.
I have a BulletCache class that is a subclass of CCNode, and I am trying to make it talk to the Player class, which is a subclass of Character, which is a subclass of LevelObject, which inherits from CCNode.
I have an accessor method in the LevelObject class:
And here is an abridged form of the Player class's init method:
The LevelObject Class also has
and
declarations for the header and implementation files respectively.
Here is my fireBullet method:
My big issue is that when I try to create a 'fireBullet' method for my character, compile, and then tap the attack button, the game either crashes due to a method not being recognized by its respective class, or the sprite does not fire the bullets.
I am running out of ideas why, but I think it may be a node issue, coupled with me not syntatically assigning the classes correctly.
I have a BulletCache class that is a subclass of CCNode, and I am trying to make it talk to the Player class, which is a subclass of Character, which is a subclass of LevelObject, which inherits from CCNode.
I have an accessor method in the LevelObject class:
Code:
-(BulletCache*) bulletCache
{
CCNode *node = [self getChildByTag:kTagBulletCache];
NSAssert([node isKindOfClass:[BulletCache class]], @"not a BulletCache");
return (BulletCache*)node;
}
And here is an abridged form of the Player class's init method:
Code:
-(id)init
{
self = [super initWithTitle:@"barbarian king"];
if(self != nil)
{
// give ourselves a tag so other nodes can find us
// (we don't have a tag because Level adds us without one)
self.tag = kTagPlayer;
//initialize bulletCache in Player class
BulletCache* bulletCache = [BulletCache node];
[self addChild:bulletCache z:1 tag:kTagBulletCache];
//load attributes & other methods
}
The LevelObject Class also has
Code:
@property BulletCache* bulletCache;
and
Code:
@synthesize bulletCache;
declarations for the header and implementation files respectively.
Here is my fireBullet method:
Code:
-(void) fireBullet
{
//as long as we're alive and not attacking
if([self isAlive] && ![self isAttacking])
{
//Always make noise, show life, and stop walking
[self playSound:@"attack"];
[self showLife];
//[self stopWalking];
}
CCAction* action = [CCSequence actions:
[CCAnimate actionWithAnimation:[profile getAnimation:@"attack" index:currentDir]],
nil];
BulletCache *bulletCache = [player bulletCache];
CGPoint shotPos = CGPointMake(self.position.x + [self contentSize].width
* 0.5f, self.position.y);
float spread = (CCRANDOM_0_1() - 0.5f) * 0.5f;
CGPoint velocity = CGPointMake(4, spread);
[bulletCache shootBulletFrom:shotPos velocity:velocity
frameName:@"bullet1big016.png" isPlayerBullet:YES];
action.tag = kActionAttack;
[sprite runAction:action];
}
My big issue is that when I try to create a 'fireBullet' method for my character, compile, and then tap the attack button, the game either crashes due to a method not being recognized by its respective class, or the sprite does not fire the bullets.
I am running out of ideas why, but I think it may be a node issue, coupled with me not syntatically assigning the classes correctly.
Last edited: