PDA

View Full Version : [AS3] weird tile engine error


infernosnow
06-25-2009, 08:55 PM
Ok, so I'm coding a tile engine and have come across this weird error. It didn't occur to me that it was an error until I tried to increase the number of pixels my character was moving per frame. I had coded the movement in such a way that if the map was to move say, 6 pixels to the right, it would run a hitTestPoint() for every pixel and gradually move the map 1 pixel every time. This is so that I don't have my character moving partially through walls. If we're only hittesting every 6 pixels, we're either going to have a big gap between the character and the wall or we're going to be too far in. So checking for a hittestpoint for every pixel prevents this.

This method works fine when you have a stationary wall and a moving character or when you have a stationary character and a moving wall. The problem came when I tried to put the character in a movieclip and move the movieclip instead and also move the character inside the movieclip in the opposite direction to give the illusion of a stationary character. ...get all that? >.> The basic collisions work fine, but it's not doing the hitTestPoint for every pixel correctly. The result is having the character go halfway into a wall.

I have illustrated as best I can what is happening in the attached picture. I have attached an fla of the example as well. It has taken me five hours trying to figure out WHAT this error was. I stripped my game down from 1400 lines of code to 307 just to isolate the movement code and still couldn't figure out what was wrong until now.

Here is the code too if you want to look at it instead of the fla itself:

stage.addEventListener(Event.ENTER_FRAME,EnterFram eHandler);

var boxSpeed = 10;

function EnterFrameHandler(e:Event):void {
for(var i=1;i<=boxSpeed;i++)
{
var point1:Point=new Point(boxcont.box.getBounds(root).x+boxcont.box.wi dth,boxcont.box.getBounds(root).y);
var point2:Point=new Point(boxcont.box.getBounds(root).x,boxcont.box.ge tBounds(root).y);
if(wall.hitTestPoint(point1.x,point1.y,true) || wall.hitTestPoint(point2.x,point2.y,true))
{
break;
}
boxcont.box.y++;//box and boxcont movements negate each
boxcont.y--;//other to give stationary box illusion
wall.y++;
}
}

infernosnow
06-25-2009, 09:30 PM
oh yeah, and there should also actually be a -1 for the .y part for each Point() since it should be checking ahead of itself.

sydd_hu
06-25-2009, 11:06 PM
hmm for the first look:

getBounds() and hittestPoint() recalculate only after the frame has been redrawn.(i think)

infernosnow
06-26-2009, 12:06 AM
I did some testing myself and I believe it's a problem with hitTestPoint because getBounds() seems to be tracing the correct values for every loop of the for() loop. If hitTestPoint() is only calculating correctly after every frame that means it's creating temporary variables to store the x and y values and is referencing those instead of the actual x and y values of the object. I know this because if you trace the x and y values throughout the loop, they are in fact correct.

infernosnow
06-26-2009, 12:30 AM
Well, I think hitTestPoint() is officially bugged, but the good news is that adding a -i to the getBounds value is an easy workaround. Since hitTestPoint won't update the current x and y values, you simply force it to by adding the appropriate amount, which conveniently is i.