我正在制作一个随机生成的地板,但是当影片剪辑在其上行走时,它的行为似乎就像一块巨大的地板,影片剪辑在其上方行走,而不是跟随图像,这是代码:
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
//variables
var h:Number = 360;
var floor:MovieClip=new MovieClip;
var guy:MovieClip=new MovieClip;
var gravity=10;
guy.graphics.beginFill(000000);
guy.graphics.drawRect(100,100,15,25);
guy.graphics.endFill();
addChild(guy);//guy creation
//floor generation
for (var i:int=0; i<600; i++)
{
if (h==360)
{
if (Math.random() <= 0.75)
{
floor.graphics.beginFill(000000);
floor.graphics.drawRect(i,h,40,40);
floor.graphics.endFill();
addChild(floor);
i += 39;
}
else
{
i += 39;
}
if (i>550)
{
h = 320;
i = 0;
}
}
if (h==320)
{
if (Math.random() <= 0.75)
{
floor.graphics.beginFill(000000);
floor.graphics.drawRect(i,h,40,40);
floor.graphics.endFill();
addChild(floor);
i += 39;
}
else
{
i += 39;
}
}
}
//listeners doesnt matter
addEventListener(Event.ENTER_FRAME,misc);
addEventListener(KeyboardEvent.KEY_UP,checkup);
addEventListener(KeyboardEvent.KEY_DOWN,checkdown);
//functions
var leftarrow:Boolean=false;
var rightarrow:Boolean=false;
function checkdown(a:KeyboardEvent)
{
if(a.keyCode==37)leftarrow=true;
if(a.keyCode==39)rightarrow=true;
}
function checkup(a:KeyboardEvent)
{
if(a.keyCode==37)leftarrow=false;
if(a.keyCode==39)rightarrow=false;
}
function misc(a:Event)
{
guy.y+=gravity;
if(guy.hitTestObject(floor))
{
gravity=0;
}else{gravity=10;}
if(leftarrow)guy.x-=5;
if(rightarrow)guy.x+=5;
}
提前致谢。