runAction在CCNode中不起作用

时间:2012-08-29 07:12:36

标签: objective-c cocos2d-iphone

我有一个继承自CCNode的类。这个类有两个CCSprites。初始化CCNode时,我希望在两个CCSprit上运行两个操作,但是会跳过操作。

两个CCSprite变量是infoPanel和bg。

部首:

      //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

实施

-(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;

 }

有趣的是,同样的东西在我在教程中找到的另一个类中有效。该类来自CCSprite

部首:

  //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

实现:

    - (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;
}

2 个答案:

答案 0 :(得分:2)

尝试在onEnter函数中运行您的操作,如果对象的isRunning boolean为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]];
}

答案 1 :(得分:0)

我遇到了同样的问题,但结果却不是onEnter问题。我的问题是我在CCSprites中嵌套了三个CCNode并将runAction发送到顶级节点。我的问题的解决方案很简单:

cascadeOpacityEnabled标记设置为顶级YES的{​​{1}}:

CCNode