随机移动剪辑没有循环?

时间:2017-07-11 15:40:04

标签: actionscript-3 air

我创建了一些MovieClips并将所有这些放在舞台上。我试图移动一个随机MovieClip,只使用它一次,但同时我需要知道它是哪一个,因为它会与其他补间效果冲突。有没有一种简单的方法可以做到这一点?

这是我尝试过的:

var biryukseklev1, ikiyukseklev1, ucyukseklev1, dortyukseklev1, besyukseklev1:int;
var assignavalue1, assignavalue2:int;

stage.addEventListener(Event.ENTER_FRAME,survivordondurlev6);
function survivordondurlev6(e:Event) {
    if (biryerlessurlev1 == 0) {
        biryukseklev1  = 764;//36
    } else if (biryerlessurlev1 == 1) {
        biryukseklev1 = 680;//120
    } else if (biryerlessurlev1 == 2) {
        biryukseklev1 = 596;//204
    } else if (biryerlessurlev1 == 3) {
        biryukseklev1 = 512;//288
    } else if (biryerlessurlev1 == 4) {
        biryukseklev1 = 428;//372
    }

    if (assignavalue1 == 0 && rabbitstatus.text.length < 2) { //Make sure to not used before
        var my:Tween = new Tween(rabbit, "y", Back.easeInOut, rabbit.y, rabbit.y -biryukseklev1, 3, true);
        rabbitstatus.text = "okfull";
    } else if((assignavalue1 == 1 && birdstatus.text.length < 2){ //Make sure to not used before
        var mys:Tween = new Tween(bird, "y", Back.easeInOut, bird.y, bird.y -biryukseklev1, 3, true);
        birdstatus.text = "okfull";
    }
}

1 个答案:

答案 0 :(得分:0)

如果你想跟踪你动画的那些片段,那么你必须有一些方法来迭代它们。最简单的列表是Array,但是如何将它们标记为“动画”取决于您。如果你从未计划再次为它们制作动画,那么只需删除你已制作动画的动画,然后将其余动画留待以后即可使用。

下面是示例代码,您应该能够在新场景中运行。您应该可以通过一些小编辑将其应用于您的需求。

import fl.transitions.Tween;
import fl.transitions.easing.*;

// We'll keep track of which blocks still need to be animated by including them in this list
// Once we animate a block, we remove it from the list.
var pool:Array = [];

// Populate the stage with blocks.
for (var i:int = 0; i < 30; i++) {
    var block:Sprite = createBlock();
    addChild(block);    
    block.y = 50;
    if (i > 0) { block.x = block.width * i; }
    pool.push(block);
}

// Create an interactive button.
var txt:TextField = new TextField();
txt.text = "Do something";
addChild(txt);
txt.addEventListener("click", animateNext);

function createBlock():Sprite {
    // Creates a single, randomly colored block.
    var block:Sprite = new Sprite();
    block.graphics.beginFill(random(0x000000, 0xFFFFFF));
    block.graphics.drawRect(0,0,16,16);
    block.graphics.endFill();
    return block;
}

function random(low:Number, high:Number):Number {
    // Returns a random number between the low and high numbers.
    return Math.floor(Math.random() * (1+high-low)) + low;
}

function animateNext(e:Event):void {
    // Do this 5 times.
    for (var i:int = 0; i < 5; i++) {
        // As long as we have blocks in the pool...
        if (pool.length > 0) {
            // Pick a random block from the pool
            var index:int = random(0, pool.length-1);

            // Animate it
            new Tween(pool[index], "y", Back.easeInOut, pool[index].y, pool[index].y + 50, 3, true);

            // Remove it from the pool so it isn't picked again
            pool.splice(index, 1);
        } else {
            // Otherwise, break the loop.
            break;
        }
    }
}