Greetings,
I’ve been reading up on some tutorials on as3isolib and fiddling around with rendering isometric terrain. What I have so far is a button that randomly generates one of two types of tiles across a grid, and a blue ball placed in the center of a random tile (based heavily on the Lee Brimelow gotoAndLearn() tutorials). Here’s an excerpt of the script:
private function btnClick(event:MouseEvent):void
{
var iso:IsoSprite;
var ball:IsoSprite = new IsoSprite ();
ball.sprites = [blueball];
while (scene.numChildren)
{
scene.removeChildAt(0);
}
for (var i:int=0; i<10; i++)
{
for (var j:int=0; j<10; j++)
{
iso = new IsoSprite();
if (Math.random() > 0.5)
{
iso.sprites = [brick];
iso.moveBy(j*CELL_SIZE, i*CELL_SIZE, 0);
scene.addChild(iso);
with (iso)
{
height = 18;
width = CELL_SIZE;
length = CELL_SIZE;
}
}
else
{
iso.sprites = [grass];
iso.moveBy(j*CELL_SIZE, i*CELL_SIZE, 0);
scene.addChild(iso);
with (iso)
{
height = 0;
width = CELL_SIZE;
length = CELL_SIZE;
}
}
}
}
scene.addChild(ball);
ball.moveTo(Math.ceil(Math.random()*10)*CELL_SIZE - CELL_SIZE/2, Math.ceil(Math.random()*10)*CELL_SIZE - CELL_SIZE/2, 0);
//scene.addChild(box);
scene.render();
}
So the first thing the button does is clear all movie clips, then proceeds to generate new terrain on a 10x10 grid using a nested for loop, defining brick tiles as having a greater height than grass tiles. Then it plops the blue ball on a random tile. This works just fine and dandy.
Now, what I would like to do is have the ball check whether it’s sitting on top of grass or on top of a brick, and if it’s sitting on top of a brick, I want the ball’s z coordinate to match the brick’s height so that it sits on top and isn’t hidden within the brick itself.
So, I’m thinking I need something that returns the isometric coordinates of the ball, and checks to see what kind of sprite exists at those coordinates, and if it’s a brick, change the ball’s z coordinate to 18. A pseudocode might look something like this:
var ballposition:Pt = new Pt(ball.x, ball.y, 0);
if(scene.getObjectsunderpt(ballposition) == brick)
{
ball.z = 18;
}
The problem is, as3isolib doesn’t really have an equivalent to getObjectsunderpoint(), as far as I’m aware. Add that to the fact that I’m not really sure how getObjectsunderpoint() works to begin with.
The other possible solution was if I could run a hit test to see if the ball was hitting a brick, that would make the ball sit on top of it. Again, problem is there’s no isometric hit test available in the library.
Even if either of those techniques were available, I can’t seem to figure out how to reference these randomly generated sprites, and if I can’t reference them the ball can’t interact with them.
If anyone has a solution to this I’d be very grateful. =] Cheers.