Enemy Movement Problem

Hello Everyone,

I have a problem with my enemy movement. Basically when the player is to the right and/or below the enemy, the enemy doesn’t move towards the player. but if it is above and/or to the left of the enemy it moves towards the player. I cant seem to figure out whats wrong. Could you help. Thanks

	if(isMoving && shouldAttack){
			if(player.getX() < getX()){
				dx -= moveSpeed;
			}
			if(player.getY() < getY()){
				dy -= moveSpeed;
			}
			if(player.getX() > getX()){
				dx += moveSpeed;
			}
			if(player.getY() > getY()){
				dy += moveSpeed;
			}
		}

-GlennBrann

Think we need more code. Also have you tried printing out dx and dy after that code block, to check if that block is actually the problem? Bit suspicious about dx and dy though, is that his actualy position or are you later doing

xpos+=dx
ypos+=dy

which would cause problems

Here is my update method

public void update(){

		// Basic Pathfinding//
		if(isMoving && shouldAttack){
			if(player.getX() < getX()){
				dx -= speed;
			}
			if(player.getY() < getY()){
				dy -= speed;
			}
			if(player.getX() > getX()){
				dx += speed;
			}
			if(player.getY() > getY()){
				dy += speed;
			}
			checkMapCollision();
		}
		
		x += dx;
		y += dy;
		
		dx = 0;
		dy = 0;
		
		if(shoot == 1 && shouldAttack){
			firing = true;
		}
		
		if(firing){
			long elapsed = (System.nanoTime() - firingTime) / 1000000;
			if(elapsed > firingDelay){
				Controller.enemProj.add(new EnemyProjectile(x , y , tileMap, sprites, player));
				firingTime = System.nanoTime();
			}
		}
		
	}

Nothing I can pick out from that. Maybe it’s in the collision bit?

Also try

System.out.println("before collision, dx: “+dx+”, dy: "+dy);
checkMapCollision();
System.out.println("after collision, dx: “+dx+”, dy: "+dy);

Move around the areas it does work and does not work, then see what the print out says

I seem to have fix it. I think i was not adding the speed for right and down…hmm… weird