Bug Help Please


(Benjamin Wagner) #1

Hello, I have run into a bug in my flashpunk code that i also noticed in the breakout game demo for haxepunk. I am unaware of the cause, and therefore don’t know how to fix it, but if I make an entities velocity to fast, it ignores simple bounds checking and goes out of bounds, never to be seen again.`

public function MyEntity() 
	{
		graphic = new Image(PLAYER);
		graphic.x = 320;
		graphic.y = 240;
		xa = 20;
		ya = 20;
	}
	
	override public function update():void
	{
		var x:int = Input.mouseX;
		var y:int = Input.mouseY;
		if (graphic.y == 520)
		{
			ya = -ya;
		}
		
		if (graphic.x == 700)
		{
			xa = -xa;
		}
		
		if (graphic.y == -40)
		{
			ya = -ya;
		}
		
		if (graphic.x == -50)
		{
			xa = -xa;
		}
		graphic.x += xa;
		graphic.y += ya;
	}`

Any Help, on how to fix the bounds checking would be much appreciated.


(Ben Pettengill) #2

I think instead of this:

		if (graphic.y == 520)
	{
		ya = -ya;
	}

you should be doing this:

	if (graphic.y > 520)
	{
		y = 520;
		ya = -ya;
	}

Right now, you’re checking if the sprites’s x and y values are exactly on the borders, but if it’s moving fast enough, it’s easy for it to skip over that single-pixel line. To fix it, you want to check if the sprite is beyond the border, then move it back so it’s touching the border, and then change direction.


(Martí Angelats i Ribera) #3

It’s better not to use the varibles x and y becouse they alrady exist as variables in the entity (BTW, in your code they are unused). Instead of using the graphic x and y i recomend you to use the x and y of the entity (it’s good to use it becouse itwill make you able to handle things like collisions).

A code with the same functionallity:

public class MyEntity extends Entity
{
    
    private var xa:Number;
    private var ya:Number;
        
    public function MyEntity() 
    {
        super(320, 240, new Image(PLAYER));
        xa = 20;
        ya = 20;
    }
    
    override public function update():void
    {
        if (graphic.y >= 520)
        {
            ya = -ya;
        }

        if (graphic.x >= 700)
        {
            xa = -xa;
        }

        if (graphic.y <= -40)
        {
            ya = -ya;
        }

        if (graphic.x <= -50)
        {
            xa = -xa;
        }
        x += xa;
        y += ya;
    }
}

Notice that in this code you move the entity instead of the graphic in it.

PD: The graphic.x and graphic.y are the offset of the graphic. Don’t use them to move “things”. The correct way is move the entire entity.

PD2: In general, when using intervals the conditions use >= and <= to avoid problems like that. It’s good to take it like a general rule for any programming lenguage.

Edit: Better use the constructor, no?


(Benjamin Wagner) #4

Thanks, that fixed it. It makes sense now. I used >= and <= and initialized x and y in the if statement block. It’s been a while since I’ve been working with the game development side of programming. Also, I didn’t know that entity already had an x and y object. I’m new to flashpunk, and was trying to get stuff to work without the documentation. Thanks again.


(Martí Angelats i Ribera) #5

I recomend you to have a look [here (tutorials)][1]. They teach you how to do all the basic things. They are not extensive and they may help you (even without writte and test them, they can help to know how FP works in general).

PD: By deafult the x and y of an entity is 0

PD2: You can use the Entity constructor to set the x, y and graphic (in this case using super(x, y, graphic); ) [1]: http://useflashpunk.net/tutorials/