AS3 hitTestPoint always returns false for one function
I have a movieclip I drawn, called Map. It has a child with the name Wall. I'm trying to make a function that returns a 2d tile array of if there is something in that tile or not, for pathfinding.
The problem is, levelGrid is entirely false. I'm not sure what I am doing wrong.
I have filled t开发者_JS百科he whole movieclip (Wall) with bright red, and yes it still returns false.
The x and y of both Map and Wall is 0, so I don't think there should be issues with hitTestPoint accepting global x / y.
They are added to a display list.
My other movement hittest works fine.
My code:
public function levelMap() {
for (var i = 0; i < height / 25; i++) {
levelGrid[i] = [];
for (var j = 0; j < width / 25; j++) {
levelGrid[i][j] = this.getChildByName("Wall").hitTestPoint(j*25,i*25,true);
}
}
trace(levelGrid);
}
Thanks in advance (levelGrid is a class variable)
I made a visual test out of your code. I did not change any functionality though so I think your problem is your graphics. Check the width, height och positions of the graphics. If you dont get anything in the Array, then maybe those values are not set correctly.
Each dot represent a hitTestPoint in this test. If its green you have a hit (true return value). Red means no hit. Hope this helps.
TestHitPoint.as
package
{
import flash.display.Sprite;
public class TestHitPoint extends Sprite
{
private static const TILE_SIZE : int = 25;
private var levelGrid : Array;
public function TestHitPoint()
{
// add graphics to test
addChild( new WallGraphics() );
// fix array
levelGrid = new Array();
// loop
for (var i : int = 0; i < height / TILE_SIZE; i++)
{
levelGrid[i] = [];
for (var j : int = 0; j < width / TILE_SIZE; j++)
{
var success : Boolean = this.getChildByName("Wall").hitTestPoint(j*TILE_SIZE, i*TILE_SIZE, true);
levelGrid[i][j] = success;
addChild( new PositionMarker(j*TILE_SIZE, i*TILE_SIZE, success) );
}
}
}
public function levelMap() : void
{
}
}
}
import flash.display.Sprite;
internal class WallGraphics extends Sprite
{
public function WallGraphics()
{
name = "Wall";
with(graphics)
{
beginFill(0x0000FF, 1);
drawRect(100, 50, 60, 200);
drawRect(50, 100, 200, 50);
endFill();
}
}
}
internal class PositionMarker extends Sprite
{
public function PositionMarker(posX : Number, posY : Number, success : Boolean)
{
with(graphics)
{
beginFill((success) ? 0x00FF00 : 0xFF0000, 1);
drawCircle(posX, posY, 4);
endFill();
}
}
}
Output:
精彩评论