我正试图在Adobe Flash中构建一个类似Flappy Bird的游戏,使用Action Script 3作为学校项目。我使用一个管道对象数组来创建每个级别的管道数量(在这个阶段,我的代码是一个级别/阶段)。
但我有一个问题,在我创建了管道数组并将每个管道对象添加到舞台后,我尝试使用其他函数来移动管道,迭代数组并将x维值更改为每个管道对象,但它不起作用。
下面是我的代码:
import flash.events.MouseEvent;
stop();
Bird_mc.stop();
//array of pipes;
var pipeArray:Vector.<Pipe > = new Vector.<Pipe > ;
var birdVelocity:int = 0;
var stageGravity:int = 2;
stage.addEventListener(Event.ENTER_FRAME,birdFall);
stage.addEventListener(Event.ENTER_FRAME,createPipesAndLines);
stage.addEventListener(Event.ENTER_FRAME,movePipesAndLines);
stage.addEventListener(MouseEvent.CLICK,birdFly);
function birdFall(event:Event):void
{
Bird_mc.y += birdVelocity;
birdVelocity += stageGravity;
}
function birdFly(event:MouseEvent):void
{
birdVelocity = -16;
Bird_mc.play();
}
/*Move Pipes and Lines to the left - TO BE MADE*/
function createPipesAndLines(event:Event):void
{
for (var i:int = 0; i<10; i++)
{ //use of of-else to separate the pipes up and down and rotate em
if (i%2==0)
{
pipeArray.push(new Pipe());
addChild(pipeArray[i]);
pipeArray[i].x = i * 250;
pipeArray[i].y = 50;
}
else
{
var tempPipe:Pipe = new Pipe();
tempPipe.rotation = 180;
pipeArray.push(tempPipe);
addChild(pipeArray[i]);
pipeArray[i].x = i * 300;
pipeArray[i].y = 400;
}
}
}
//move the pipes to left
function movePipesAndLines(event:Event):void
{
for (var i:int = 0; i<10; i++)
{
pipeArray[i].x -= 0.5;
}
}发布于 2014-10-23 08:21:13
首先,我建议使用单个事件函数来调用所有其他需要更新的函数。
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
birdFall();
birdFly();
movePipesAndLines();
}其次,你并不是真的想从游戏一开始就添加所有管道。我可以想象,随着屏幕的移动,Flappy Bird会附加新的管道对象。这样做的效率要高得多。
除此之外,代码看起来还不错。但是,我想看看管道对象中的内容。也许Pipe.x是一个int,而不是Number
https://stackoverflow.com/questions/26518952
复制相似问题