When should I change the parent of a CCNode in cocos2d? - cocos2d-iphone

I have a CCLayoutBox at the bottom of a scene which is the parent for several CCSprites. These sprites represent the game tokens that the player can play on the board.
On touchBegan where the player has touched a game token, I start updating the position of the the token relative to the CCLayoutBox as the touchMoved event fires.
The question I have is whether this is the correct thing to do? Should I immediately remove the CCSprite from the CCLayoutBox when the player "picks up" the token? Should I re-parent the node to the board when the player drops the token in a valid location?

It's not wrong to use using the CCLayoutBox as your position reference but it depends on how you use your board and CCLayoutBox.
If your board is static there is no problem at all, but if you need to move along the game to another point of the board for example you're gonna have some problems with the tokens already positioned in the board.
If the player can move along the board, you can remove the token from the CCLayoutBox when the player drops the token in the board and add a new token with the board as parent. This will help since the tokens in the board will move along.
Hope it helps :)

Related

How to set up a score board using SFML and Box2D in C++

I want to set up a scoreboard for these two players (Pikachu)
My questions are:
how to detect when the ball touches the ground?
how to setup an object that can change during the game?
like the two in this picture:
I'm very new to SFML and Box2D, so even if I try to read the source code to see what I can use to implement things above, I still have no idea.
Can anyone give me some clues?
To create a scoreboard similar to the one in the image provided you can put text on the screen that holds the current score of the players and then placing it in the position where you want the score to be placed on the screen. In SFML this would be done by creating a sf::Text object.
To answer your other questions.
1.how to detect when the ball touches the ground
A simple way of doing this would be to check the Y value of the coordinates of the ball and from that determining if the ball is touching the ground.
2.how to setup an object that it can change during the game.
I am unsure what you mean by this.

Pygame: Character Centered Movement System (Diablo II like click-to-move)

I am currently working on a new RPG game using Pygame (my aim here is really to learn how to use object oriented programming). I started a few days ago and developed a movement system where the player clicks a location and the character sprite goes to that location and stops when it get's there by checking if the sprite 'collides' with the mouse position.
I quickly found however that this greatly limited the world size (to the app window size).
I started having a look into making a movement system where the background would move with respect to the player, hence providing the illusion of movement.
I managed to achieve this by creating a variable keeping track of my background map position. The map is much bigger than the app window. And each time I want my player to move I offset the background by the speed of the player in the opposite direction.
My next problem now is that I can't get my character to stop moving... because the character sprite never actually reaches the last position clicked by the mouse, since it is the background that is moving, not the character sprite.
I was thinking of spending some time coding in a variable that would keep track of how many displacements it would take the character sprite to reach the mouse clicked position if it was to move. Since the background moves at the character sprite's speed it would take as many displacement of the background in the x and y directions to center the clicked position on the background to the character sprite at the center of the screen.
It would be something like that:
If MOUSEBUTTON clicked:
NM = set number of moves needed to reach the clicked position based on character sprite distance to click and character sprite speed.
If NM != 0:
Move background image
Else:
pass
This would mean that when my background has moved enough for the character sprite to now be just over the area of the background that was originally clicked by the player, the movement would stop since NM == 0.
I guess that my question is: Does that sound like a good idea or will it be a nightmare to handle the movement of other sprites and collisions ? And are there better tools in Pygame to achieve this movement system ?
I could also maybe use a clock and work out how many seconds the movements would take.
I guess that ultimately the whole challenge is dealing with a fixed reference point and make everything move around it, both with respect to this fixed reference, but also to their own. e.g. If two other sprites move toward one another, and the character of the player also "moves" then the movement of the other two sprites will have to depend both on the position of the other sprite and also on the offset of the background caused by the movement of the player's character.
An interesting topic which has been frying my brain for a few nights !
Thank you for your suggestions !
You actually asking for an opinion on game design. The way I look at it, nothing is impossible so go ahead and try your coding. Also it would be wise to look around at similar projects scattered around the net. You may be able to pick up a lot of tips without re inventing the wheel. Here is a good place to start.
scrolling mini map

How to create a b2body using box2d that is continuously moving left and right and doesn't fall down when player jumps on to it?

I am working on a platformer game using cocos2d with box2d in which i have ground body that must continuously move to left and right, but doesn't fall when player jumps onto it. I am not able to do so, my body falls when player jump onto it. Provide me some link or code to do so.
I think the best solution is to move the sprite for the platform, not the platform itsef.
Instead of creating a small platform for the jumping, create a rectangle that extends across the entire pit.
Only move the sprite over the region that you want the player to jump on to (i.e. move it left and right so the player knows the "safe" area to jump).
Use the b2ContactListener to detect the collision between the rectangle and the player. If they are in contact and the player is over the moving sprite, then don't do anything different.
If they are in contact and the player is NOT over the moving sprite, disable the collisions response in the PreSolve event of the b2Contact listener and for the entire time the player is in contact with it. The player should fall through to the pit below.
You could also use a sensor body for the moving platform to give you a better "contact mechanism" than just the sprite. So if the player is in contact with the "platform sensor" and the "rectangle across the pit", don't do anything in the PreSolve event. Otherwise, if they are only in contact with the "rectangle across the pit", let them drop through it.
Does this work?

Is the traditional update-render-loop method overkill for cocos2d/cocos2dx?

So lets say i'm making a board game. i've got a game board array, my logic needs to check for position and collision detection, thats all fine.
Traditionally using something like directX you would have a game loop, test some logic, update the game board array and finally draw the screen,
but with cocos2dx we don't directly draw to the screen we add sprite to layers, and cocos does the rest!
For instance..
Initialise a game array that represents the game board
Initialise a game object
add objects to the array
update the screen (render the array to the screen)
start game loop
Test for some logic condition
remove object from array
more Test for some logic condition, update object position
add objects to the array
update the screen again
loop
The previous would work fine but i don't need to remove the object from the layer only to update its position and re-render to the screen,
with cocos2d/cocos2dx if i keep a reference to that object i could just move it, and update the array.
I can continue with this approach, but i want to know if i'm missing something.
The game array helps with the game logic and mimics the actual playable area, but i can't help feeling its a bit stone-age.
Can anybody help with this, am i completely missing the plot?
All game engines use an update loop. Without it, you wouldn't be rendering anything.
That said, if your game is entirely user-input driven, you can react to user input events (touches) and only then perform game logic (moving or removing pieces, checking win conditions, update score, next turn).
If you want "new-school" then you'll be looking at a game design tool, not a game engine.
This looks fine to me. The thing to keep in mind is during your game loop you will be able to handle animations that keep the board game interesting. I think that is not overkill, you can always find things to animate.

Board Game using SDL

I am building a board game in SDL and here is the problem I am currently facing.
I have a pawn on square 1 and I roll the dice. Based on the value I get on the dice the pawn moves to another square. I am bale to move the pawn after i read the SDL tutorials online. But the problem I am facing is that after moving the pawn to a new location the old pawn still stays at the old location. The tutorials I found on the internet moves a dot but also refreshes the background to cover up the old dot. But I cant do that as my game board is intricate and there are pawns from other players sitting there.
Is there a way in SDL that I could really move a pawn instead of having to create a new pawn at the new location and covering up the old pawn?
The fundamental concept of sprites: Before you insert the sprite, you save a copy of the original screen content. When you need to remove the sprite, you just paste the stored old content back in.
You will have to process all your objects in the correct order (LIFO) for this to work. Since you'll usually be double-buffered, this happens on the cold buffer, so this isn't an issue.
No, your code will need to be able to redraw that board position with the pawn missing. There isn't any way for the computer to automatically reconstruct what the board should look like without the pawn.
It sounds like your render code is mixed in with your game logic. You ought to separate rendering so that you can redraw the complete game scene with a single function call, which you can then use whenever a visible change is made to the game state.