AS3: How can I construct this Array problem?
-
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 seem to get my head wrapped around it.
-
Answer:
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 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html?filter_flash=cs5&filter_flashplayer=10.2 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.
muudless at Stack Overflow Visit the source
Other answers
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()); } } }
eLouai
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How can i get rid of my constant sinus problem?Best solution by Yahoo! Answers
- How can I remove a casset tape from my car player? It is a 2003 Hyundai Sonota. Does anyone else this problem?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.