A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Moving item placement in an array

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    37

    Moving item placement in an array

    I need to "arrange" an array item by using the up/down arrow keys.
    Each item in the array is represented by a movieclip on the screen, and the idea is to select one, and then move it up and down through the array. For example say you have 7 items in your inventory and want to move item nr 6 ( array[6] ) to the top of the visual representation list ( array[0] ). So it would move through the array and when you hit the top it jumps back down to 7 again. one jump for each keystroke. hope this makes sense.

    Anyone know a simple way to do this? Somehow I am stuck in my head and can't seem figure it out.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    array[6] would be the seventh item, since array[0] is the first.

    Once you've selected an item, on each key press you just need to swap it with one of its neighboring items (or the one on the other end if it's first or last). To swap positions of two items in the array, do something like this:
    Code:
    //swap item at i with item at i+1, or 0 if i+1 == array.length
    function swapDown(i:int):void{
      var temp:* = array[i];
      var j:int = (i+1) % array.length;
      array[i] = array[j];
      array[j] = temp;
    }
    That will swap the items in the array. To reflect that visually is another thing. What was it you needed help with?

  3. #3
    Member
    Join Date
    Sep 2010
    Posts
    37
    Great man, I think that was just what i needed. I'll be able to add to the function what I need to represent it visually. One thing though: what does the "%" mean? I've seen it before but never actually understood what it did.

    Thanks!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    % is the modulo operator. It returns the remainder of dividing the first number by the second. So if i+1 is 7, then 7 divided by 7 has 0 remainder, so it returns 0. modulo is an easy way of wrapping around a limited range of numbers, especially when that range starts at 0.

    http://help.adobe.com/en_US/FlashPla...rs.html#modulo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center