BulletCache and Accessor Problems

Discussion in 'iOS Programming' started by TwinofSian, Jan 30, 2012.

  1. TwinofSian, Jan 30, 2012
    Last edited: Jan 30, 2012

    TwinofSian macrumors member

    Joined:
    Oct 23, 2011
    #1
    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:

    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.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Thanks for sharing neither the crash log, the method that wasn't recognized, nor even the class that wasn't able to recognize it.

    In all seriousness, we can't help much without knowing what the issue is.

    Also, doesn't Cocos2D have its own forums, where the majority of forum goers would actually be familiar with
    ?

    I'm not sure how much help you'll be able to get here (I don't personally know much about Cocos2D... I'd rather build my own game engines from scratch just so I get the experience dealing with low level stuff.)
     
  3. TwinofSian, Jan 30, 2012
    Last edited: Jan 30, 2012

    TwinofSian thread starter macrumors member

    Joined:
    Oct 23, 2011
    #3
    well the fireBullet method is where the crashes happen, because let's say I'll make -(BulletCache*) bulletCache a class method by changing it to
    +(BulletCache*) bulletCache.

    I'll get a compile with a warning telling me that "LevelObject may not respond to +getChildByTag:"

    Tapping the attack button yields a nice sweet crash, but I get no such warning when it's an instance variable.

    Then what usually happens, when I try to call the the bulletCache class method 'shootBulletFrom' method inside the 'fireBullet' method, I get a "player may not respond to '+shootBulletFrom' so the last few weeks I have been trying to find a way to accessorize the BulletCache class with the Player class.


    I thought I was really close, but now I dont think so. I may have to put it in the GameLayer class and accessorize there, but the frustrating thing is that I simply don't know anymore.


    I have no goddamn clue.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Are you trying to send messages to the class or an instance of the class? Be very clear. If you don't know what this means stop and learn the basics of object oriented programming.
     

Share This Page