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++;
}
}
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++;
}
}