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.

How do I draw a sprite right at my Player sprite's position when I pressed Spacebar?

protected void playerInput() {
        KeyboardState key = Keyboard.GetState();

        if (key.IsKeyDown(Keys.Escape)) this.Exit();
        if (key.IsKeyDown(Keys.Up))     playerPos.Y -= 5;
        if (key.IsKeyDown(Keys.Down))   playerPos.Y += 5;
        if (key.IsKeyDown(Keys.Left))   playerPos.X -= 5;
        if (key.IsKeyDown(Keys.Right))  playerPos.X += 5;
        if (key.IsKeyDown(Keys.Space))  bSpawnSandbag = true;
    }

protected override void Update(GameTime gameTime) {
        playerInput();
        base.Update(gameTime);
    }

 protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(player, playerPos, Color.White);

        if (bSpawnSandbag) {
           spriteBatch.Draw(sandbag, new Vector2(playerPos.X + 3, playerPos.Y + 3), Color.White);               
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

If I use the code above, the "sandbag / the sprite I want to draw" will move with my Player position and I don't want that. I want it to draw at THAT specific position my Player was once at when I press Spacebar for 1 time. Any clue? O_O I want it to be...

Let say, player position = (10, 10) Player pressed Spacebar, draw Sandbag at 10,10 (permanently) Even though Player moved to 12, 10 & not pressing Spacebar , the Sandbag would still be at 10,10

share|improve this question

closed as too localized by Byte56, bummzack, Josh Petrie, Trevor Powell, Anko Apr 7 '13 at 16:31

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 6 down vote accepted

Think for a moment what happens if bSpawnSandbag is true. It will always draw the sandbag. However the only input you are using is the playerPos.X/Y for where to draw it. Of course it's going to draw it at the players position. It's continues and not a one time thing. Code isn't called once unless your structure is specified/setup as such. As long the bSpawnSandbag is true, sandbag will be drawn.

I don't know what properties your sprites has. Simply keep track of the position when space is hit

Vector2 bagPos = new Vector2();

protected void playerInput() {
        KeyboardState key = Keyboard.GetState();

        if (key.IsKeyDown(Keys.Escape)) this.Exit();
        if (key.IsKeyDown(Keys.Up))     playerPos.Y -= 5;
        if (key.IsKeyDown(Keys.Down))   playerPos.Y += 5;
        if (key.IsKeyDown(Keys.Left))   playerPos.X -= 5;
        if (key.IsKeyDown(Keys.Right))  playerPos.X += 5;

        //If bSpawnSandbag is not true AND spacebar is down
        if (key.IsKeyDown(Keys.Space) && !bSpawnSandbag)  {

         bSpawnSandbag = true;
         bagPos.X = playerPos.X;
         bagPos.Y = playerPos.Y;

         }
    }

protected override void Update(GameTime gameTime) {
        playerInput();
        base.Update(gameTime);
    }

 protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(player, playerPos, Color.White);



        if (bSpawnSandbag) {

           spriteBatch.Draw(sandbag, bagPos, Color.White);               
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

Here is a link that might be handy for you. Assuming you are a beginner.

Edit:

As for your question in your comment...

( Not tested and it's just to give you an idea )

Let's assume we are using our own created Sprite class. It contains a Texture2D you need to pass through the constructor and a position vector. Which will be both used in the draw function.

public class Sprite(){
public Vector2D position{get;set;}
private Texture2D texture;

public Sprite(Texture2D t, Vector2D pos)
{
  position = pos;
      texture = t;
 }

public Sprite(Texture2D t)
{
    texture = t;
}

public draw(SpriteBatch sb)
{
   sb.draw(texture,position);
}


}  

Now lets say we have a class that manages our sprites somewhere:

//Our list that holds sprite objects
List<Sprite> spriteList = new List<Sprite>();

//Draw all sprites in our list
private void drawSprites(){
  for(int i = 0; i < spriteList.Count; i++)
    //Pass our SpriteBatch to our draw function in Sprite
    spriteList[i].draw(spriteBatch);
}

//Create new sandbags
private void createSandBag(Vector2D atPos){
   Sprite sandbag = new Sprite(MY_STATIC_TEXTURE);
  sandbag = atPos;
  spriteList.add(sandbag);
}

you then call createSandBag when Space bar is hit and pass the position of your player

share|improve this answer
    
Yes I know, that's why I asked "when I press Spacebar for 1 time"... I apologize if my question is not clear enough , English is not my native language... Basically, for the Spacebar that part, I want it to be, Let say, player position = (10, 10) Player pressed Spacebar, draw Sandbag at 10,10 (permanently) Even though Player moved to 12, 10 & not pressing Spacebar , the Sandbag would still be at 10,10 –  Xeon Apr 3 '13 at 16:42
    
@Xeon did you look at the code ? I gave you the solution –  Sidar Apr 3 '13 at 16:44
    
Erm, okay...But if I want to change it to be able to spawn multiple sandbags...Any solution ? –  Xeon Apr 3 '13 at 16:50
    
You are asking for pretty trivial/basic stuff. You should have a function which creates your sandbags and adds them to a list/array and you simply iterate through them with a forloop. Somewhere where you organize your game objects. –  Sidar Apr 3 '13 at 17:06
    
@Xeon added some code –  Sidar Apr 3 '13 at 17:20

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