ヘイE:
私は学校のプロジェクトのためのJavaでゲームを作成しています。私は現在、プレイヤーのクラスを持っていると私は彼がその中に歩いていく床を作成しています。今、私はちょうど彼が「床」の上を歩くように、私は、画像に基づいて別のセクションを追加するクランプ法を作りました。結局、私は彼が上にジャンプできるようにする必要があるというプラットフォームを持っていますが、彼がミスならば落ちます。このコードは、現在動作していないと私はそれについて移動するかどうかはわかりません。床を行うには簡単にまたはより効率的な方法はありますか?そうでない場合、どのように私は、プラットフォームの作業を行うことができますか?
私はすでにそれがプラットフォームであるかないかどうかをテストする方法の終わりに変数を使用してみました。
package com.sewd.sophomorevisitation.objects;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.sewd.sophomorevisitation.Background;
import com.sewd.sophomorevisitation.KeyListener;
import com.sewd.sophomorevisitation.Main;
import com.sewd.sophomorevisitation.sprites.SpriteAnimation;
import com.sewd.sophomorevisitation.sprites.SpriteSheet;
public class Player extends GameObject {
Handler handler;
static int times = 0;
private static BufferedImage[] shreccRun = {SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(1, 0, 128, 106, 0, 0), SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(2, 0, 128, 106, 0, 0)};
private static BufferedImage[] shreccJump = {SpriteSheet.getSprite(3, 0, 128, 106, 0, 0), SpriteSheet.getSprite(4, 0, 128, 106, 0, 0)};
public static SpriteAnimation shreccRunAnimation = new SpriteAnimation(shreccRun, 5);
public static SpriteAnimation shreccJumpAnimation = new SpriteAnimation(shreccJump, 5);
public static SpriteAnimation animation = shreccRunAnimation;
public Player(int x, int y, ID id) {
super(x, y, id);
}
public void tick() {
y += velY;
if(x < 0) {
death();
}
if(Main.started) {
/*floor(1076, 1215, -500, Main.screenSize.width, 0, 972);
floor(1215, 1374, -500, Main.screenSize.width, 0, 969);
floor(1374, 1538, -500, Main.screenSize.width, 0, 936);
floor(1538, 1667, -500, Main.screenSize.width, 0, 925);
floor(1667, 3711, -500, Main.screenSize.width, 0, 919);*/
floor(0, 473, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 40);
floor(473, 600, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 70);
floor(600, 775, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 90);
floor(775, 2900, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 100);
floor(2900, 2980, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 140);
floor(2980, 3060, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 190);
floor(2980, 3060, -500, Main.screenSize.width, 0, 873);
floor(3060, 3073, -500, Main.screenSize.width, 0, 843);
floor(3073, 3827, -500, Main.screenSize.width, 0, 813);
floor(3827, 3857, -500, Main.screenSize.width, 0, 784);
floor(3827, 10000, -500, Main.screenSize.width, 0, 784);
}
if(animation != shreccJumpAnimation) {
animation.update();
}else {
if(times <= 5)animation.update();
times++;
}
}
public void death() {
System.out.println("Dead");
}
public static void floor(int start, int stop, int minX, int maxX, int minY, int maxY) {
if( start <= Background.x && Background.x < stop) Player.clamp(minX, maxX, minY, maxY);
}
public static void clamp(int minX, int maxX, int minY, int maxY) {
if(x <= minX) x = minX;
else if(x >= maxX) x = maxX;
else if(y <= minY) {
y = minY;
KeyListener.isInAir = true;
velY = -velY;
if(velY == 0) velY = 10;
}else if(y >= maxY) {
y = maxY;
KeyListener.isInAir = false;
if(times > 0) {
times = 0;
animation.stop();
animation.restart();
animation = shreccRunAnimation;
animation.start();
}
}else return;
}
public void render(Graphics g) {
g.drawImage(animation.getSprite(), x - shreccRun[0].getWidth() - 50, y, shreccRun[0].getWidth() * 2, shreccRun[0].getHeight() * 2, null);
}
}
床にはプラットフォームが存在しないすべての場所で正しく動作する必要があります。彼はちょうどそれの上を歩きます。彼はプラットフォームに着陸されている場合それ以外の場合、彼はそのプラットフォームが終了するまで、それに滞在する必要があり、その後、彼は落ちます。彼はその後、プラットフォームを逃した場合や、彼が下がります。
ヘイE:
理解した
public static boolean onFloor;
public void tick() {
if(Main.isStarted()) {
floor();
if(!onFloor) {
Player.setVelY(20);
}else {
Player.setVelY(0);
}
}
}
private void floor() {
clamp(0, 5000, 824, "f");
clamp(5000, 10000, 750, "p");
}
private void clamp(int start, int stop, int minY, String type) {
if(x > start && x < stop) {
if(Player.getY() != minY) {
if(Player.getY() > minY) {
if(type != "p") {
Player.setY(minY);
Player.setVelY(0);
onFloor = true;
return;
}
}
if(!Player.jumping) {
Player.setVelY(10);
onFloor = false;
}
}else if(Player.getY() == minY) {
Player.setY(minY);
Player.setVelY(0);
onFloor = true;
}else if(Player.getY() > minY) {
if(type != "p") {
Player.setY(minY);
Player.setVelY(0);
}
}
}
}