ユーチューブでニューボストンのチュートリアルを見ているので、今は「バッキーズランド」を作っています。このコードを使用すると、現在、キャラクターを左右、上下に動かすことができます。それは彼がどのように動いているかに応じてプレーヤーの側面を変えます。私はJavaの初心者で、キャラクターをジャンプさせる方法を知りたいです。どんな助けでもいただければ幸いです!
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight;
Image worldMap;
boolean quit = false;
int[] duration = {200,200};
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;
float shiftY = buckyPositionY + 160;
boolean jumping = false;
float verticalSpeed = 0.0f;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/world.png");
Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")};
Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.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;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);
bucky.draw(shiftX, shiftY);
g.drawString("Buckys X: "+buckyPositionX+"\nBuckys Y: "+buckyPositionY, 400, 20);
if(quit==true){
g.drawString("Resume (R)", 250, 100);
g.drawString("Main Menu (M)", 250, 150);
g.drawString("Quit Game (Q)", 250, 200);
if(quit==false){
g.clear();
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_UP)){
bucky = movingUp; //change bucky to up image
buckyPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
if(buckyPositionY>162){
buckyPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
}
}
if(input.isKeyDown(Input.KEY_DOWN)){
bucky = movingDown;
buckyPositionY -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
}
}
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
}
}
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
}
}
//when they hit escape
if(quit==true){
if(input.isKeyDown(Input.KEY_R)){
quit = false;
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);
try{
Thread.sleep(250);
}catch(InterruptedException e){
e.printStackTrace();
}
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);
}
}
}
public int getID(){
return 1;
}
}