PDA

View Full Version : Resorting Array


Subhaus design
10-13-2009, 08:57 PM
Hi,

I'm sure that someone probably asked about this problem, but after 2 days of googling I haven't find anything similar.

I'm making some vertical menu where button (actually mc) on click should tween to the end of menu.

I've try to splice clicked item, and after that to push it back to the same array so it will go to it's end. After that, I've plan to change y position of all items through for loop and using Tweener.

here's my code:

import caurina.transitions.*;

var mits:Array = new Array("about", "biography", "work");
var mitems:Array = new Array();
for(var i:int = 0; i<mits.length; i++) {
var mitem:menuItm = new menuItm();
mitem.id = i;
mitem.itm_txt.text = mits[i];
mitem.itm_txt.wordWrap = false;
mitem.itm_txt.autoSize = TextFieldAutoSize.LEFT;
mitem.x = 10;
mitem.y = 10+i*40;
mitem.name = "mitem"+i;
menus.addChild(mitem);
mitem.buttonMode = true;
mitem.mouseChildren = false;
mitems.push(mitem);
mitem.addEventListener(MouseEvent.CLICK, moveItems);
}

function moveItems(e:MouseEvent):void {
var spliced:Array = mitems.splice(e.target.id, 1);
mitems.push(spliced);

for(var a:int = 0; a<mitems.length; a++) {
var ipsilon:Number = 10+(a*40);
Tweener.addTween(mitems[a], {y:ipsilon, time:1});
trace(mitems[a].id);
}
}


this code works for only few clicks (3 I think). The output look like this:

0
2
undefined

---------------------

2
undefined
undefined

---------------------

2
undefined
## [Tweener] Error: The property 'y' doesn't seem to be a normal object property of [object menuItm] or a registered special property.
undefined

I've try also with eather older Tweener class and TweenLite. Using them I'm not getting error, but the script is acting the same.

What am I doing wrong?

ps. sorry for my bad english. Hope you've understand.:rolleyes:

kingundkong
10-13-2009, 10:02 PM
i think you need to do this: (explenation in the code)

import caurina.transitions.*;

var mits:Array = new Array("about", "biography", "work");
var mitems:Array = new Array();
for(var i:int = 0; i<mits.length; i++) {
var mitem:menuItm = new menuItm();
mitem.id = i;
mitem.itm_txt.text = mits[i];
mitem.itm_txt.wordWrap = false;
mitem.itm_txt.autoSize = TextFieldAutoSize.LEFT;
mitem.x = 10;
mitem.y = 10+i*40;
mitem.name = "mitem"+i;
this.addChild(mitem);
mitem.buttonMode = true;
mitem.mouseChildren = false;
mitems.push(mitem);
mitem.addEventListener(MouseEvent.CLICK, moveItems);
}

function moveItems(e:MouseEvent):void {
var spliced:Array = mitems.splice(e.target.id, 1);
mitems.push(spliced[0]); // push not the whole array but just the menuItm to the mitems array

for(var a:int = 0; a<mitems.length; a++) {
var mc:menuItm=mitems[a];
mc.id=a; // reassing the id variable, cause you getting a new order after mixing
var ipsilon:Number = 10+(a*40);
Tweener.addTween(mitems[a], {y:ipsilon, time:1});
trace(mitems[a].id);
}
}

kingundkong
10-13-2009, 10:04 PM
sorry , i changed this line:

this.addChild(mitem);

just for testing. just change it again to
menus.addChild(mitem);

Subhaus design
10-13-2009, 10:11 PM
wow, this was fast. Thanks man. Your really king! (and kong ofcourse), and you've just made my day!

kingundkong
10-13-2009, 10:21 PM
you are welcome !