所以我正在努力开发这个java 2d游戏。 youtube上的某个人正在这样做,我只是按照他的教程。问题是,当我尝试移动我的角色时,它会在屏幕上冲刺。我的setVelX值为-1,但它移动的次数要多得多。我已经尝试将输入移动速度提高到一倍,但如果它变为双倍,他就永远不会移动。
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
for(entity en: Game.handler.entity)
{
switch(key){
case KeyEvent.VK_W:
if (en.jumping==false)
{
en.jumping = true;
en.gravity = 10.0;
}
break;
case KeyEvent.VK_S:
break;
case KeyEvent.VK_A:
en.setVelX(-1);
break;
case KeyEvent.VK_D:
en.setVelX(1);
break;
}
}
主要关注A或D试图让速度减慢
public void setVelX(double velX) {
this.velX = velX;
}
public void tick() {
x+=velX;
while (running)
{
long now = System.nanoTime();
delta+=(now-lastTime)/1000000000;
lastTime = now;
while(delta >= 1)
{
tick();
ticks++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis()-timer>1000)
{
timer+=1000;
System.out.println(frames + " FPS " + ticks + " Updates/sec");
frames = 0;
ticks = 0;
}
}
stop();
}
我已经查看过这样的其他问题,但我找不到任何有用的问题。角色移动的次数远远超过1个空格,角色的动画也非常快。
答案 0 :(得分:2)
我认为你应该重新设置速度,因为当你每次移动时都会加速(25 FPS = 25次移动),我建议你只使用线程睡眠25 FPS,否则这个操作的每一帧都会完成后,这意味着一切都会发生得更快,因此看起来他的动作远不止于此。代码:
VelX=0;
并且,在void tick中:
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.EventQueue;
class Player {
public float health;
public float yvel;
public float xvel;
public float x;
public float y;
public BufferedImage image;
public void work(Graphics g, int[] keys) {
g.drawImage(image,(int)x,(int)y,null);
if (keys[0]==1) { //Is the UP-Key pressed
yvel=yvel-1;
}
if (keys[1]==1) { //Is the DOWN-Key pressed
yvel=yvel+1;
}
if (keys[2]==1) { //Is the LEFT-Key pressed
xvel=xvel-1;
}
if (keys[3]==1) { //Is the RIGHT-Key pressed
xvel=xvel+1;
}
x=x+xvel;
y=y+yvel;
xvel=xvel-(xvel/4); //One fourth gets lost
yvel=yvel-(yvel/4); //One fourth gets lost
}
public Player(BufferedImage imagew,float xw, float yw, float healthw) {
x=xw;
y=yw;
image=imagew;
health=healthw;
}
}
public class Game extends JFrame implements KeyListener {
public BufferedImage playerimage;
public Player player;
public int[] keys=new int[4];
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
keys[0]=1;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keys[1]=1;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keys[2]=1;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keys[3]=1;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
keys[0]=0;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keys[1]=0;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keys[2]=0;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keys[3]=0;
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
keys[0]=1;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
keys[1]=1;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
keys[2]=1;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
keys[3]=1;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
keys[4]=1;
}
}
public Game(){
super("Game");
try {
playerimage = ImageIO.read(getClass().getClassLoader().getResource("player.gif")); //Player Image
} catch(IOException bug) {
System.out.println(bug);
}
player=new Player(playerimage,100,100,100);
addKeyListener(this);
setTitle("Game");
setContentPane(new Pane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setResizable(false);
setVisible(true);
while (true){
repaint();
try {
Thread.sleep(33);
} catch(InterruptedException bug) {
Thread.currentThread().interrupt();
System.out.println(bug);
}
}
}
class Pane extends JPanel{
public Pane() {
}
public void paintComponent(Graphics g){
player.work(g,keys);
}
}
public static void main(String args[]){
new Game();
}
}
重置速度。
但是目前我正在开发相同类型的java游戏,所以我有一个“教程”:
db.test.distinct("test2")
这很好用,因为摩擦总是只会丢失四分之一,它会在屏幕上“滑动”。
以下是摩擦力图:速度梯度非常低。