I'm rotating an instance of a CCLayer subclass like this:
[self runAction:[CCRotateBy actionWithDuration:0.5 angle:180.0]];
This rotating is in response to a user tap (which may come rapidly). How can I determine if the layer is currently being rotating? In this case I can just ignore the tap.
When declaring your CCAction, it is possible to set a tag attribute to it, and then retrieve the action by using getActionByTag. If the returning value is not nil, then it means that the action is actually running.
CCRotateBy *rotate = [CCRotateBy actionWithDuration:1.0 angle:100];
rotate.tag = 100;
[myNode runAction:rotate];
if ([myNode getActionByTag:100]) {
NSLog(#"Rotating!!");
}
Related
In SpriteKit, I have a simple action for my sprite:
sprite.size = CGSizeMake(40, 10);
sprite.physicsBody =[SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
sprite.position = CGPointMake(65, 230);
SKAction *changeImage = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniSlide"]];
SKAction *wait = [SKAction waitForDuration:.1];
SKAction *changeImage2 = [SKAction setTexture:[SKTexture textureWithImageNamed:#"raniStick"]];
SKAction *sequence=[SKAction sequence:#[changeImage, wait, changeImage2]];
[sprite runAction:sequence];
It changes textures, waits, then changes textures again. This is so it ducks for a certain time period. After ducking I want it to change back to its original size. When I put the code for changing size after the runAction command, it doesn't perform the first sprite.size = CGSizeMake(40, 10), and doesn't duck at all, as it stays the same size as it was in the beginning. So I was wondering how to make an if statement that when if an action has been completed, it then changes the size of the sprite.
You can't do it with an if statement, but take a look at runAction:completion:. You use it like:
[sprite runAction:sequence completion:^{
// the action is complete, change size back
}];
Cocos2d-android - I have an animation which has 5 Frames. which will rolling in position. how to make the button rolling like a globe.
I don't know if it can be done like menuitem, but you can make a sprite with forever action(animation of yours 5 frames)and in ccTouchesBegan/ccTouchesEnded you add code to go to another scene or for do an another function after touching on your sprite.
`private CCSprite animatedButton; // Make your sprite visible at begin of your class/layer.`
`animatedButton = CCSprite.sprite("yourImage.png");
CCAnimation buttonAnimation = CCAnimation.animation("", 1f);
buttonAnimation.addFrame("1.png");
buttonAnimation.addFrame("2.png");
addChild(animatedButton, 1);
CCIntervalAction buttonAction = CCAnimate.action(duration, buttonAnimation, false);
animatedButton.runAction(CCRepeatForever.action(buttonAction));
`
Now should be your button(CCSprite) animating. I didnt tried code. And now you just find out in ccTouchesBegan or ccTouchesEnded if your button was touched. If yes, you could do what you want. :)
`if(animatedButton.getBoundingBox.contains(x,y)){
CCScene scene = GameLayer.scene();
CCDirector.sharedDirector().replaceScene(scene);
}`
X and y are coordinates of touch;
You can add animations to menuitem. Like this
CCMenuItem button=CCMenuItemImage.item(image, image, this, "label");
button.setScaleX((winSize.width/8.0f)/image.getTexture().getWidth());
button.setScaleY((winSize.height/8.0f)/image.getTexture().getHeight());
button.setPosition(winSize.width/2,winSize.height+50);
CCAction actionMove42=CCSequence.actions(CCDelayTime.action(0.3f), CCEaseBackOut.action(CCMoveTo.action(1.0f, CGPoint.ccp(0,0))));
button.runAction(actionMove42);
-(void) redafter1
{
red = [CCMenuItemImage
itemFromNormalImage:#"red.png" selectedImage:#"redclick.png"];
red.position = ccp(175, 725);
redMenu = [CCMenu menuWithItems:red, nil];
redMenu.position = CGPointZero;
redMenu.scale = .75;
[self addChild:redMenu z:10];
}
How would I go about animating this object to move to another location on the screen? I am very new to this, please be basic in your explanation.
If you want to animate CCNode (this is base class of all cocos2d objects such as layers, sprites, labels, menuitems, etc.), you have to use actions mechanism. To move object, use CCMoveTo, CCMoveBy actions with runAction: method.
id moveAction = [CCMoveTo actionWithDuration: animationDuration position: targetPosition];
[nodeToAnimate runAction: moveAction];
In your case if you will run action on the object right after adding it to the some parent, that is visible (your scene or other visible parent), action will be started right after object appear.
You add [redMenu runAction:[CCMoveTo actionWithDuration:time position:place]]; (you choose time and position) at the end of your redafter1 function so when the parent method is eventually called by a method such as init your menu will move.Remember you cannot move a CCMenuItemImage because it's locked to the CCMenu's position. You have to move the CCMenu itself.Hope this was helpful.
im trying to use CCFadeOut first for my game, which is the very first action
then I want to move onto scales and then blink without the effect of previous actions.
but when i change to the next action, the effect of previous action last..
for example, if i used fadeout before scale, then change to scale, the ccsprite is faded out with momented scale action
So, what i want to do is removing previous action's effect...
can you please give me some sample code for that?
Are you sequencing the actions one after another using CCSequence?
id fadeout = [CCFadeOut actionWithDuration:2];
id scale = [CCScaleTo actionWithDuration:2 scale:2];
id blink = [CCBlink actionWithDuration:2 blinks:5];
CCSequence* sequence = [CCSequence actions:fadeout, scale, blink, nil];
[sprite runAction:sequence];
i am spending a lot for my time for a simple things i think.I want to hide and show a sprite
in scene.
myS = [CCSprite spriteWithFile:#"Background_Pause_pad.png"];
[myS setPosition:ccp(384,470)];
myS.opacity = 0;
[self addChild:myS z:1];
and when i need to appear it..
[myS runAction:[CCFadeIn actionWithDuration:1]];
and hide it
[myS runAction:[CCFadeOut actionWithDuration:1]];
but it does not work.....can anyone plz help??
Why do you use a Sequence for one action ?
You have to choose the animation you want !
E.g : if you choose CCFadeIn
[mySprite runAction:[CCFadeIn actionWithDuration:0.5f]];
I think you can try the below stuff of the code. It would work for you
id action1 = [CCFadeIn actionWithDuration:1];
id action2 = [CCDelayTime actionWithDuration:1];
id action3 = [CCFadeOut actionWithDuration:1];
[myS runAction:[CCSequence actions:action1,action2,action3,nil]];
As you need the fadein fadeout action it would generate it and display the Same.