0

I am generally new to both Java and Slick2D, and even after browsing Google, I couldn't find the answer to my question. In my game I have a character that can currently run back and forth, and jump. I want him to be able to shoot projectiles, but the way I have it coded, the projectile changes trajectory when he switches direction or jumps.

if(input.isKeyPressed(Input.KEY_SPACE)){    
        shotX = charPosX;
        shotY = charPosY;
    if(left){
            shotX += delta * -0.5f;             
        }else{
            shotX +=delta * 0.5f;
        }

}

This is basically the code I currently have. The "left" boolean simply states whether the character is facing left or right. I feel like I'm missing something obvious, but I was wondering if anyone had a simple solution to this, perhaps a way to stop setting the shot's position to the the character's position after it is rendered. Any help is appreciated.

  • What types are shotX and charPosX? I expect this is down to assigning references rather than values, but if you expand on the information a little I should be able to help. – DeejUK Jun 27 '12 at 07:57
  • All of my position variables are floats. Also, that sounds like it's what I'm looking for. Could you elaborate on how I would assign a reference rather than a value? – John Smith Jun 28 '12 at 17:24
  • floats or Floats, with a big F? – DeejUK Jun 29 '12 at 15:10
  • Traditional internet-hobby-game-developing wisdom says that your projectile entities and the player entity should be separate objects. Would you mind posting your full code if you haven't figured this out already? – spirulence Jul 24 '12 at 22:58

1 Answers1

0

As you have been told in the comments, it is difficult to determine the situation from such little code, but what I believe the problem is is that while the space bar is held, the if clause you have gets executed over and over, setting the bullet's position to that of the character.

What would probably be better is to have a boolean value in the bullet class, indicating whether it has been fired, and then updating the location every update period by adding the delta times speed to the values. The situation you are describing seems to have only one bullet that is reset every time the space bar is held.

Also, the bullet can only move while the space bar is held, because the movement code is in the if statement. I hope this helps.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Kilo9
  • 26
  • 2