我开始制作舞台宽度:800,高度 600。
然后我在舞台上添加了一个空白的 MovieClip 并旋转它以获得 3d 外观。然后,我将地图图形和播放器图形添加到黑色 MovieClip。然后我将对象放置到地图图形中。基本上,当按下向上键时,它会移动地图图形,当按下左/右键时,它会旋转空白影片剪辑。
现在的问题是,当我尝试获取地图图形上的对象与玩家图形之间的距离,并尝试根据距离对它们进行重新排序时。出于某种原因,当我使用 (trace(Ground.getChildAt(0)); 时,我得到 [object Shape] 而不是 [object Crate]。
我不确定这是否是整个问题,或者我可能没有正确分类它们。对此的任何帮助将不胜感激。
这是代码:
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
//Changing Perspective
root.transform.perspectiveProjection.projectionCenter=new Point (400,0);
//Blank MoveClip
var World:MovieClip = new MovieClip();
World.x = 400;
World.y = 50;
World.z = -700;
World.rotationX = -90;
addChild(World);
//Map Graphic
var Ground:evergreen = new evergreen();
Ground.x = 0;
Ground.y = 0;
World.addChild(Ground);
//Player Graphic
var Player:Test_Objects = new Test_Objects();
Player.x = 0;
Player.y = 0;
Player.rotationX = 90;
Player.scaleX = 0.1;
Player.scaleY = 0.1;
World.addChild(Player);
//Crate Graphics
var Box_X:Array = new Array();
var Box_Y:Array = new Array();
var Box_Rotate_X:Array = new Array();
var Box_Rotate_Z:Array = new Array();
Box_X = [0, 0, -50, 50];
Box_Y = [0, -100, -50, -50];
Box_Rotate_X = [90, 90, 90, 90];
Box_Rotate_Z = [0, 0, 90, 90];
for (var Sides:int = 0; Sides < Box_X.length; Sides++)
{
var Box:Crate = new Crate();
Box.x = Box_X[Sides];
Box.y = Box_Y[Sides];
Box.rotationX = Box_Rotate_X[Sides];
Box.rotationZ = Box_Rotate_Z[Sides];
Box.Distances = 0;
Ground.addChild(Box);
}
//Tree Graphics
var Object_List_X:Array = new Array();
var Object_List_Y:Array = new Array();
Object_List_X = [-200, -150, -125];
Object_List_Y = [-200, -150, -125];
for (var CreateObject:int = 0; CreateObject < Object_List_X.length; CreateObject++)
{
var New_Object:Test_Objects = new Test_Objects();
New_Object.x = Object_List_X[CreateObject];
New_Object.y = Object_List_Y[CreateObject];
New_Object.rotationX = 90;
Ground.addChild(New_Object);
}
//Keyboard Listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, Key_Pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, Key_Released);
//Keys Variables
var Move_Forward:Boolean = false;
var Ground_Rotate:int = 0;
function Key_Pressed (event:KeyboardEvent)
{
//trace(event.keyCode)
if (event.keyCode == 37)
{
Ground_Rotate = 1;
}
if (event.keyCode == 39)
{
Ground_Rotate = -1;
}
if (event.keyCode == 38)
{
Move_Forward = true;
}
//trace(World.rotationY);
}
function Key_Released (event:KeyboardEvent)
{
if ((event.keyCode == 37) || (event.keyCode == 39))
{
Ground_Rotate = 0;
}
if (event.keyCode == 38)
{
Move_Forward = false;
}
}
//Moving Listener
stage.addEventListener(Event.ENTER_FRAME, Move_Ground);
//Distance Variables
var Object_Array_D:Array = new Array();
var DX:Number;
var DY:Number;
var Distance:int;
function Move_Ground (event:Event)
{
//Get Distances
for (var SortID:int = 0; SortID < Ground.numChildren; SortID++)
{
DX = (Ground.getChildAt(SortID).x + Ground.x) - Player.x;
DY = (Ground.getChildAt(SortID).y + Ground.y) - Player.y;
Distance = Math.sqrt(DX * DX + DY * DY);
Object_Array_D[SortID] = Distance;
}
//Sort Objects
SortOBJECTS();
//Update Rotation
Player.rotationZ -= Ground_Rotate;
World.rotationY += Ground_Rotate;
if (World.rotationY < 1)
{
World.rotationY += 360;
}
if (World.rotationY > 360)
{
World.rotationY += -360;
}
//Update Movement
if (Move_Forward)
{
if ((World.rotationY > 0) && (World.rotationY < 91))
{
//var Ground_Rotation:int = int(World.rotationY);
//trace(Ground_Rotation);
Ground.y += (((90 - World.rotationY) * 3) / 90);
Ground.x += ((World.rotationY * 3) / 90);
}
if ((World.rotationY > 90) && (World.rotationY < 181))
{
Ground.y -= (((World.rotationY - 90) * 3) / 90);
Ground.x += (((90 - (World.rotationY - 90)) * 3) / 90);
}
if ((World.rotationY > 180) && (World.rotationY < 271))
{
Ground.x -= (((World.rotationY - 180) * 3) / 90);
Ground.y -= (((90 - (World.rotationY - 180)) * 3) / 90);
}
if ((World.rotationY > 270) && (World.rotationY < 361))
{
Ground.y += (((World.rotationY - 270) * 3) / 90);
Ground.x -= (((90 - (World.rotationY - 270)) * 3) / 90);
}
}
}
function SortOBJECTS ()
{
for (var Sort1:int = 1; Sort1 < Object_Array_D.length; Sort1++)
{
for (var Sort2:int = 1; Sort2 < Object_Array_D.length; Sort2++)
{
if (Object_Array_D[Sort1] < Object_Array_D[Sort2])
{
Ground.setChildIndex(Ground.getChildAt(Sort1), Sort2);
}
}
}
}