0

I have two class files hudlayer.m and actionlayer.m

I have a method named jump in hudlayer.m And i have a method named jumpone in actionlayer.m

-(void) jumpone {
    _heroBody->ApplyLinearImpulse(b2Vec2(_playerVelX/[_lhelper pixelsToMeterRatio], 1.25), _heroBody->GetWorldCenter()); 
}

and another method called jump in hudlayer.m

-(void)jump {
   ActionLayer *aob = [[ActionLayer alloc] init]; 
   [aob jumpone];
}

The problem is when i call the Jumpone method from actionlayer.m my sprite jumps (i.e method called)

My init method of action layer

- (id)initWithHUD:(HUDLayer *)hud
{
    if ((self = [super init])) {

        [self setupWorld]; 
    }
    return self;
}

But when i call jumpone via jump method in from hudlayer.m it fails and my app crashed. Any help will be appreciated .thanks

vishnu
  • 869
  • 1
  • 16
  • 25

2 Answers2

0

Everytime you call jump it creates a new instance of you ActionLayer. And following that, you setup a new world and everything get tangled up. Furthermore its a memory leak.

Make you ActionLayer to an iVar of HUDLayer and call

   aob = [[ActionLayer alloc] init];

in the HUDs init method. Dont forget to release aob in dealloc of the HUDLayer

zeiteisen
  • 7,078
  • 5
  • 50
  • 68
0

the best solution for your problem is to add a tag to hudlayer & action layer

ex: hudlayer.tag=1; actionlayer.tag=2;

and then just use getChildByTag like this:

[[[[CCDirector sharedDirector]runningScene] getChildByTag:1]jumpone];
skytz
  • 2,201
  • 2
  • 18
  • 23
  • Hi thanks for reply...but if i add HUDLayer.tag = 2; i get an error "property tag not found on object of HUDlayer" – vishnu Jun 25 '12 at 15:20
  • well not HUDLayer.tag literally...i ment add self.tag in hud layer init method...i should've been more specific – skytz Jun 25 '12 at 15:59
  • Thanks a lot bro its working now :) bro can i get your Skype ? – vishnu Jun 25 '12 at 16:01
  • well..funny thing..i dont have skype =) but you can contact me through email (see profile) – skytz Jun 25 '12 at 16:15