我有一个继承自 CCNode 的类。这个类有两个 CCSprite。当 CCNode 初始化时,我希望在两个 CCSprite 上运行两个动作,但是这些动作被跳过了。
两个 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;
}