Thank you for visiting Blazing Games

Coding 3D Sprites

The 3D Sprites are rendered by going through the list of objects in the game and seeing if they are within the field of view. Those that are not are simply made invisible. The objects that are visible then have their distance from the camera determined. This information is used to sort the objects so that the closer objects are drawn above objects that are further away. The setChildIndex function is your friend here as it allows you to change the position within the display list that the object is.

public function render(secs:Number = 1):Boolean
{
var cntr:int, cntrZ:int;
var ang:Number;
var zBillboard:IBillboard;
var tbd:BillboardData = _playersSprites[_curCharacter].getBillboardData();
var px:Number = tbd.mapX;
var py:Number = tbd.mapY;
var ts:MovieClip;
var hght:Number;
var wdth:Number;
var visibleTrees:int = 0;
var indx:int;
var dist:Number;
var ground:Number;

// first, move charles
_charles.takeTurn(this, secs);
_playTime += secs;

// loop through the trees and render the visible ones
for (cntr = 0; cntr < _sprites.length; ++ cntr) {
ts = _sprites[cntr];
zBillboard = IBillboard(_sprites[cntr]);
tbd = zBillboard.getBillboardData();
ang = getAngle(tbd.mapX - px, tbd.mapY - py, 3200);
ang -= _playersD[_curCharacter];
ang += 640;//320;
while (ang < 0) ang += 3200;
while (ang > 3200) ang -= 3200;
if (ang < 1280) {
ts.visible = true;

dist = getDistance(tbd.mapX, tbd.mapY, px, py);
ground = 240 + 500.0 / dist;
hght = tbd.nearHeight / dist;
wdth = tbd.nearWidth / dist;
ts.x = ang - 320 - wdth/2;
ts.y = ground - hght;
ts.width = wdth;
ts.height = hght;
// z sort the tree against other billboards
indx = visibleTrees;
for (cntrZ = 0; cntrZ < visibleTrees; ++cntrZ) {
zBillboard = IBillboard(_forest.getChildAt(cntrZ));
if (dist > zBillboard.getBillboardData().getDistance()) {
indx = cntrZ;
break;
}
}
_forest.setChildIndex(ts, indx);
tbd.setDistance(dist);
++visibleTrees;

} else
ts.visible = false;
_playersSprites[_curCharacter].visible = false;
// trace (ts.mapX, ts.mapY, ang);
// ts.visible = ang < 640;
}
updateAutomap();
var distanceFromCharles:Number = 999;
tbd = _charles.getBillboardData();
for (cntr = 0; cntr < 3; ++cntr) {
dist = getDistance(
getPlayerX(cntr),getPlayerY(cntr), tbd.mapX,tbd.mapY);
if (distanceFromCharles > dist)
distanceFromCharles = dist;
}

return (distanceFromCharles < 1.0);
}

Previous page
Chapter 32 Page 5

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2009 Blazing Games Inc. All Rights Reserved