Well I try to implement the run action to move a specific sprite towards the specific destination, I can achieve that using the below code snippet.
[spriteA runAction:[CCSequence actions:[CCMoveTo actionWithDuration:0.2 position:realDest],[CCCallFuncN actionWithTarget:self selector:#selector(spriteMoveFinished:)],nil]];
But now the problem arise that the sprites moves perfectly but the fixture & bodydef of that sprite stays on it previous positions. Please guide me that how can I move the body & fixture along with the sprite using 'runAction' or in any other way.
Thanks & Regards,
Zahur
You have to apply force/impulse to the body and then set the sprite position to body position.
Related
I am new to cocos2d-x and I am implementing my own flappy bird.
Now I am working with the collision detection and I am stuck.
I added the down_pipe sprite and the up_pipe sprite to a parent sprite pipes, and I move the parent shift from right to left.
But when I check the collision by
if (bird->boundingBox().intersectsRect(pipes->boundingBox()))
It doesn't work.
And I try this way:
if (bird->boundingBox().intersectsRect(pipes->getChildByTag(DOWN_PIPE)->boundingBox()) || bird->boundingBox().intersectsRect(pipes->getChildByTag(UP_PIPE)->boundingBox()))
It still doesn't work.
How can I solve the problem? Any advice?
You try this,
CCRect target = CCRectMake(pipes->getPosition().x - (pipes->getContentSize().width/2),pipes->getPosition().y - (pipes->getContentSize().height/2),pipes->getContentSize().width,pipes->getContentSize().height);
if (bird->boundingBox().intersectsRect(target))
I'm making a platform game, and everything was great. My sprite moved when i touched the left side of the screen, and jumped when i touched the right side. But then i decided to make it move by itself. so i added the ccmoveto function, but now it does not jump! I'm new to cocos2d but everything is working ok, except this, already searched but couldn't find the answer can someone please help me?
I tried everything, but it only jumps if i delete the ccmoveto action.
I'm using cocos2d 2.0
Thank you!!
CcMoveTo will override any manual position changes, inluding changes from other actions like CCJump. Your character is set to move to destination in a straight line, no matter what.
It's issues like these why I always recommend not to use actions for gameplay logic. Especially position, you need to retain full control over it. Use a direction vector and integrate position every update: and you're free to do everything you need.
my advice is to use one of the physics engines provided with cocos2d: Box2D and Chipmunk physics. Thanks to this engines you can define the characteristics of the world (i.e. gravity vector) a shape and a mass for your sprite (i.e. a rectangle with a weight). Then when you need it to jump you will just create a force vector with the characteristics you need (i.e. angle, etc.) and keep updated your sprite with its physical body. This will make your sprite jump and land quite realistically.
I'm trying to add a SpriteSheet that is shown in the layer but not animated yet, I want to animate onClick.
The problem is when adding the sprite to the layer, the sprite is not shown if its not animated on start.
In Cocos2D-iphone I have not this problem.
Thanks in advance for any help provided.
Just have a look on the coocs2d-android master file :
cocos2d-master\cocos2d-android\src\org\cocos2d\tests\ActionTest.java
In this you can find the Animation class and compare your code with the test file...
or
write down your code ...
Alternatively, show a static image (first sprite) until user clicks, then load the sprites and start animation.
Is there any possibility to show only a part of an CCSprite?
It seams that contentSize property doesn't have a good result.
I think you might have to create a new sprite for this. The general pseudo code is this.
CCTexture2D *origTexture = originalSprite->getTexture();
CGRect rect = {0, 0, 20, 20};
CCSprite *destSprite = CCSprite::spriteWithTexture(origTexture, CGRect);
Both doc_180's and James' answers work by creating new CCSprite using a portion of the texture, but if you are using clipping method, you will get CCSprite that uses the full texture but have the ability to only draw a portion of it on screen. One advantage of this method is you are able to modify how big or small the portion that you want shown or hidden on the fly rather than having to re-create the CCSprite again and again (or replacing the texture again and again).
So, to use the clipping method, simply download the ClippingNode class from here, and add the CCSprite you want clipped to that ClippingNode. Then you call one of its methods to specify which region to limit the drawing to. I'm currently using it to create a progress bar so I know for sure it works great.
Get the [sprite displayedFrame], change the frame of that, and create a new sprite with that spriteframe: CCSprite *sprite2 = [CCSprite spriteWithSpriteFrame:frame]
Can anyone tell me how to vibrate/shake CCSprite in cocos2d???plz give me some example.
If the object is static you could create a CCSequence of MoveTo actions - pixel or two to the one side and pixel to the other of the real position and repeat it with CCRepeat how many times you need or use CCRepeatForever and remove action by tag with CCActionManager method removeActionByTag:(int)tag target:(id)target when some event occur.