This is a problem I've spent way to much time on. As most things that take a long time to solve the solution is rather simple. This example is using a code base that draws a grid of some fixed size and has a path following the grid row and columns. There is a fixed path for each level in the game stored in a case statement that fills an array with the path for a given level. Path points are stored as there grid positions.
The simple version of this code is that we make an array and store all the "Occupied" positions. Once we have that this function checks each value of our Array. During this check the function compares the mouse x,y position to the Array's x,y positions. The only time this function sets our Boolean value to true is when both the mouse x,y are equal to one of the Array's x,y positions.
/*
*Declared in side (main.as)
*public var OccupiedValue :Boolean = false;
*public var Occupied:Array = new Array;
*Occupied = (Path:Array);
*Declared in side (tower.as)
*var game:(Main.as);
*Occupied.push((new tower x,y));
*Original author: Ian Mott @ www.IanMott.com -Please leave comment if you use this code. *Thank you
*/
public function checkOccupied():void
{
//Result of this check Boolean value
game.OccupiedValue = false;
//Position of the mouse
var x1 :int = this.parent.mouseX;
var y1 :int = this.parent.mouseY;
//Format Position to grid size
x1 = Math.ceil(x1/this.game.grid_size);
y1 = Math.ceil(y1/this.game.grid_size);
//Check Occupied for same x, y
for (var i :int = 1; i < game.Occupied.length; i++)
{
var p_point :Array = game.Occupied[i];
var pX :int, pY :int;
pX = p_point[0];
pY = p_point[1];
if (pX == x1 && pY == y1)
{
game.OccupiedValue = true;
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment