Reza Shirazian
Reza Shirazian

Reputation: 2353

runAction not working within a CCNode

I have a class that inherits from CCNode. This class has two CCSprites. When the CCNode is initialized I expect two actions to be run on the two CCSprites, however the actions are skipped.

The two CCSprite variables are infoPanel and bg.

Header:

      //THIS IS NOT WORKING

#import "cocos2d.h"

    @interface GamePopUp : CCSprite <CCTargetedTouchDelegate> 

    @property (strong,readwrite) CCSprite* infoPanel;
    @property (strong,readwrite) CCSprite* bg;

 +(id)PopupInfo;
 -(id)initForInfoPopup;
 -(void)onEnter;
 -(void)onExit;
 -(void) rotate;

 @end

Implementation

-(id)initForInfoPopup
   { self = [super init];

  if(self)
  {
      _ourDevice = [[DimensionManager SharedDimensionManager]OurDevice];
      CGSize s = [[CCDirector sharedDirector] winSize];
      infoPanel = [CCSprite spriteWithFile:@"infoPanel.png"];

    if(_ourDevice == iPad)
    {
        infoPanel = [CCSprite spriteWithFile:@"infoPanel-hd.png"];
    }

    infoPanel.position = CGPointMake(s.width/2, s.height/2);




    bg = [CCSprite node];
    bg.color = ccBLACK;
    bg.opacity = 0;
    [bg setTextureRect:CGRectMake(0, 0, s.width, s.height)];
    bg.anchorPoint = ccp(0,0);

    [bg setTag:1];



    [self addChild:bg];
    [self addChild:infoPanel];

//These actions do not take place:
    [bg runAction:[CCFadeTo actionWithDuration:4 opacity:250]];
    [infoPanel runAction:[CCSequence actions:
                          [CCScaleTo actionWithDuration:4 scale:3],
                          [CCScaleTo actionWithDuration:4 scale:1],
                          nil]];





  }
 return self;

 }

The funny thing is that the same thing works in another class I found in a tutorial. That class inherets from CCSprite

Header:

  //THIS IS WORKING
  #import "cocos2d.h"

@interface PopUp : CCSprite {
  CCSprite *window,*bg;
  CCNode *container;
}

+(id)popUpWithTitle: (NSString *)titleText description:(NSString *)description sprite:(CCNode *)sprite;
- (id)initWithTitle: (NSString *)titleText description:(NSString *)description sprite:(CCNode *)sprite;

-(void)closePopUp;

@end

Implementation:

    - (id)initWithTitle: (NSString *)titleText description:(NSString *)description sprite:(CCNode *)sprite {
    self = [super init];
    if (self) {

     _ourDevice = [[DimensionManager SharedDimensionManager]OurDevice];

    CGSize s = [[CCDirector sharedDirector] winSize];
    container = sprite;
    CCLabelTTF *desc;
      int fSize = 36;

    if (_ourDevice == iPhone)
    {
    window = [CCSprite spriteWithFile:@"uglyPopup.png"];


         desc = [CCLabelTTF labelWithString:description fontName:@"TOONISH" fontSize:fSize/2];
    }else {
        window = [CCSprite spriteWithFile:@"uglyPopup-hd.png"];   
          desc = [CCLabelTTF labelWithString:description fontName:@"TOONISH" fontSize:fSize];
    }

    window.opacity = 160;
    bg = [CCSprite node];
    bg.color = ccBLACK;
    bg.opacity = 0;
    [bg setTextureRect:CGRectMake(0, 0, s.width, s.height)];
    bg.anchorPoint = ccp(0,0);
    [bg disableTouch];
    window.position = ccp(s.width/2, s.height/2);
    window.scale = 1;


    desc.position = ccp(window.position.x, window.position.y + window.contentSize.height / 2.2);
    desc.opacity = (float)255 * .75f;

    [window addChild:desc];
    [self addChild:bg z:-1 tag:tBG];
    [self addChild:window];
    [window addChild:container z:2];

    //THESE ACTIONS RUN:
    [bg runAction:[CCFadeTo actionWithDuration:ANIM_SPEED / 2 opacity:150]];
    [window runAction:[CCSequence actions:
                           [CCScaleTo actionWithDuration:ANIM_SPEED /2 scale:.9],
                           [CCScaleTo actionWithDuration:ANIM_SPEED /2 scale:.8],
                           nil]];


    }

    return self;
}

Upvotes: 0

Views: 2040

Answers (2)

michaelgulak
michaelgulak

Reputation: 631

I had the same issue, but it turned out to not be the onEnter issue. My issue was that I had three CCSprites nested in my CCNode and was sending runAction to the top level node. The solution for my issue was simple:

Set the cascadeOpacityEnabled flag to YES on the top level CCNode:

buyBuildingDialog.cascadeOpacityEnabled = YES;
[buyBuildingDialog runAction:[CCActionFadeIn actionWithDuration:0.25]];

Upvotes: 0

Sohaib
Sohaib

Reputation: 11297

Try running your actions in onEnter function, actions will not run if your object's isRunning boolean is NO

- (void) onEnter
{
    [super onEnter];
    //These actions do not take place:
    [bg runAction:[CCFadeTo actionWithDuration:4 opacity:250]];
    [infoPanel runAction:[CCSequence actions:
                          [CCScaleTo actionWithDuration:4 scale:3],
                          [CCScaleTo actionWithDuration:4 scale:1],
                          nil]];
}

Upvotes: 2

Related Questions