Movimento do personagem em Y

Galera to desenvolvendo um game aqui do mas to com problema. os inimigos perseguem corretamente o player no eixo x mas no y dependendo da posição eles bugao ficam tremendo tentado achar um valo inteiro( não sei por q não encontra um valor inteiro ja q eu fiz o mesmo castyng para os eixos x e y)…
o Game ja ta bem andado nao sei qual classe expor aqui pois ta tudo conectado…

package Pack.Entities;

import java.awt.image.BufferedImage;

import Pack.game.Game;

public class Inimigos extends Entity{
	
	private double speed =2.5;

	public Inimigos(int x, int y, int widtht, int height, BufferedImage sprite) {
		super(x, y, widtht, height, sprite);
		
	}

	public void tick() {
		
		if ((int)x<Game.player.getX()){
			x += speed;
			
		}
		else if ((int)x>Game.player.getX()){
			x -= speed;
		}
		else if  ((int)y<Game.player.getY()){
			y += speed;
			
		}
		else if ((int)y>Game.player.getY()){
			y -= speed;
	
	}
	}
}

/////////////////// Clçasse Entidades //////////////////////////////////////

package Pack.Entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import Pack.World.Camera;
import Pack.game.Game;

public class Entity {
	
	protected double x;
	protected double y;
	protected int widtht;
	protected int height;
	protected BufferedImage sprite;
	
	public static BufferedImage Arma= Game.spriteSheet.getSprite(64*8, 64, 64, 64);
	public static BufferedImage Muniçao= Game.spriteSheet.getSprite(64*6, 64, 64, 64);
	public static BufferedImage Potion_Vida= Game.spriteSheet.getSprite(64*7, 64, 64, 64);
	public static BufferedImage Inimigo_1= Game.spriteSheet.getSprite(64*3, 0, 64, 64);
	
	
	public Entity(int x, int y, int widtht, int height, BufferedImage sprite) {
		
		this.x=x;
		this.y = y;
		this.widtht = widtht;
		this.height =height;
		this.sprite =sprite; 	
	}
	
	public void setX(int newX) {
		this.x = newX;
	}
	public void setY(int newY) {
		this.y = newY;
	}
	public int getY() {
		return (int)this.y;
	}
	public int getX() {
		return (int)this.x;
	}
	public int getWidtht() {
		return widtht;
	}
	public void setWidtht(int widtht) {
		this.widtht = widtht;
	}
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void tick() {
		
	}
	public void render(Graphics g) {
		g.drawImage(sprite,getX()-Camera.x, getY()-Camera.y, null);
	}
	
}

Qual é o código onde eles ficam tentando achar um valor inteiro?

esta no método tick da classe inimigo mas ele estende a posição das enntities…
nao sei se e isso mas e como se nao tivese convertendo para inteiro em um dos eixos
(int) y > Game.player.getY()

apesar de q se eu usar else if em todas as funções os inimigos nao vao travar, porem nao queroq eles se movam na diagonal;

public class Inimigos extends Entity {

	private double speed = 2.5;

	public Inimigos(int x, int y, int widtht, int height, BufferedImage sprite) {
		super(x, y, widtht, height, sprite);

	}

	public void tick() {

		if ((int) x < Game.player.getX()) {
			x += speed;

		} else if ((int) x > Game.player.getX()) {
			x -= speed;
		} else if ((int) y < Game.player.getY()) {
			y += speed;

		} else if ((int) y > Game.player.getY()) {
			y -= speed;

		}
	}
}

///// Clase entities/////

package Pack.Entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import Pack.World.Camera;
import Pack.game.Game;

public class Entity {
	
	protected double x;
	protected double y;
	protected int widtht;
	protected int height;
	protected BufferedImage sprite;
	
	public static BufferedImage Arma= Game.spriteSheet.getSprite(64*8, 64, 64, 64);
	public static BufferedImage Muniçao= Game.spriteSheet.getSprite(64*6, 64, 64, 64);
	public static BufferedImage Potion_Vida= Game.spriteSheet.getSprite(64*7, 64, 64, 64);
	public static BufferedImage Inimigo_1= Game.spriteSheet.getSprite(64*3, 0, 64, 64);
	
	
	public Entity(int x, int y, int widtht, int height, BufferedImage sprite) {
		
		this.x=x;
		this.y = y;
		this.widtht = widtht;
		this.height =height;
		this.sprite =sprite; 	
	}
	
	public void setX(int newX) {
		this.x = newX;
	}
	public void setY(int newY) {
		this.y = newY;
	}
	public int getY() {
		return (int)this.y;
	}
	public int getX() {
		return (int)this.x;
	}
	public int getWidtht() {
		return widtht;
	}
	public void setWidtht(int widtht) {
		this.widtht = widtht;
	}
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void tick() {
		
	}
	public void render(Graphics g) {
		g.drawImage(sprite,getX()-Camera.x, getY()-Camera.y, null);
	}
	
}

Provavelmente o problema está justamente em você fazer o cast de double pra int, com isso você está truncando o valor.

Experimenta arredondar o valor ao invés de truncar.

Cria dois novos métodos no seu Player e utiliza eles:

public int getRoundX() {
    return (int) (getX() + 0.5);
}

public int getRoundY() {
    return (int) (getY() + 0.5);
}

ele fica pedindo para criar tray e catch memo q eu faça continua pedindo, ou passo o método get como static ai gera outro BO vai pedir outras variáveis como static ate chegar a variável x das entidades q nao pode ser statica

sou muito persistente mas não to conseguindo resolver esse BO, ja conferi os tiles para 16 pixels ae funciona, mas tb não quero usar 16pixels a imagem e ruim de mais.
tem como ver o projeto q ta no zip pelo google drive?

Sempre poste seu código.

Esse não é um arquivo ZIP é um arquivo RAR.

Seu código não pede nenhum try-catch.

Como eu já mencionei antes, o problema do seu código está no cast de double pra int, onde ao invés de truncar você deveria arredondar o valor.

Sua lógica também está errada, você só está computando as coordenadas em Y caso não compute as coordenadas em X, ou seja está computando o Y num else, está errado, tem que sempre calcular ambos, X e Y.

Se você ajustar o código e fazer o arredondamento que sugeri, vai funcionar:

public void tick() {
    // primeiro somar 0.5 e depois fazer cast pra int
    // é uma forma de arredondar sem usar o Math.round
    int playerX = (int) (Game.player.getX() + 0.5);
    int playerY = (int) (Game.player.getY() + 0.5);

    // primeiro um if-else pra decidir entre mover o inimigo pra direita ou esquerda
    if (x < playerX) {
        x += speed;
    } else if (x > playerX) {
        x -= speed;
    }

    // depois um if-else pra decidir entre mover o inimigo pra baixo ou pra cima
    if (y < playerY) {
        y += speed;
    } else if (y > playerY) {
        y -= speed;
    }
}
1 curtida

o código não e meu so to replicado do curso, mas bem a achei isso estranho, começo a entender oq vc quer dizer e ta fazendo mais sentido mesmo amanha vou aplicar e qualquer coisa volto aqui.

oi Brother voltei estava na correria voltei na minha duvida infelizmente o método ainda dão deu certo tentei outras alterações mas esse bug ainda persiste :frowning:
bug

Imagens não ajudam a identificar o problema.
Posta o seu código atualizado e não esqueça de usar o botão de formatação de código: </>.

ok…
ClASE DOS INIMIGOS=======

public class Inimigos extends Entity{
	
	public boolean right_IN, up_IN, left_IN,down_IN;
	public int right_dir_IN=0, up_dir_IN=1, left_dir_IN=3,down_dir_IN=4;
	public int dir_IN = down_dir_IN;
	public double speed_IN= 2.0;
	private int frames_IN =0, maxFrame_IN=7, index_IN =1,maxIndex_IN=2;
	private boolean muved_IN = false;
	private int mascaraX=16 ,mascaraY=16,mascaraW=30, mascaraH=30;
	
	private BufferedImage[] right_INimigo;
	private BufferedImage[] left_INimigo;
	private BufferedImage[] up_INimigo;
	private BufferedImage[] down_INimigo;

	public Inimigos(int x, int y, int widtht, int height, BufferedImage sprite) {
		super(x, y, widtht, height, null);
		
		this.right_INimigo = new BufferedImage[3];
		this.left_INimigo = new BufferedImage[3];
		this.up_INimigo = new BufferedImage[3];
		this.down_INimigo = new BufferedImage[3];
		
		for(int i=0; i<3;i++) {
			this.right_INimigo [i]=Game.spriteSheet.getSprite(192+(i*64), 128, 64, 64);
			this.left_INimigo [i]=Game.spriteSheet.getSprite(192+(i*64), 64, 64, 64);
			this.up_INimigo [i]=Game.spriteSheet.getSprite(192+(i*64), 192, 64, 64);
			this.down_INimigo [i]=Game.spriteSheet.getSprite(192+(i*64), 0, 64, 64);
		}
		
		
	}

	public void tick() {
		
	if(inimigoColideComPlayer()== false) {

		if(Game.aliatorio.nextInt(100)<50) {
			
		    // primeiro somar 0.5 e depois fazer cast pra int
		    // é uma forma de arredondar sem usar o Math.round
		    int playerX = (int) (Game.player.getX() + 0.5);
		    int playerY = (int) (Game.player.getY() + 0.5);
		    // primeiro um if-else pra decidir entre mover o inimigo pra direita ou esquerda
		    if (x < playerX && World.caminhoLivre((int)(x+speed_IN), this.getY())
					&& !inimigoColide((int) (x+speed_IN), this.getY())) {
		    	this.dir_IN = right_dir_IN;
		        x += speed_IN;
		    } else if (x > playerX && World.caminhoLivre((int)(x-speed_IN), this.getY())
					&& !inimigoColide((int) (x-speed_IN), this.getY())) {
		    	this.dir_IN = left_dir_IN;
		        x -= speed_IN;
		    }

		    // depois um if-else pra decidir entre mover o inimigo pra baixo ou pra cima
		    else if (y < playerY && World.caminhoLivre(this.getX(),(int)(y+speed_IN) )
					 && !inimigoColide(this.getX(),(int)(y+speed_IN))) {
		    	this.dir_IN =down_dir_IN;
		        y += speed_IN;
		    } else if (y > playerY && World.caminhoLivre(this.getX(),(int)(y-speed_IN) )
					 && !inimigoColide(this.getX(),(int)(y-speed_IN))) {
		    	this.dir_IN = up_dir_IN;
		        y -= speed_IN;
		    }
			
		
		if (muved_IN) {
			frames_IN++;
			if(frames_IN == maxFrame_IN) {
				frames_IN=0;
				index_IN ++;
				if(index_IN >maxIndex_IN) {
					index_IN=0;
				}
			}
		}
		
		}
	
	}
	else {
		
		if(Game.aliatorio.nextInt(100)<10) {
			Game.player.vidaPlayer --;
		}
		
	//	System.out.println("VIDA: "+Game.player.vidaPlayer);
		Game.player.tomandoDano =true;
			if(Game.player.vidaPlayer<=0) {
			System.exit(1);
	}		
	}	
	}

=====Classe WORLD

public class World {

	private static Tile[] tiles;
	public static int WIDTH, HEIGHT;
	public static final int TILE_size=64;


	public World(String path) {

		try {
			BufferedImage miniMapa = ImageIO.read(getClass().getResource(path));
			int[] pixels = new int[miniMapa.getWidth() * miniMapa.getHeight()];

			WIDTH = miniMapa.getWidth();
			HEIGHT = miniMapa.getHeight();

			tiles = new Tile[miniMapa.getWidth() * miniMapa.getHeight()];

			miniMapa.getRGB(0, 0, miniMapa.getWidth(), miniMapa.getHeight(), pixels, 0, miniMapa.getWidth());

			for (int xx = 0; xx < miniMapa.getWidth(); xx++) {
				for (int yy = 0; yy < miniMapa.getHeight(); yy++) {

					int pixelAtual = pixels[xx +(yy * miniMapa.getWidth())];
					
					tiles[xx + (yy * this.WIDTH)]= new TileChao(xx*64,yy*64,Tile.Tile_chao);
				
					// Chão!
					if (pixelAtual == 0xff070000) {

						tiles[xx+(yy*this.WIDTH)] = new TileChao(xx*64,yy*64,Tile.Tile_chao);
						
					}
					// Parede!
					else if (pixelAtual == 0xffffffff) {
				
						tiles[xx+(yy*this.WIDTH)] = new TileParede(xx*64,yy*64,Tile.Tile_parede);
						
					} 
					// Player!
					else if (pixelAtual == 0xff0c3ee6) {
						
					Game.player.setX(xx*64);
					Game.player.setY(yy*64);
						
					}
					// Arma!
					else if (pixelAtual == 0xff74685a) {
						
					Game.entities.add(new Arma(xx*64,yy*64,64,64,Entity.Arma));
						
					}
					// Muniçãi!
					else if (pixelAtual == 0xfffeee00) {
						
						Game.entities.add(new Muniçao(xx*64,yy*64,64,64,Entity.Muniçao));
						
					}
					// Vida!!
					else if (pixelAtual == 0xff00ff00) {
						
						Game.entities.add(new Potion(xx*64,yy*64,64,64,Entity.Potion_Vida));
					}
					// Inimigo_1!
					else if (pixelAtual == 0xffe90505) {
						
						Inimigos IN = new Inimigos(xx*64,yy*64,64,64,Entity.Inimigo_1);
						Game.entities.add(IN);
						Game.inimigosLista.add(IN);
					}

				}
			}

		} catch (IOException e) {

			e.printStackTrace();
		}

	}
	
	public static boolean caminhoLivre(int xNext, int yNext) {
		
		int x1 = xNext / TILE_size;
		int y1 = yNext / TILE_size;
		
		int x2 = (xNext + TILE_size-1) / TILE_size;
		int y2 = yNext  / TILE_size;
		
		int x3 =  xNext / TILE_size;
		int y3 = (yNext  + TILE_size-1)/TILE_size;
		
		int x4 =  (xNext  + TILE_size-1)/TILE_size;
		int y4 = (yNext  + TILE_size-1)/TILE_size;
		
		
		return !(tiles[x1+(y1*World.WIDTH)]instanceof TileParede
				|| tiles[x2+(y2*World.WIDTH)]instanceof TileParede
				|| tiles[x3+(y3*World.WIDTH)]instanceof TileParede
				|| tiles[x4+(y4*World.WIDTH)]instanceof TileParede);
		
	}

	public void render(Graphics g) {

		int xstart = Camera.x >>6;
		int ystart = Camera.y >>6;
		
		int xfinal = xstart+(Game.WIDTH >>6)+1;
		int yfinal = ystart+(Game.HEIGHT >>6);
		
		for (int xx = xstart; xx <=xfinal; xx++) {
			for (int yy = ystart; yy <= yfinal; yy++) {
				
				if(xx<0 || yy< 0 || xx>= WIDTH || yy >= HEIGHT) 
					continue;		
				Tile tile = tiles[xx+(yy*WIDTH)];
				tile.render(g);
			}
		}
	}
}

ClASE PLAYER====

public class Player extends Entity {

	public boolean right, up, left, down;
	public int right_dir = 0, up_dir = 1, left_dir = 3, down_dir = 4;
	public int dir = down_dir;
	public double speed = 3.0;
	private int frames = 0, maxFrame = 7, index = 1, maxIndex = 2;
	private boolean muved = false;
	public double vidaPlayer = 100, vidaMaxima = 100;
	public static int muniçao = 0;
	public boolean tomandoDano = false;
	private int danoFrames = 0;
	//private int mascaraX = 16, mascaraY = 16, mascaraW = 30, mascaraH = 30;

	private BufferedImage[] right_Player;
	private BufferedImage[] left_Player;
	private BufferedImage[] up_Player;
	private BufferedImage[] down_Player;

	private BufferedImage[] right_PlayerDano;
	private BufferedImage[] left_PlayerDano;
	private BufferedImage[] up_PlayerDano;
	private BufferedImage[] down_PlayerDano;

	public Player(int x, int y, int widtht, int height, BufferedImage sprite) {
		super(x, y, widtht, height, sprite);

		this.right_Player = new BufferedImage[3];
		this.left_Player = new BufferedImage[3];
		this.up_Player = new BufferedImage[3];
		this.down_Player = new BufferedImage[3];

		for (int i = 0; i < 3; i++) {
			this.right_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 128, 64, 64);
			this.left_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64, 64, 64);
			this.up_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 192, 64, 64);
			this.down_Player[i] = Game.spriteSheet.getSprite(0 + (i * 64), 0, 64, 64);
		}
		// Player toma dano
		this.right_PlayerDano = new BufferedImage[3];
		this.left_PlayerDano = new BufferedImage[3];
		this.up_PlayerDano = new BufferedImage[3];
		this.down_PlayerDano = new BufferedImage[3];

		for (int i = 0; i < 3; i++) {
			this.right_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 6, 64, 64);
			this.left_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 5, 64, 64);
			this.up_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 7, 64, 64);
			this.down_PlayerDano[i] = Game.spriteSheet.getSprite(0 + (i * 64), 64 * 4, 64, 64);
		}

	}

	public void tick() {
		muved = false;
		// se precisonasr right
		if (right && World.caminhoLivre((int) (x + speed), this.getY())) {
			muved = true;
			dir = right_dir;
			x += speed;
			// System.out.println("valor"+x);
		} else if (left && World.caminhoLivre((int) (x - speed), this.getY())) {
			muved = true;
			dir = left_dir;
			x -= speed;
		} else if (up && World.caminhoLivre(this.getX(), (int) (y - speed))) {
			muved = true;
			dir = up_dir;
			y -= speed;
		} else if (down && World.caminhoLivre(this.getX(), (int) (y + speed))) {
			muved = true;
			dir = down_dir;
			y += speed;
		}
		//
		if (muved) {
			frames++;
			if (frames == maxFrame) {
				frames = 0;
				index++;
				if (index > maxIndex) {
					index = 0;
				}
			}
		}
		checarColisao_PVida();
		checarColisao_Muniçao();
		//playColideComParede();

		if (tomandoDano) {
			danoFrames++;
			if (danoFrames == 8) {
				danoFrames = 0;
				tomandoDano = false;
			}
		}

		if (this.vidaPlayer <= 0) {
			Game.entities = new ArrayList<Entity>();
			Game.inimigosLista = new ArrayList<Inimigos>();
			Game.spriteSheet = new SpreteSheet("/sprites.png");
			Game.player = new Player(0, 0, 64, 64, Game.spriteSheet.getSprite(0, 0, 64, 64));
			Game.entities.add(Game.player);
			Game.world = new World("/MiniMapa.png");
			return;
		}

		Camera.x = Camera.clamp(this.getX() - (Game.WIDTH / 2), 0, World.WIDTH * 64 - Game.WIDTH);
		Camera.y = Camera.clamp(this.getY() - (Game.HEIGHT / 2), 0, World.HEIGHT * 64 - Game.HEIGHT);
}

Falta de atenção, como já expliquei anteriormente, ao calcular as coordenadas y você não deve fazer else if else é somente if else.

É 1 if else pro x e 1 if else pro y.

São duas coordenadas independentes, se você usar o else ele nunca vai calcular o y.

tipo eu uso o else para q eles se movam somente nas 4 direções, se fo so o if os inimigos tb andam na diagonal q nao e o que eu gostaria, mas de qualquer modo com else ou não o bug continua.

Do jeito que você está fazendo, toda vez que movimentar em x, vai ignorar o movimento em y.

então era isso que eu queria move apenas em x ou apenas em y (não queria que os inimigos se movessem na diagonal quando eu tiro o ) , mas tanto eu usando… a função com o else ou sem o else o movimento ficam bugados vc iria entender melhor se eu mostrasse o vídeo da ou vc abrisse o projeto, amanha vou ver se boto vlink de vídeo do youtub aqui não sei e se e permitido. Ta um BO isso kkkk mas não aquento ficar nessa duvida

public void tick() {
		
	if(inimigoColideComPlayer()== false) {

		if(Game.aliatorio.nextInt(100)<50) {
			
		    // primeiro somar 0.5 e depois fazer cast pra int
		    // é uma forma de arredondar sem usar o Math.round
		    int playerX = (int) (Game.player.getX() + 0.5);
		    int playerY = (int) (Game.player.getY() + 0.5);
		    // primeiro um if-else pra decidir entre mover o inimigo pra direita ou esquerda
		    if (x < playerX && World.caminhoLivre((int)(x+speed_IN), this.getY())
					&& !inimigoColide((int) (x+speed_IN), this.getY())) {
		    	this.dir_IN = right_dir_IN;
		        x += speed_IN;
		    } else if (x > playerX && World.caminhoLivre((int)(x-speed_IN), this.getY())
					&& !inimigoColide((int) (x-speed_IN), this.getY())) {
		    	this.dir_IN = left_dir_IN;
		        x -= speed_IN;
		    }

		    // depois um if-else pra decidir entre mover o inimigo pra baixo ou pra cima
		     if (y < playerY && World.caminhoLivre(this.getX(),(int)(y+speed_IN) )
					 && !inimigoColide(this.getX(),(int)(y+speed_IN))) {
		    	this.dir_IN =down_dir_IN;
		        y += speed_IN;
		    } else if (y > playerY && World.caminhoLivre(this.getX(),(int)(y-speed_IN) )
					 && !inimigoColide(this.getX(),(int)(y-speed_IN))) {
		    	this.dir_IN = up_dir_IN;
		        y -= speed_IN;
		    }