I wanted to have a build functions. When I press for example 2, the image will appear with my character.

The question is, how do I stop it from following my character

//build
//I created many comments cuz this part would be confusing!
//buildInit if its initiated it would draw
if(buildInit==false || buildInit==true)
{
//contol ro make it on
if(input.isKeyDown(Input.KEY_B))
{//buildInit is to enable the build inventory (The player can choose)
buildInit=true;
//What buildings
}
if(buildInit==true)
{
if(input.isKeyDown(Input.KEY_1))
{
supply=true;
}
}
//Put it down!!!
if(supply==true)
{
//this would stop it from following and stop there FOREVER
if(input.isKeyDown(Input.KEY_SPACE))
{
stopCarry=true;
}
}
}
//TO stop carry
if(stopCarry==true)
{
//I don't know how to do this part. I tried to do many ways but I couldn't get //it!
}

if(supply==true)
{//shiftX is my character also shiftY
SupplyImg.draw(shiftX, shiftY);
}
if(stopCarry==true)
{//stayX and stayY is the place for the building to stay. I don't know how to do this
SupplyImg.draw(stayX, stayY);
}

You should show some code. It's a coding question, it's will be hard to help without code.
–
nathanSep 19 '12 at 17:21

Maybe also try to explain it a bit better, because now it's not really clear what you want to do. I tried to answer the question, but I wasn't sure, if I even was answering what you wanted to ask. Maybe you could create some images to show what you try to achieve, or show an example from another game, so we know we're on the same page.
–
tom van greenSep 20 '12 at 8:40

1 Answer
1

TLDR Version:
Don't store position value and states of objects directly in the game / state class. Create classes for game objects and only store, update and render those objects. When you press you build key, simply remove the building material from the player and create a new game object on the respective position.

Long Version:

Ok, from looking at your code I think you have to learn a bit more about object oriented design and how to apply it for problems as yours. Maybe I drift a bit offtopic concerning your question. If so sorry about that, but I think you should get the idea, how this can help you.

In your code, you have a variable that tracks, if you are building something. Now imagine, you want to have 10 other building options, then you would need 10 variables to track the state. Try to grasp the concept of classes and objects.

So, back to your question. First of all I would think of what classes you will have in your game. Assuming from your question I would start by implementing a class GameObject. This class holds some basic information, that are used for all game objects. I put a vector in the class, that represents the position of the object, and an image, that is the graphical representation of that object.

Maybe you noticed that the class is marked abstract. This means, that this class is not meant to be instanciated, but other classes need to extend from it. It already implements a basic render methodThe method update is defined here in the base class without any implementation.

So now lets look on how to implement another game object. One thing you will need is the player class and this could look something like this:

If you look at the code, you can see that two things were implemented. The update method changes the position of the GameObject according to the defined velocity. And the render method first simply calls the render method of the base class and then checks, if it has to draw the building material.

Some comments to this. The entities do not manage their state themselves (besides the position), but you would handle all game objects in the game loop and depending on the state you would change the fields of the entity, for example like this:

With this, you would have the display functionality capsuled in the GameObject and extend that functionality in the PlayerObject class by updating the players position and rendering the building materials, if the player is currently in a building state.

Now to the second part, building the object and leaving it there. To accomplish this, I would create another class StaticObject or something like that.

To make things a bit more interesting, we add the logic to spawn a static game object to the player object (this is maybe not the best solution, but it should do the work). So we add this code to the PlayerObject class:

Now, when the player presses a key to build the object, you have to do 2 things. First set the players state back to MODE_NONE, because he completed the action. As second, you create a StaticObject with the building material and add it to your GameObject list. This object is now independent from your player and stays at its place, even if you move around.

A short abstract, how (parts) of my Game or GameState class would look like, using the examples from above:

Keep in mind, it is not the ultimately best solution and I scribbled it down without any IDE, so no warranties that the code actually works ;)

Sooo, I hope it isn't too much. As you can see, we try to capsule functionality to the respective actors. Also, like this you can implement very easily computer controlled characters, that also can use the same build mechanics (just instead of receiving its state from the keyboard input, an AI component would change the state). And also, you can add 10 objects, and you don't have to create all the boolean and position variables for all of them, but only an GameObject per object.