开发者

AS3: How can I construct this Array problem?

开发者 https://www.devze.com 2023-04-06 12:25 出处:网络
I want to do something like this with AS3, but I\'m new to AS3 and I\'m not sure how to go about the coding.

I want to do something like this with AS3, but I'm new to AS3 and I'm not sure how to go about the coding.

What I want to do is there are 4 MCs in an array, and I need them to move one MC over every time I click on one. And I need them to loop.

So I'll need something like: if on click #1 - 4, 1, 2, 3 if on click #2 - 1, 2, 3, 4 if on click #3 - 2, 3, 4, 1 if on click #4 - 3, 4, 1, 2

How would I be able to do this in an array? I can't se开发者_JAVA百科em to get my head wrapped around it.


If you want to rotate an array you can use a combination of shift()/push() or pop()/unshift(), depending on which direction you want to rotate. In your case shift/push will do.

This basic code illustrate how you can do that:

var mc1:MovieClip,mc2:MovieClip,mc3:MovieClip,mc4:MovieClip;

var mcs:Array = [mc1, mc2, mc3, mc4];

function rotateArray(els:Array):MovieClip
{
    var el:MovieClip = els.shift();

    els.push(el);

    return el;

}

var nextEl:MovieClip = rotateArray(mcs);

Obviously, you also need to initialise and attach a click handler to your movie clips, here mc1, mc2 etc are just for illustration.

Check the AS3 Array docs for info on shift, push, pop, etc

To rotate left-wise: shift() will pull the first element of an array and return it (while removing it from the array) push() will put it back at the end of the array

To rotate right-wise: pop() will pull the last element of an array and return it (also removing it from the array) unshift() will put it back at the beginning of the array.


Below is the code to do what you asked.
The key piece is movieclips.push(movieclips.shift());

movieclips.shift() removes the first element from the array.
movieclips.push() adds the element to the end of the array.
Also check out the pop and unshift functions of the array.

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public var mc1:MovieClip = new MovieClip();
    public var mc2:MovieClip = new MovieClip();
    public var mc3:MovieClip = new MovieClip();
    public var mc4:MovieClip = new MovieClip();

    public var movieclips:Array = new Array(mc4,mc1,mc2,mc3);

    public class Demo extends MovieClip
    {
        public function Demo()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        public function init():void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            addChild(mc1);
            addChild(mc2);
            addChild(mc3);
            addChild(mc4);
            mc1.addEventListener(MouseEvent.CLICK, click);
            mc2.addEventListener(MouseEvent.CLICK, click);
            mc3.addEventListener(MouseEvent.CLICK, click);
            mc4.addEventListener(MouseEvent.CLICK, click);
            // Left as exercise: Place and position mc shape, image or bitmap
        }

        public function click(e:MouseEvent):void
        {
            movieclips.push(movieclips.shift());
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消