Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

}

The Booleans are here

//building
boolean buildInit = false;
//
boolean supply = false;
boolean stopCarry = false;

Image: ` //build if(buildInit==true) { Build.draw(100,20); }

  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);
  }

`

The cord as you see, the map is moving, not my character!

float probePositionX = 0; //this is the pos for the maps
float probePositionY = 0;
float shiftX = probePositionX + 700;//this is the pos for my character
float shiftY = probePositionY + 400;

For stayX and stayY, I don't know the code for the building to stop following

These are experiments:

    float stayX = probePositionX + shiftX;
float stayY = probePositionY + shiftY;

I don't know which one should I write the code. And how do I make the Image stop following me? My opinion is shiftX and shiftY. I will add you to my credits in my game.

share|improve this question
3  
You should show some code. It's a coding question, it's will be hard to help without code. –  nathan Sep 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 green Sep 20 '12 at 8:40
add comment

1 Answer

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 maybe start reading the concepts of OOP or read the Wikipedia article to get used to the subject.

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.

public abstract class GameObject{
    public Vector2f position;
    public Image image;

    public GameObject(Image image){
        this.position = new Vector2f();
        this.image = image;
    }

    public void render(Graphics g, float xOffset, float yOffset)
    {
        if(this.image == null) return;
        g.drawImage(this.image, this.position.x + xOffset, this.position.y + yOffset);
    }

    public abstract void update(int d);        
}

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:

public class PlayerObject extends GameObject {
    public final static int STATE_NONE = -1;
    public final static int STATE_BUILDING_ROCK = 0;
    public final static int STATE_BUILDING_WOOD = 1;

    public final static int INDEX_ROCK = 0;
    public final static int INDEX_WOOD = 1;

    public final static float MOVE_SPEED = 0.1f;

    public int state;
    public Vector2f velocity;

    public Image[] objectImages;

    //Because we want to display objects, that can be built, we need to provide the sprites /
    //images of the objects.
    public PlayerObject(Image image, Image[] objectImages){
        super(image);
        this.velocity = new Vector2f();
    }

    @Override
    public void render(Graphics g, float xOffset, float yOffset)
         //First of all we call the render method of the base class
         //This calls the code defined in the GameObject.render method.
         super.render(g, xOffset, yOffset);

         //Now in the player implementation of the render method, we want to
         //know, if the player holds an object, that also should be rendered
         if(this.state == STATE_BUILDING_ROCK){
             g.drawImage(this.objectImages[INDEX_ROCK], this.position.x + xOffset, this.position.y + yOffset);
         }
         else if(this.state == STATE_BUILDING_WOOD){
             g.drawImage(this.objectImages[INDEX_WOOD], this.position.x + xOffset, this.position.y + yOffset);
         }
    }

    @Override
    public void update(int d){
        this.position.x += this.velocity.x * MOVE_SPEED;
        this.position.y += this.velocity.y * MOVE_SPEED;
    }
}

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:

if(container.getInput().isKeyPressed(Input.KEY_2)){
    player.state = PlayerObject.STATE_BUILDING_ROCK;
}

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.

public class StaticGameObject extends GameObject{
    public StaticGameObject(Image i){
        super(i);
    }
    @Override
    public void update(int d){
        //Does not do anything in update
    }
}

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:

public StaticObject build()
{
    Image i = null;
    if(this.state == STATE_BUILDING_ROCK){
        i = materials[INDEX_ROCK];
    }
    else if(this.state == STATE_BUILDING_WOOD){
        i = materials[INDEX_GRASS];
    }
    //If no image has been found for the material, the thing is not built.
    if(i == null) return null;
    StaticObject obj = new StaticObject(i);
    obj.position.set(this.position);
    //Reset the state
    this.state = MODE_NONE;
    return obj;
}

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:

public class TestGame extends BasicGame
{
    private Image playerImage;
    private Image[] materials;
    private PlayerObject player;
    private List<GameObject> objects;

    public TestGame(){ 
        super("Test Game"); 
        objects = new ArrayList<GameObject>();
    }

    [...]
    public void init(GameContainer container){
        playerImage = new Image("PlayerImage.png");
        materials = new Image[2];
        materials[0] = new Image("Rock.png");
        materials[1] = new Image("Wood.png");

        player = new PlayerObject(playerImage, materials);
        player.position.set(100, 100);
        objects.add(player);
        [...]
    } 

    public void update(GameContainer container, int d){
        //First do input handling and update the player objects state, if needed
        Input i = container.getInput();
        if(i.isKeyPressed(Input.KEY_1)){
            player.state = PlayerObject.STATE_BUILDING_ROCK;
        }
        if(i.isKeyPressed(Input.KEY_2)){
            player.state = PlayerObject.STATE_BUILDING_WOOD;
        }

        if(i.isKeyPressed(Input.KEY_ENTER) )
        {
            //If the build key is pressed, we try to build with the player
            GameObject obj = player.build();
            //If an object could have been built, it is added to the global game object list
            if(obj != null){
                objects.add(obj);
            }
        }
        //Now update all objects
        for(GameObject object : objects){
            object.update(d);
        }
    } 

    public void render(Graphics g, GameContainer container, int d){
        for(GameObject object : objects){
            //The x and y offset parameters are used, when the map scrolls
            object.render(d, 0, 0);
        }
    } 
}

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.