Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 试着做一个游戏,让我躲避从天上掉下来的陨石。as3_Actionscript 3 - Fatal编程技术网

Actionscript 3 试着做一个游戏,让我躲避从天上掉下来的陨石。as3

Actionscript 3 试着做一个游戏,让我躲避从天上掉下来的陨石。as3,actionscript-3,Actionscript 3,我想做一个陨石从天而降的游戏。。。到目前为止,我已经有1个正在坠落和消失,但它没有循环。我如何使多颗陨石落在不同的位置,并保持循环这是我的代码到目前为止 var randomX:Number = Math.random() * 400; test_mc.x = randomX; test_mc.y = 0; var speed:Number = 10; test_mc.addEventListener(Event.ENTER_FRAME, moveDown); function move

我想做一个陨石从天而降的游戏。。。到目前为止,我已经有1个正在坠落和消失,但它没有循环。我如何使多颗陨石落在不同的位置,并保持循环这是我的代码到目前为止

var randomX:Number = Math.random() * 400;

test_mc.x = randomX;
test_mc.y = 0;

var speed:Number = 10;

test_mc.addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void
{
  e.target.y += speed; 

  if(e.target.y >= 500
  )
  {
    test_mc.removeEventListener(Event.ENTER_FRAME, moveDown);
  }
}

使用数组或向量(哪种更好)(甚至你可以创建自己的链表类,或循环通过stage/sprite的子级)访问所有现有的陨石。循环遍历每一帧中的数组/向量/列表,使它们移动并处理冲突。计算帧数或使用Math.random()在新陨石出现之前暂停。

不要有一个测试对象,而是定义一个数组和一个计数器变量来跟踪何时应添加新陨石:

var meteorites:Array = new Array();
var counter:int = 0;
不要将事件监听器添加到单个陨石中,而是将事件监听器添加到舞台,让它触发游戏循环:

stage.addEventListener(Event.ENTER_FRAME, gameLoop);  

function gameLoop(e:Event):void {
    counter ++;
    if (counter>=10) { // this will add a new Meteorite every 10 frames
        counter = 0;
        meteorites.push(new Meteorite());
        addChild(meteorites[meteorites.length-1]);
        // you could add code here to position the new Meteorite (meteorites[meteorites.length-1]) randomly in the X direction
    }
    if (meteorites.length>0) {
        for (var loop:int=meteorites.length-1;loop>=0;loop--) {
            meteorites[loop].y += speed;
            if (meteorites[loop].y>500) {
                removeChild(meteorites[loop]);
                meteorites.splice(loop, 1) // this removes the meteorite at index [loop] from the Array
            }
        }
    }
}
为此,您必须启用meteorite MovieClip For actionscript(在库属性/高级面板中),并为其指定meteorite类名

编辑 我添加了“addChild”和“removeChild”调用,这是显示陨石所必需的。此外,你将不需要任何陨石定位在你的舞台上的闪光。此代码将为您在游戏中添加它们。

您还可以创建“智能”陨石类,当y>=500时,该类陨石将自行移动并自行毁灭。只要不时地制作它们,然后放在舞台上/精灵上就行了。