Ich habe kürzlich ein bisschen Code in meinem Spieler entwickelt, wodurch der Spieler auf der Höhe der Zeit, in der die Jump -Taste abgehalten wird, auf einer Höhe springen lässt. Wenn Sie jedoch auf dem Boden landen, schüttelt die Kamera ein wenig. Wie kann ich das beheben?package com.catastrophe.entity;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import com.catastrophe.graphics.Animation;
import com.catastrophe.graphics.AnimationLibrary;
import com.catastrophe.graphics.Camera;
import com.catastrophe.save.PlayerSave;
import com.catastrophe.world.Level;
import com.catastrophe.world.Tile;
public class Player extends Entity {
public static final int FURBEE = 0;
public static final int MAC = 1;
public static final int PIXIE = 2;
public float speed, health;
public PlayerSave ps;
public boolean left, right, jump;
public Animation leftAnimation, rightAnimation;
public float speedX, speedY;
public int jumpHeight;
public int jumpTimer;
public float gravity;
public int player;
private boolean grounded;
private int coyoteTime;
private int inputBuffer;
public Animation current;
private boolean firstJumpFrame;
public Player(int player, float x, float y, int width, int height, float health, float speed, int jumpHeight, float gravity, PlayerSave ps) {
super(x, y, width, height, health);
this.speed = speed;
this.height = height;
this.ps = ps;
speedX = 0;
speedY = 0;
jumpTimer = 0;
this.player = player;
this.jumpHeight = jumpHeight;
this.gravity = gravity;
this.grounded = false;
this.inputBuffer = 0;
switch(player) {
case FURBEE:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_FORWARD, 10);
break;
case MAC:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_FORWARD, 10);
break;
}
leftAnimation.setDimension(width,height);
rightAnimation.setDimension(width,height);
current = leftAnimation;
current.setDimension(width,height);
current.setPosition(x, y);
}
public void tick(Level lvl) {
current.unpause();
if (coyoteTime > 0) {
coyoteTime--;
}
if (inputBuffer > 0) {
inputBuffer--;
}
boolean canJump = (grounded || coyoteTime > 0) && jumpTimer == 0;
if (jump) {
inputBuffer = 10;
firstJumpFrame = true;
}else {
jumpTimer = 0;
}
if (inputBuffer > 0 && canJump) {
jumpTimer = jumpHeight;
speedY = -gravity;
if(firstJumpFrame) {
this.y -= 10;
firstJumpFrame = false;
}
grounded = false;
coyoteTime = 0;
inputBuffer = 0;
}
if (jumpTimer > 0) {
jumpTimer--;
} else {
speedY = gravity;
}
if (left) {
speedX = -speed;
current = leftAnimation;
} else if (right) {
current = rightAnimation;
speedX = speed;
} else {
speedX = 0;
current.imageIndex = 0;
current.pause();
}
x += speedX;
Rectangle2D.Double bounds = new Rectangle2D.Double(x, y, 120, 60);
for (Tile tile : lvl.tiles) {
if (bounds.intersects(tile.x, tile.y, 60, 60) && tile.collision && !tile.semisolid) {
if (speedX > 0) {
x = tile.x - 120;
} else if (speedX < 0) {
x = tile.x + 60;
}
speedX = 0;
}
}
boolean wasGrounded = grounded;
grounded = false;
y += speedY;
bounds = new Rectangle2D.Double(x, y, 120, 60);
for (Tile tile : lvl.tiles) {
if (bounds.intersects(tile.x, tile.y, 60, 60) && tile.collision) {
if (speedY > 0) {
y = tile.y - 60;
grounded = true;
speedY = 0;
} else if (speedY < 0 && !tile.semisolid) {
y = tile.y + 60;
speedY = 0;
}
}
}
if (!wasGrounded && grounded) {
coyoteTime = 10;
}
current.tick();
}
public void render(Graphics2D g, Camera camera) {
leftAnimation.setPosition(x, y);
rightAnimation.setPosition(x, y);
current.setPosition(x - camera.x, y - camera.y);
current.render(g);
}
public void switchPlayer(int player) {
this.player = player;
switch(player) {
case FURBEE:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_FORWARD, 10);
break;
case MAC:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_FORWARD, 10);
break;
}
leftAnimation.setDimension(width,height);
rightAnimation.setDimension(width,height);
current = rightAnimation;
}
}
< /code>
Wenn Sie mehr von meinem Code benötigen, lassen Sie es mich wissen. Wenn sie die Sprungtaste drücken.
Wie kann ich den Spieler dazu bringen, auf einer Höhe zu springen, je nachdem, wie lange er den Sprungknopf drückt, ohne ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post