Недавно я разработал немного кода в моем классе Player , который заставляет игрока прыгать на высоте, основываясь на количестве времени, когда кнопка Jump удерживается. Однако иногда приземление на земле заставляет камеру немного встряхнуть. Как это исправить? < /P>
Вот код класса моего игрока: < /p>
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>
Если вам нужно больше моего кода, дайте мне знать. Когда они нажимают кнопку прыжка.
Подробнее здесь: https://stackoverflow.com/questions/794 ... s-the-jump
Как я могу заставить игрока прыгать на высоте, основываясь на том, как долго они нажимают кнопку прыжка, не мешая входно ⇐ JAVA
Программисты JAVA общаются здесь
1738710888
Anonymous
Недавно я разработал немного кода в моем классе Player , который заставляет игрока прыгать на высоте, основываясь на количестве времени, когда кнопка Jump удерживается. Однако иногда приземление на земле заставляет камеру немного встряхнуть. Как это исправить? < /P>
Вот код класса моего игрока: < /p>
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>
Если вам нужно больше моего кода, дайте мне знать. Когда они нажимают кнопку прыжка.
Подробнее здесь: [url]https://stackoverflow.com/questions/79413262/how-can-i-make-the-player-jump-at-a-height-based-on-how-long-they-press-the-jump[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия