如何创建阶梯效果

时间:2013-12-02 19:58:59

标签: actionscript-3

所以我试图不断在舞台上添加元素,并将这些元素添加到特定的X和Y. 我的意思是什么,我想得到的是一个阶梯效应(上下):

让我们说第一元素 100 pix stage.Y

第二元素: Y 是第一个元素。第二个元素。高度及其 X 位置是第一个元素。+第二个元素.width(在我们可以看到第一个元素的整个主体之后立即出现)

3rd 元素:Y是第2个元素.y + 3nd element.height,它的X位置是第2个元素.x + 3nd element.width(在我们看到整个身体之后立即出现第2个元素)

之前的最后一个元素楼梯效果下降将是stage.stageheight - 100

像这张照片一样 enter image description here

但是我不知道该怎么做(我知道我必须把它放在for循环中并且在那里有一个if语句,每次检查上下边界(stage.stageHeight - 100和stage。 Y + 100)但我无法弄明白)

到目前为止,我所拥有的是

private var itemsToAnimate:Array = []
    private var magnetBuff:Boolean = false;
    private var block:Block = new Block();

    private var stage_H:int = stage.stageHeight;
    private var block_H:int = block.height;


    public function AB_Main()
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(evt:Event)
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
        this.addEventListener(Event.ENTER_FRAME, onEveryFrame);
    }

    private function onEveryFrame(ev:Event):void
    {
        createItems()
        animateItems();
    }

    private function animateItems():void
    {
        var itemToTrack:Block;

        for(var i:uint = 0; i < itemsToAnimate.length; i++)
        {
            itemToTrack = itemsToAnimate[i];
            itemToTrack.x -= 3;

            if(itemToTrack.x < -50)
            {
                itemsToAnimate.splice(i, 1);
                this.removeChild(itemToTrack);
            }
        }

    }


    private function createItems():void
    {
        if(Math.random() > 0.75)
        {
            var itemToTrack:Block = new Block();
            itemToTrack.x = stage.stageWidth - 50;
            itemToTrack.y = int( Math.random() * stage.stageHeight) 
            this.addChild(itemToTrack);
            itemsToAnimate.push(itemToTrack);
        }
    }

这让我得到了一个像图片中那样的随机定位块

enter image description here

2 个答案:

答案 0 :(得分:1)

这里有一个值,它将沿着x轴不断增加,然后是另一个沿y轴增加或减少的值,当你触摸顶部或底部时,它会交替变化。这很简单:

var increaseY:Boolean = true;     // Whether we are moving up or down the y axis.
var position:Point = new Point(); // Position to place next item.
var move:int = 10;                // How much we move along each axis.
var margin:int = 100;             // Distance from top or bottom before alternating.

for each(var i:Sprite in itemsToAnimate)
{
    i.x = position.x;
    i.y = position.y;

    if(increaseY) position.y += move;
    else position.y -= move;

    position.x += move;

    if(position.y < margin || position.y > stage.stageHeight - margin)
    {
        // Reverse direction.
        increaseY = !increaseY;
    }
}
可以调整

move以改变路径上每个项目之间的距离。

答案 1 :(得分:1)

我认为你可以做这样的事情(在没有测试的情况下从头开始写它,我希望它能正常运行):

function draw(min:Number, max:Number, horSpacing:Number, vertSpacing:Number):void {
    var increasing:Boolean = true;
    var lastY:Number = min + vertSpacing;
    for(var i:int=0; i<100; i++) {
        var c:Circle = new Circle(); //where Circle would be your dot with registration point in center
        c.x = 20 + i * horSpacing; //hardcoded margin
        c.y = (increasing) ? lastY - vertSpacing : lastY + vertSpacing;
        lastY = c.y;
        addChild(c);
        if(c.y <= max) increasing = false;
        if(c.y >= min) increasing = true;
    }
}

draw(stage.stageHeight - 100, 100, 20, 20);