I am new to cocos2d-x and i am developing a game using cocos2d-x in xcode. In my game I added player sprite and Obstacle sprite. Obstacle sprite is moving continuously from right to left.
Now i am trying to add collision between the two sprites. So that I am using bounding box.Following is my code. but it is not working. this is first time that i am using bounding box.
if(obs1->boundingBox().intersectsRect(man->boundingBox()))
{
obs1->stopAllActions();
}
I created both the sprite in init() function and declare the above code in init function. Please help me to solve this.
you should be using if xxx->containsPoint()
Related
I am trying to create a simple platformer game in C++ with SDL, although am having trouble implementing animated sprites into the game. Linked is my git repo.
As you will see i'm currently having difficult trying to draw a single portion of the sprite sheet as the player sprite and looping through it to animate it. At the moment the entire sprite sheet that contains the player sprites is being squished into a 32 pixel rectangle. I have looked into SDL_RenderCopy although changing the variables that I pass to the method has no affect.
https://github.com/mountainfolks/Platformer_SDL
Have you stepped through your code to see which draw call you're hitting? AnimatedSprite inherits from Sprite, but you don't have Draw set as virtual in the base class, so you're actually calling Sprite::Draw. That would explain the behavior that points to the source rectangle being null.
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))
Se we are programming a game and in the center of the map there's area where the player cant go.
How we should create that area?
Game is top down perspective.
Assuming you are using some kind of collision detection that stops the player from going certain places (like off screen), all you have to do is to add an object that you can check for collisions with for the area where you don't want to player to go. Given that you haven't provided any specific information on how your game works this is the most specific answer you're likely to get.
Make an SDL_Rect that encompasses the entrance to the area, then check the collision of the SDL_Rect and the player
You will need some kind of collision detection as already mentioned above.
Then when moving the player, just check if the new position would collide with the rectangle in the middle of the screen.
Something like...
if( Input == Walk_Right)
{
//Move Player
MovePlayerRight();
//If Player collides with MiddleRect, move back
if (CheckCollison(&PlayerRect, &BoxRect) == true) MovePlayerLeft();
}
Check online for Collusion Detection. Simple Box Method should be enough to check if two Rectangles touch.
in cocos2d I need to drag a sprite in my project using finger. how do i get hold of that sprite? any help will be appreciated. thanx. i am using chipmunk engine.
You need to capture the touch began, work out what sprite is being touched and then track the movement of the touch. In touches moved you update the position of the sprite.
Sound complicated? It's not really. Here is a great tutorial for it.
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
In my game i am using tile map and sneaky joystick and I want to perform following task in my game.I am using box2d in my game.
I am create sprite body using box2d and also create static body for tile map.
My main problem is when my sprite moves that time tile map can not scroll.
Please help me.
Thanks
I am not sure what code you are using for your joystick but, Here's what I used when I made use of joystick for my game. Its very simple to use and easy to understand.
Hope that help you.. ! :)