CCSprite comes and after 3 sec it'll remove automatically ?[Done] - cocos2d-android

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

Related

Scene Change Crash in cocos2d

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)];

Cocos2d: ARC releasing CCSprite too soon?

I enabled ARC for my cocos2d project.
Now i try doing the following:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
[self addChild:menu];
And in the BuildTowerMenu class:
+(id)menuAtLocation:(CGPoint)location {
return [[self alloc] initMenuAt:location];
}
-(id) initMenuAt:(CGPoint)location {
if (self = [super init]) {
self.position = location;
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[self addChild:item1];
}
return self;
}
But for some reason, the Sprite never shows up. After a bit of debugging i see that when i return from menuAtLocation, the CCSprite is still in the Array of children of BuildTowerMenu, but empty (only got an id).
If i actually add the sprite from outside it works and the Sprite is displayed:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[menu addChild:item1];
[self addChild:menu];
Any hints on what i did wrong here?
P.S.: i added a breakpoint in the dealloc of CCSprite, which never gets called (i guess it should be called if ARC is releasing it)...
I think that the problem is that you set equal positions to your menu and sprite.
I mean that this part of code
CGPoint location = ccp(200.f, 200.f);
[menu setPosition: location];
[sprite setPosition: location];
[menu addChild: sprite];
will add your sprite with position position (400.f, 400.f), relatieve to the menu's parent. You are doing almost the same thing in your BuildTowerMenu's initMenuAt: method/
As already commented on the initial question_:
Ok, it had nothing to do with ARC; the Sprite which seemed to be released was there, it was just a debugger - bug which didnt display it correctly. The problem was actually the line self.position = location; After moving the setposition after the addChild, everything worked.

CCRepeatForever action stops automatically while switching scenes

I am developing a simple game app.
I am required to have two screens one on left & other on right, just like on scroll view with pagination having 2 screens to toggle between. I am detecting swipes with the help of RootViewController class and successfully swiping left and right screens.
But the problem is I have to have infinitely running rotateBy action on both screens each running this action on single sprite placed on center of screen.
I am using a main scene called SelectWorld & two are it's sub scenes.
First screen is called factory & other is called stack.
Following is my code:
Select World Screen:
#implementation SelectWorld
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
SelectWorld* layer = [SelectWorld node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
[RootViewController singleton].swipeCallBackHandler = self;
[[RootViewController singleton] enableSwipeDetection];
factory = [[ColorfulFactory scene] retain];
stack = [[CoinsStack scene] retain];
[self addChild:factory];
isOnFactory = YES;
}
return self;
}
-(void)swipedLeft
{
if(isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = NO;
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:stack];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
-(void)swipedRight
{
if(!isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = YES;
CCTransitionScene* transitionalScene = [CCTransitionSlideInL transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
Here's the code for factory only, same goes for stack-
#implementation ColorfulFactory
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
ColorfulFactory* layer = [ColorfulFactory node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
CGSize size = [CCDirector sharedDirector].winSize;
CCLabelTTF* info = [CCLabelTTF labelWithString:#"Colorful Factory" fontName:#"Helvetica" fontSize:35.0];
info.position = ccp(size.width/2, size.height-50);
[self addChild:info];
CCSprite* sprite = [CCSprite spriteWithFile:#"Icon.png"];
sprite.position = ccp(size.width/2, size.height/2);
[self addChild:sprite];
sprite.tag = 123;
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:60 angle:360.0];
CCRepeatForever* repeatForever = [CCRepeatForever actionWithAction:rotateBy];
[sprite runAction:repeatForever];
repeatForever.tag = 456;
}
return self;
}
Now the problem is, in first two swipes actions are running fine, but as soon as I try to swipe more than two or three times, actions got stopped. I haven't written a single line to stop this in both the classes. I require it there running forever.
Your mistake is that you're re-using the same scene object for every scene change. You can't do that in cocos2d, you have to create a new instance of the scene object.
You should never retain a scene object, since there ought to be only one active scene at any one time (with the exception of the duration of a scene transitioning to another scene - but that's handled by cocos2d internally).
For example:
CCScene* factory = [ColorfulFactory scene];
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
If you need both objects in memory, make them layers (or nodes), not scenes. Don't use replaceScene but instead animate the layers with regular moveTo actions in and out of the screen.

Cocos2d fade in/out action to repeat forever

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.

how to remove timer

i have this timer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerFunc) userInfo:nil repeats:YES];
and on some function i want to completly remove this...how?
You should not use NSTimer in Cocos2d, instead you should do:
[self schedule:#selector(updateTimerFunc) interval:1.0];
And to remove it:
[self unschedule:#selector(updateTimerFunc)];
or:
[self unscheduleAllSelectors];
If you have an update-function you could use: [self scheduleUpdate];