Boa tarde, conseguem me auxiliar, sou bem iniciante e não estou encontrando o porque do player não realizar o movimento, não consigo encontrar o problema ou falta de informação que está causando isso.
Utilizei a função println e ela funciona.
Agradeço se poderem me passar o que posso fazer.
identar package projectZelda;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import com.gcstudio.entities.Entity;
import com.gcstudio.entities.Player;
import com.gcstudio.graficos.Spritesheet;
public class Game extends Canvas implements Runnable,KeyListener {
private static final long serialVersionUID = 1L;
public JFrame frame;
private Thread thread;
private boolean isRunning = true;
private final int WIDTH = 240;
private final int HEIGHT = 160;
private final int SCALE = 3;
private BufferedImage image;
public List<Entity> entities;
public Spritesheet spritesheet;
private Player player;
public Game() {
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initFrame();
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
entities = new ArrayList<Entity>();
spritesheet = new Spritesheet("/spritesheet.png");
player = new Player(0,0,16,16,spritesheet.getSprite(32, 0, 16,16));
entities.add(player);
}
public void initFrame() {
frame = new JFrame("Marizelda");
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop() {
isRunning = false;
try {
thread.join(); }
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
Game game = new Game();
game.start();
}
public void tick() {
for (int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.tick();
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(0,255,0));
g.fillRect(0,0,WIDTH,HEIGHT);
for (int i = 0; i < entities.size();i++) {
Entity e = entities.get(i);
e.render(g);
}
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image,0,0,WIDTH*SCALE,HEIGHT*SCALE,null);
bs.show();
}
public void run(){
long lastTime = System.nanoTime();
double amountOfThicks = 60.0;
double ns = 1000000000 / amountOfThicks;
double delta = 0;
int frames = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta+= (now - lastTime)/ns;
lastTime=now;
if(delta>=1){
tick();
render();
frames++;
delta--;
}
if(System.currentTimeMillis()-timer >= 1000) {
System.out.println("FPS: "+frames);
frames = 0;
timer+=1000;
}
}
stop();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D) {
player.right = true;}
else if(e.getKeyCode()==KeyEvent.VK_LEFT ||
e.getKeyCode()==KeyEvent.VK_A){
player.left = true;}
if(e.getKeyCode() == KeyEvent.VK_UP ||
e.getKeyCode()==KeyEvent.VK_W){
player.up = true; }
else if(e.getKeyCode()==KeyEvent.VK_DOWN ||
e.getKeyCode()==KeyEvent.VK_S) {
player.down = true;}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D){
player.right = false;
}else if(e.getKeyCode()==KeyEvent.VK_LEFT ||
e.getKeyCode()==KeyEvent.VK_A){
player.left = false;}
if(e.getKeyCode() == KeyEvent.VK_UP ||
e.getKeyCode()==KeyEvent.VK_W){
player.up = false;}
else if(e.getKeyCode()==KeyEvent.VK_DOWN ||
e.getKeyCode()==KeyEvent.VK_S) {player.down = false;}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
identar package com.gcstudio.entities;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Entity {
protected int x;
protected int y;
protected int width;
protected int height;
private BufferedImage sprite;
public Entity(int x, int y, int width, int height, BufferedImage sprite) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.sprite = sprite;
}
public void setX(int newX) {
this.x = newX;
}
public void setY(int newY) {
this.y = newY;}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public void tick() {
}
public void render(Graphics g) {
g.drawImage(sprite,this.getX(),this.getY(),null);
}
}
identar package com.gcstudio.entities;
import java.awt.image.BufferedImage;
public class Player extends Entity {
public boolean right,up,left,down;
public int speed = 4;
public Player(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, sprite);
}
public void tick() {
if(right)
x+=speed;
else if(left)
x-=speed;
if(up)
y-=speed;
else if(down)
y+=speed;
}
}