AS3 - 如果实例的数量超过X

时间:2014-03-09 11:27:34

标签: actionscript-3 flash

在as3文档中,我正在创建它,以便每个帧在舞台上的随机位置再增加5个心。我希望它显示一个文本框,实例名称为" firstText"如果舞台上有多于10个heartRandom实例。

这是我的代码:

addEventListener(Event.ENTER_FRAME, loop);
function loop(event){
    var h1:Number = Math.random() * 550;
    var h2:Number = Math.random() * 400;
    var h3:Number = Math.random() * 550;
    var h4:Number = Math.random() * 400;
    var h5:Number = Math.random() * 550;
    var h6:Number = Math.random() * 400;
    var h7:Number = Math.random() * 550;
    var h8:Number = Math.random() * 400;
    var h9:Number = Math.random() * 550;
    var h10:Number = Math.random() * 400;
    var hF1:heartRandom = new heartRandom();
    var hF2:heartRandom = new heartRandom();
    var hF3:heartRandom = new heartRandom();
    var hF4:heartRandom = new heartRandom();
    var hF5:heartRandom = new heartRandom();
    addChild(hF1);
    addChild(hF2);
    addChild(hF3);
    addChild(hF4);
    addChild(hF5);
    hF1.x = h1;
        hF1.y = h2;
    hF2.x = h3;
    hF2.y = h4;
    hF3.x = h5;
    hF3.y = h6;
    hF4.x = h7;
    hF4.y = h8;
    hF5.x = h9;
    hF5.y = h10;
}

1 个答案:

答案 0 :(得分:0)

定义心脏数量的最大值,例如 - 200

var maxHearts: uint = 200;
var currentHeartCount: uint;

function loop(e: Event){
    //Your code with hearts generation

    //You are generating 5 hearts every frame
    currentHeartCount += 5;
    if(currentHeartCount >= maxHearts){
        //Display your firstText TextField
        //or create it here, if you don't have it already
        firstText.visible = true;
        //Stop loop?
        removeEventListener(Event.ENTER_FRAME, loop);
    }
}