I'm trying to make a method for my CCSprite based Player class to start the player instance fading in and out until stopped by calling stopAllActions.
In my Player class I have:
- (void)pulse
{
[self setOpacity:1.0];
CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];
CCSequence *pulseSequence = [CCSequence actions:
fadeIn, // I get a warning about incompatible pointer types...
fadeOut,
nil];
[self runAction:pulseSequence];
}
This doesn't work and doesn't address the repeat forever part. I know I should probably use CCRepeatForever but I'm not seeing how to implement it correctly.
Thanks!
I have not run this, but I think others have succeeded with something like:
- (void)pulse
{
[self setOpacity:1.0];
CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:127];
CCFadeTo *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:255];
CCSequence *pulseSequence = [CCSequence actionOne:fadeIn two:fadeOut];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:pulseSequence];
[self runAction:repeat];
}
I had the same problem and it took me a loooong time to figure out why.
when you create CCSequences I found that you have to copy the CCAction.
In your case.
CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];
CCSequence *pulseSequence = [CCSequence actions:
[fadeIn copy],
[fadeOut copy],
nil];
Hope I helped.
Related
I am having a problem when trying to push a button and going to the next scene. I get the SIGABRT error. I don't know what the problem is:
[...]
//Play Button
CCMenuItem *playbutton;
playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod:)];
CMenu *menu = [CCMenu menuWithItems:itemAchievement,playbutton,itemLeaderboard, nil];
[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 50)];
// Add the menu to the layer
[self addChild:menu];
-(void)playButtonMeathod{
// Create a scene transition that uses the "RotoZoom" effect
CCTransitionRotoZoom *transition = [CCTransitionRotoZoom transitionWithDuration:1.0 scene:[Level_1 scene]];
// Tell the director to run the transition
[[CCDirector sharedDirector] replaceScene:transition];
[...]
One issue might be that the method signature is incorrect in your CCMenuItemFont selector target. Try:
CCMenuItemFont *playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod)];
I am working on project with cocos2d-android.
What I need this time is : A CCSprite comes on the screen and stay 3-4 sec and remove automatically. What class is available to do this work
If anybody have done this thing earlier. Suggest me the way ?
I'll give you code example in Objective-c cause i've never dealt with cocos2d-android, i believe it's pretty straightforward
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallBlock *block = [CCCallBlock actionWithBlock:^{
[self removeChild:spriteToDisplayAndRemove];
}];
[self runAction:[CCSequence actions:delay, block, nil]];
EDIT:
Since blocks are unavailable in cocos2d-android you might use CCCallFunc instead. Again, Objective-c sample:
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
spriteToDisplayAndRemove.tag = 100;
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallFunc *callFunc = [CCCallFunc actionWithTarget:self selector:#selector(removeSprite)];
[self runAction:[CCSequence actions:delay, callFunc, nil]];
And here is your removeSprite method:
-(void)removeSprite
{
CCSprite *sprite = [self getChildByTag:100];
[self removeChild:sprite];
}
Right now am placing the bomb after removing the first one ,now thw requirement is to place the bombs continuously and that bomb has only one sprite sheet,there am getting problem like if first bomb is placed and immediatly second bomb is placed than first bomb is not removing.
Plz help me and am new to cocos2d
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
#"Bombs.plist"];
_spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"Bombs.png"];
[_tileMap addChild:_spriteSheet z:100 ];NSMutableArray *walkAnimFrames =
[NSMutableArray array];
for(int i = 1; i <=20; ++i)
{
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName: [NSString stringWithFormat:#"Bomb_%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
_bomb= [CCSprite spriteWithSpriteFrameName:#"Bomb_1.png" ];
_bomb.position =_player.position;
[_bomb runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2f],
[CCAnimate actionWithAnimation:walkAnim],[CCCallFuncN actionWithTarget:
self selector:#selector(spriteDone:)],nil]];
[_spriteSheet addChild:_bomb z:100];
// selector to remove bomb
- (void)spriteDone:(id)sender {
[_spriteSheet removeChild:_bomb cleanup:YES];
_bombRemoved =TRUE; NSLog(#"Bool value: %d",_bombRemoved);
}
Edit 2 : make certain bomb disappears after the animation completes AND upon next touch.
// where you determine the touch should add a bomb
if(_bomb) {
[_bomb removeFromParentAndCleanup:YES];
_bomb = nil;
}
// do your stuff creating new _bomb object
// when you create the action, change to :
[_bomb runAction:[CCSequence actions:
[CCDelayTime actionWithDuration:0.2f],
[CCAnimate actionWithAnimation:walkAnim],
[CCCallBlock actionWithBlock:^{
[_bomb removeFromParentAndCleanup:YES];
_bombRemoved = YES;
_bomb=nil;
}
],nil]
];
please look the following picture
i want to do this function,when i click some button,it pop out a layer
my code is
-(id)init{
if (self = [super init]) {
CCMenuItem *successbtn = [CCMenuItemImage itemFromNormalImage:#"success.png"
selectedImage:#"success.png"
target:self
selector:#selector(successgame:)];
CCMenu *ccMenu = [CCMenu menuWithItems:successbtn, nil];
ccMenu.position=ccp(950,700);
[self addChild:ccMenu z:1 tag:2];
}
return self;
}
-(void)successgame:(id)sender{
//how can i write here?
}
so how can i write?
There is two possibilities. Either just add the button when you really want to show it and remove it from the Scenegraph as soon as it is not needed anymore.
Alternatively just make it invisible with the visible-property.
I'm trying to use CCWaves action, but it turns my screen into black, any suggestions to solve this problem?
id myWave = [CCWaves actionWithWaves:10 amplitude:7 horizontal:YES vertical:YES grid:ccg(25,20) duration:60];
[sprite runAction: [CCRepeatForever actionWithAction: myWave]];
In your AppDelegate, make sure your EAGL depthformat is 0:
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];