-
Hit test help, please!
Hello,
I'm trying to get a movieclip of a person to climb a movieclip of a wall, but the wall is uneven, so I'd like the person to hug the surface of the wall closely (without leaving it at all, actually) as it climbs vertically up it.
This is the code I've come up with, but the player just goes to the wall's initial x value and climbs straight vertically from there, without following the curve of the wall:
Code:
var climbSpeed:Number = 5;
addEventListener(Event.ENTER_FRAME, climbing);
function climbing(evt:Event):void {
if (player.hitTestPoint(wall.x, player.y, true)) {
player.y = (player.y - climbSpeed);
} else {
player.y = (player.y - climbSpeed);
player.x = wall.x;
}
}
Where am I going wrong?
Thanks.
-
-
You chose the wrong method for this.
Actually you should use BitmapData's hitTest() method for curvy surfaces
-
Ah brilliant, thanks casey. I found a tutorial on BitmapData and got it working.
can you (or anyone else) figure out how I'd get the player movieclip to always stay on the outline of the wall movieclip as it scrolls down? I've commented in my code below where I am struggling:
Code:
function stickToWall(evt:Event):void {
wall.y = (wall.y + wallScrollSpeed);
var playerRect:Rectangle = player.getBounds(this);
var playerOffset:Matrix = player.transform.matrix;
playerOffset.tx = player.x - playerRect.x;
playerOffset.ty = player.y - playerRect.y;
var playerBmpData = new BitmapData(playerRect.width, playerRect.height, true, 0);
playerBmpData.draw(player, playerOffset);
var wallRect:Rectangle = wall.getBounds(this);
var wallBmpData = new BitmapData(wallRect.width, wallRect.height, true, 0);
var wallOffset:Matrix = wall.transform.matrix;
wallOffset.tx = wall.x - wallRect.x;
wallOffset.ty = wall.y - wallRect.y;
wallBmpData.draw(wall, wallOffset);
var wallLoc:Point = new Point(wallRect.x, wallRect.y);
var playerLoc:Point = new Point(playerRect.x, playerRect.y);
if (wallBmpData.hitTest(wallLoc, 255, playerBmpData, playerLoc, 255)) {
player.x = player.x - wallOffset.tx; // it works but it's extremely bumpy and glitchy!
} else {
player.x = player.x + wallOffset.tx; // same here!
}
playerBmpData.dispose();
wallBmpData.dispose();
}