Greedings its been a while and i’ve been working on Java programming again and i had some questions that i can’t really make out how do it. So lets begin :
Here is the GetInput() code where you check the keys and depending on what you press you set the yMove or xMove equal to speed so the player will move. Although if i set the yMove and xMove to 0 where there is collision then it never moves again. Any solution?
public void getInput() {
xMove = 0;
yMove = 0;
if (game.getKeys().up) {
yMove = -speed;
}
if (game.getKeys().down) {
yMove = speed;
}
if (game.getKeys().left) {
xMove = -speed;
}
if (game.getKeys().right) {
xMove = speed;
}
if (x < 0) {
x = 0;
}
if (x > 250) {
x = 250;
}
if (y < 0) {
y = 0;
}
if (y > 250) {
y = 250;
}
rectup.setBounds((int) x, (int) y, (int) width, (int) height);
rectdown.setBounds((int) x, (int) y, (int) width, (int) height);
rectleft.setBounds((int) x, (int) y, (int) width, (int) height);
rectright.setBounds((int) x, (int) y, (int) width, (int) height);
collision();
}
Here is the Collision Class where i check for the distance of the player and the enemies. The collision works perfectly but i don’t know how to tell the Player Entity to stop moving.
public void collision() {
if (isColliding()) {
lastX = getX();
LastY = getY();
}
if (enemies.size() != 0) {
for (int i = 0; i < enemies.size(); i++) {
double diffX = getX() - enemies.get(i).getX();
double diffY = getY() - enemies.get(i).getY();
double distanceToPlayer = Math.sqrt((diffX * diffX)
+ (diffY * diffY));
if (distanceToPlayer < 44) {
x = lastX;
y = LastY;
}
}
}
}
Thanks for any help