How to stop an entity from moving when colliding?


(AnthonyY) #1

I have an entity, that when it collides with a hitbox, it can’t go any further. As of now, it just goes through the hitbox. Any way I could achieve this?


(Bartłomiej Kalemba) #2

Well… I’m just starting here, but try to help You:

I do it in this way: When You check the move Key:

if (Input.check(Key.DOWN))
		{
			newY = y + this.speed * FP.elapsed;
			if (newY + height > FP.height)
			{
				newY = FP.height - height;
			}
		}

Just assign the new value to other var. Then You can check collision:

if (collide("bullet", x, newY) == null)
		{
			y = newY;
		}

So, if there is no collision, I simply re assign new value to y :smile:

Hope this helpfull, but maybe someone more experienced say sth more… :slight_smile:


(AnthonyY) #3

What is this.speed? Obviously “this” works, but speed isn’t found after that.


(Bartłomiej Kalemba) #4

Speed is just object variable. You set it below lines:

public class Player extends Entity
{
	private var speed:int = 310;

You can set it whatever you want. Just check if it fit for Your game.

BTW: When You:

newY = y + this.speed * FP.elapsed;

FP.elapsed is necessary, because if You miss it, then player will be moving with different speed on different (slower/faster) computers.


(Jacob Albano) #5

Not necessarily. If you run in fixed-framerate mode, your update logic will be run at a consistent speed on any computer.

My approach is generally to use moveBy() to handle my collision.


var move:Point = new Point();
if (Input.check("left")) move.x--;
if (Input.check("right")) move.x++;
if (Input.check("up")) move.y--;
if (Input.check("down")) move.y++

move.normalize(speed);

moveBy(speed.x, speed.y, ["enemy", "platform"]);