Hello all. My problem at this time is trying to create a firing system so that a new bullet is created while the button is down. I currently have one bullet rendering and moving (though not continuous movement from one press). So I wanted to have it so that each time the update reads the key pressed, it creates a new bullet.
Code:
@Override
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
gc.setTargetFrameRate(60);
screen = new Image("res/gamescreen.png");
player = new Entity("player");
player.AddComponent( new ImageRenderComponent("PlayerRender", new Image("res/player.png")) );
player.setPosition(new Vector2f(300, 176));
bullet = new Entity("bullet");
bullet.AddComponent(new ImageRenderComponent("BulletRender", new Image("res/bullet.png")));
bullet.setPosition(new Vector2f(315, 197));
bullet.AddComponent(new BulletFiringComponent("BulletFire"));
}
Here I init the player and the bullet. I'm perplexed by having the bullet entity being stored in an arraylist, then having a new one created that works correctly. I'll also post my component that handles the bullet firing motion.
Code:
package entities;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
public class BulletFiringComponent extends Component {
float direction;
ArrayList<Entity> bullets;
public BulletFiringComponent (String id) {
this.id = id;
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) {
Vector2f position = owner.getPosition();
Vector2f start = new Vector2f(315, 197);
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_LEFT)) {
position.x -= 6f;
} else if (input.isKeyDown(Input.KEY_RIGHT)) {
position.x += 6f;
}
if (input.isKeyPressed(Input.KEY_B)) {
owner.setPosition(start);
}
}
}
If anyone can explain what I need to do I would appreciate it greatly.
