画面に2つの長方形があり、一方の長方形は移動し、もう一方は静止している単純なJavaゲームをコーディングしました。移動する長方形は、キーボードの矢印入力で移動し、上下左右に移動できます。私が抱えている問題は、両方の長方形が移動しているのに、一方だけが(rectOne)、rectTwoが静止している必要があるということです。変数は、次のように設定されています。
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
//my two rectangles are shown bellow
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
座標変数buckyPositionXとbuckyPositionYは0,0なので、画面の左2隅を表示し、座標変数shiftXとshiftYは画面の中央の座標を表示するため、移動する長方形は常に中央に配置されます。
すべてのゲームコードを含むPlayクラスは、いくつかのメソッドで構成されています。重要なメソッドは更新とレンダリングです。更新では、長方形の移動速度とキーボード入力を処理して、内部にあるものの例を示します。クラスを更新します。これは、上キーが押されたときのコードのサンプルです。
if(input.isKeyDown(Input.KEY_UP)){buckyPositionY += 2;}
これは、すべてのキーボード矢印入力に対して行われます。次は、画面とグラフィックスにすべてを描画するレンダリングメソッドです。このメソッドには、以前に変数でコーディングされた長方形を画面に描画することが含まれます。
ですから、キーボード入力で長方形を動かそうとすると、画面上に2つの長方形が描かれ、両方の長方形が同時に移動します。長方形の座標を設定して遊んでみると、問題があると思います。たとえば、+buckyPositionXと+buckyPositionYを削除しますが、同じ問題が発生します。この問題を解決できるアイデアがあれば、教えてください。
前もって感謝します。
編集1:
リクエストに応じて、プレイクラスの完全なコードを次に示します。
imports are here
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight;
Image worldMap;
int[] duration = {200, 200};//how long frame stays up for
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/world.png");
Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};
movingUp = new Animation(walkUp, duration, false);
movingDown = new Animation(walkDown, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
bucky = movingDown;//facing screen initially on startup
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
bucky.draw(shiftX, shiftY);//makes him appear at center of map
g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
//up
if(input.isKeyDown(Input.KEY_UP)){
bucky = movingUp;//changes the image to his back
buckyPositionY += 2;;//increase the Y coordinates of bucky (move him up)
if(buckyPositionY>162){//if I reach the top
buckyPositionY -= 2;//stops any further movement in that direction
}
}
//down
if(input.isKeyDown(Input.KEY_DOWN)){
bucky = movingDown;
buckyPositionY -= 2;
if(buckyPositionY<-550){
buckyPositionY += 2;//basically change the direction if + make -
}}
//left
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += 2;
if(buckyPositionX>324){
buckyPositionX -= 2;//delta * .1f
}}
//right
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= 2;
if(buckyPositionX<-776){
buckyPositionX += 2;
}}}
public int getID(){
return 1;
}}
質問への回答:
rectTwo変数で変数buckyPositionXとbuckyPositionYを使用した理由は、g.fillRect関数を使用してこれを描画しようとすると、これらの入力ですべてが正常に機能し、rectOneが移動し、RectTwoは静止したままですが、 g.fillRect(rectOne.X、rectOne.Y ...)と言うことで変数この問題が発生します。両方の長方形が一緒に移動している場合、座標とサイズがgを使用して描画した場合とまったく同じであるため奇妙です。 fillRect(500 + buckyPositionX、330 + buckyPositionY、210、150)およびrectOneについても同じです。私はいくつかのコードでそれをより明確にしようとします:
これを変数で使用する場合:
java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
そしてこれは私のrenderメソッドで:
g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
長方形の両方、rectOneとrectTwoは一緒に移動します。
しかし、変数を設定せずにレンダリングメソッドでこれを使用すると、次のようになります。
g.fillRect(shiftX, shiftY,90,90)
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150)
私の衝突はこの方法では機能しませんが(それが私が修正したい理由です)、すべてが視覚的に機能します。長方形が画面に表示され、rectTwoが静止している間にrectOneが移動します。
これは混雑しすぎて話題から外れ始めたので、私の問題をより詳細に説明する新しい投稿を作成します。