Java: 在不影响已经发射的弹丸的情况下向不同方向发射弹丸
Java: Firing projectile in different direction without affecting projectile already sent
所以基本上,当我改变方向时,我射出的弹丸会跟随。问题是之前所有的弹丸也都改变了方向
这是我的 projectile.java :
package dev.codenmore.tilegame.gfx;
import java.awt.Graphics;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.entity.creatures.Player;
public class Projectile {
private double x;
private double y;
public Projectile (double x, double y, Handler handler){
this.x = x;
this.y = y;
}
public void tick(){
if ( Player.getPos() == 3)
x += 10;
if ( Player.getPos() == 2)
x -= 10;
if (Player.getPos() == 1)
y -= 10;
if (Player.getPos() == 0)
y += 10;
}
public void render(Graphics g){
g.drawImage(Assets.arrow, (int) x, (int) y, null);
}
}
这是我的controller.java:
package dev.codenmore.tilegame;
import java.awt.Graphics;
import java.util.LinkedList;
import dev.codenmore.tilegame.gfx.Projectile;
public class Controller {
private static LinkedList<Projectile> b = new LinkedList <Projectile>();
Projectile TempProjectile;
Handler handler;
public Controller(Handler handler){
this.handler = handler;
}
public void tick(){
for(int i = 0; i < b.size(); i++){
TempProjectile = b.get(i);
TempProjectile.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < b.size(); i++){
TempProjectile = b.get(i);
TempProjectile.render(g);
}
}
public static void addProjectile(Projectile block){
b.add(block);
}
public static void removeProjectile(Projectile block){
b.remove(block);
}
}
我的player.java:
package dev.codenmore.tilegame.entity.creatures;
import java.awt.Color;
import java.awt.Graphics;
import dev.codenmore.tilegame.Controller;
import dev.codenmore.tilegame.Game;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.gfx.Assets;
import dev.codenmore.tilegame.gfx.Projectile;
public class Player extends Creature {
public static int pos = 0;
public Player(Handler handler, float x, float y) {
super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, Creature.DEFAULT_CREATURE_HEIGHT);
bounds.x = 46;
bounds.y = 64;
bounds.width = 32;
bounds.height = 44;
}
@Override
public void tick() {
getInput();
move();
handler.getGameCamera().centerOnEntity(this);
}
private void getInput(){
xMove = 0;
yMove = 0;
if(handler.getKeyManager().up)
yMove = -speed;
if(handler.getKeyManager().down)
yMove = speed;
if(handler.getKeyManager().left)
xMove = -speed;
if(handler.getKeyManager().right)
xMove = speed;
if(handler.getKeyManager().shoot)
Controller.addProjectile(new Projectile((double)(x - handler.getGameCamera().getxOffset()), (double)(y - handler.getGameCamera().getyOffset()), handler));
}
public static int getPos(){
return pos;
}
@Override
public void render(Graphics g) {
if ( pos == 0)
g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 1)
g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 2)
g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 3)
g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if(handler.getKeyManager().up){
g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 1;
}else if(handler.getKeyManager().left){
g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 2;
}else if(handler.getKeyManager().right){
g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 3;
}else if(handler.getKeyManager().down){
g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 0;
}
}
}
我对编程还很陌生,对于这些错误我深表歉意。
这是发生了什么:http://gyazo.com/118b525d22e42a92fece77f70201cdad
问题所在:
public void tick(){
if ( Player.getPos() == 3)
x += 10;
if ( Player.getPos() == 2)
x -= 10;
if (Player.getPos() == 1)
y -= 10;
if (Player.getPos() == 0)
y += 10;
}
在每个tick中询问玩家的方向,方向可以改变,因此射弹也会改变。为避免这种情况,请像这样在开始时保存每个弹丸的位置:
private int pos;
public Projectile (double x, double y, Handler handler){
this.x = x;
this.y = y;
this.pos = Player.getPos();
}
然后打勾方法:
public void tick(){
if ( pos == 3)
x += 10;
if ( pos == 2)
x -= 10;
if ( pos == 1)
y -= 10;
if ( pos == 0)
y += 10;
}
切换会更好:
public void tick(){
switch(pos){
case 3: x+=10; break;
case 2: x-=10; break;
case 1: y-=10; break;
case 0: y+=10; break;
}
}
所以基本上,当我改变方向时,我射出的弹丸会跟随。问题是之前所有的弹丸也都改变了方向
这是我的 projectile.java :
package dev.codenmore.tilegame.gfx;
import java.awt.Graphics;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.entity.creatures.Player;
public class Projectile {
private double x;
private double y;
public Projectile (double x, double y, Handler handler){
this.x = x;
this.y = y;
}
public void tick(){
if ( Player.getPos() == 3)
x += 10;
if ( Player.getPos() == 2)
x -= 10;
if (Player.getPos() == 1)
y -= 10;
if (Player.getPos() == 0)
y += 10;
}
public void render(Graphics g){
g.drawImage(Assets.arrow, (int) x, (int) y, null);
}
}
这是我的controller.java:
package dev.codenmore.tilegame;
import java.awt.Graphics;
import java.util.LinkedList;
import dev.codenmore.tilegame.gfx.Projectile;
public class Controller {
private static LinkedList<Projectile> b = new LinkedList <Projectile>();
Projectile TempProjectile;
Handler handler;
public Controller(Handler handler){
this.handler = handler;
}
public void tick(){
for(int i = 0; i < b.size(); i++){
TempProjectile = b.get(i);
TempProjectile.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < b.size(); i++){
TempProjectile = b.get(i);
TempProjectile.render(g);
}
}
public static void addProjectile(Projectile block){
b.add(block);
}
public static void removeProjectile(Projectile block){
b.remove(block);
}
}
我的player.java:
package dev.codenmore.tilegame.entity.creatures;
import java.awt.Color;
import java.awt.Graphics;
import dev.codenmore.tilegame.Controller;
import dev.codenmore.tilegame.Game;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.gfx.Assets;
import dev.codenmore.tilegame.gfx.Projectile;
public class Player extends Creature {
public static int pos = 0;
public Player(Handler handler, float x, float y) {
super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, Creature.DEFAULT_CREATURE_HEIGHT);
bounds.x = 46;
bounds.y = 64;
bounds.width = 32;
bounds.height = 44;
}
@Override
public void tick() {
getInput();
move();
handler.getGameCamera().centerOnEntity(this);
}
private void getInput(){
xMove = 0;
yMove = 0;
if(handler.getKeyManager().up)
yMove = -speed;
if(handler.getKeyManager().down)
yMove = speed;
if(handler.getKeyManager().left)
xMove = -speed;
if(handler.getKeyManager().right)
xMove = speed;
if(handler.getKeyManager().shoot)
Controller.addProjectile(new Projectile((double)(x - handler.getGameCamera().getxOffset()), (double)(y - handler.getGameCamera().getyOffset()), handler));
}
public static int getPos(){
return pos;
}
@Override
public void render(Graphics g) {
if ( pos == 0)
g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 1)
g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 2)
g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if (pos == 3)
g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
if(handler.getKeyManager().up){
g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 1;
}else if(handler.getKeyManager().left){
g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 2;
}else if(handler.getKeyManager().right){
g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 3;
}else if(handler.getKeyManager().down){
g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
pos = 0;
}
}
}
我对编程还很陌生,对于这些错误我深表歉意。 这是发生了什么:http://gyazo.com/118b525d22e42a92fece77f70201cdad
问题所在:
public void tick(){
if ( Player.getPos() == 3)
x += 10;
if ( Player.getPos() == 2)
x -= 10;
if (Player.getPos() == 1)
y -= 10;
if (Player.getPos() == 0)
y += 10;
}
在每个tick中询问玩家的方向,方向可以改变,因此射弹也会改变。为避免这种情况,请像这样在开始时保存每个弹丸的位置:
private int pos;
public Projectile (double x, double y, Handler handler){
this.x = x;
this.y = y;
this.pos = Player.getPos();
}
然后打勾方法:
public void tick(){
if ( pos == 3)
x += 10;
if ( pos == 2)
x -= 10;
if ( pos == 1)
y -= 10;
if ( pos == 0)
y += 10;
}
切换会更好:
public void tick(){
switch(pos){
case 3: x+=10; break;
case 2: x-=10; break;
case 1: y-=10; break;
case 0: y+=10; break;
}
}