来自Array的As3 addChild

时间:2015-03-26 15:51:21

标签: arrays actionscript-3 flash

我想添加一个我加载到Array的子项 我有Math.random函数返回Number并应触发addChild 后来我想把它拼接成另一个Array

var ground1: MovieClip = new Ground_01;
var ground2: MovieClip = new Ground_02;
var ground3: MovieClip = new Ground_03;
var groundArray: Array = new Array();
var groundMoveArray: Array = new Array();

public function loadGroundArray() {
        groundArray.push(ground1);
        groundArray.push(ground2);
        groundArray.push(ground3);
    }

public function randomGround(minNum: Number, maxNum: Number): Number {
        minNum = 0;
        maxNum = groundArray.length;
        return (Math.ceil(Math.random() * (0 + maxNum)));
    }

1 个答案:

答案 0 :(得分:0)

好吧,你几乎就在那里......你只需要调整你的随机方法来实际添加剪辑......

 var ground1: MovieClip = new Ground_01();
 var ground2: MovieClip = new Ground_02();
 var ground3: MovieClip = new Ground_03();

 var groundArray: Array = new Array();
 var groundMoveArray: Array = new Array();

 public function loadGroundArray() {
    groundArray.push(ground1);
    groundArray.push(ground2);
    groundArray.push(ground3);
 }

 public function addRandomGroundToStage(): void {
     var randomGroundIndex:int = Math.round(Math.random*(groundArray.length-1));
     addChild(groundArray[randomGroundIndex]);
 }