I want to set the items order that are in an array Collection and bind with a combo-box.
here is my code
[Bindable]private var langList:ArrayCollection = new ArrayCollection([{label:"Englis"},{label:"Urdu"},{label:"Arabic"},{label:"Spanish"}]);
protected function cbm_creationCompleteHandler(event:FlexEvent):void
{
for(var i:int =0; i< langList.length; i++)
{
if(langList[i].label == 'Urdu')
{
cbm.setChildIndex(cbm.getChildAt(i), 0);
break;
}
}
}
<s:ComboBox id="cbm" x="258" y="113" dataProvider="{langList}" creationComplete="cbm_creationCompleteHandler(event)"/>
when i try to run it the following exception throw by the compiler
RangeError: Error #2006: The supplied index is out of bounds.
Be careful, ComboBox::getChildAt won't return the list item, you shouldn't access the children of a Flex a component, this is a skin matter.
If you want to change the elements' order you must do it in the dataProvider, this is a data matter.
for(var i:int =0; i< langList.length; i++)
{
var item:Object = langList[i];
if(item.label == 'Urdu')
{
langList.removeItemAt(i);
langList.addItemAt(item, 0);
break;
}
}
Since your langList is Bindable, the combobox will be automatically updated.
Related
I'm really new to AS3 and I was wondering how I can apply a function to all instances of a class I'll show you guys what I have, is there anything I can do?
var cloud:Cloud;
for (i=0; i<5; i++)
{
cloud = new Cloud();
addChild(cloud);
}
this makes many instances of the Cloud class.
Later on I want to execute a function on all of these instances at once, how would I do that?
Store them in an Array or Vector:
var clouds:Vector.<Cloud> = new <Cloud>[];
for(var i:int = 0; i < 5; i++)
{
var cloud:Cloud = new Cloud();
clouds.push(cloud);
addChild(cloud);
}
Then iterate over that list and call a function on each item:
for each(var cloud:Cloud in clouds)
{
cloud.do();
}
I have a feathersui list component and I was wondering if there is a way to unshift a new item to the beginning of the list IF it doesn't current exist in the list. What I would like to do is something like the below (most is already done except the 'if it doesn't exist part):
for(var i:int = 0; i < resultsArray.length; i++)
{
if(resultsArray[i].notification_id DOES NOT EXIST IN itemListArray)
{
this.itemListArray.unshift({notificationID: resultsArray[i].notification_id,
notificationText: resultsArray[i].message,
notificationType: resultsArray[i].notification_type,
notificationDest: resultsArray[i].destination,
notificationDestID: resultsArray[i].destination_id,
dateAdded: resultsArray[i].date_added});
}
}
I know PHP has in_array so hoping someone knows an AS3 equivalent.
Thanks
Define a dictionary before the loop to get all the notificationID which itemListArray contains.
Array has a indexOf function, you can search item by that function. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#indexOf()
In your case, the array isn't directly contains the notification_id, so it won't work
var dict:Dictionary = new Dictionary();
for each (var obj:Object in itemListArray) {
dict[obj.notification_id] = true;
}
for(var i:int = 0; i < resultsArray.length; i++)
{
if(!dict[resultsArray[i].notification_id])
{
dict[resultsArray[i].notification_id] = true;
this.itemListArray.unshift({notificationID: resultsArray[i].notification_id}, notificationText: resultsArray[i].message,
notificationType: resultsArray[i].notification_type,
notificationDest: resultsArray[i].destination,
notificationDestID: resultsArray[i].destination_id,
dateAdded: resultsArray[i].date_added}););
}
}
Look at indexOf -> docs
it will return the position of the item, if it doesnt exist it will return -1
if(itemListArray.indexOf(resultsArray[i].notification_id) == -1){
// item i doesnt exist
}
public function resort():void {
while (tickets.numChildren > 0) {
tickets.removeChildAt(tickets.numChildren - 1);
}
for(var i:int = 0; i < ticketsBought.length; i++)
{
var t:TicketCard = ticketsBought[i] as TicketCard;
tickets.addChild(t);
}
}
after this function executes, the tickets are blinking visually, but I don't want that, is there a way to sort objects in the stage without blinking?
I think you can use the setChildIndex method of the DisplayObjectContainer class (see documentation) to reorder your objects without removing and then re-adding them.
Unfortunately I'm not in a position to test this at the moment, but I think the following should work:
public function resort():void {
for(var i:int = 0; i < ticketsBought.length; i++)
{
var t:TicketCard = ticketsBought[i] as TicketCard;
tickets.setChildIndex(t, i);
}
}
I'm making a game in flash and am using arrays to dynamically create items and place them inventory. LongSword is a MovieClip. I place the movieclip in the array like so:
function buyitem1(e:Event):void
{
if(Store.itemslot.length < 6 && Store.currentMenu == 1 &&score >= 450)
{
Store.itemslot.push(new LongSword);
}
}
Now I'm trying to remove the movieclip from the stage when the LongSword is "sold". How can i remove this longsword? I've tried:
for(var i:int = 0; i < Store.itemslot.length; i++)
{
if(Store.itemslot[i] == LongSword)
{
stage.removeChild(Store.itemslot[0]);
}
}
Ive also tried:
for(var i:int = 0; i < Store.itemslot.length; i++)
{
if(Store.itemslot[i] == new LongSword)
{
stage.removeChild(Store.itemslot);
}
}
and several variations. any ideas?
Try something like:
for each(var i:MovieClip in Store.itemslot)
{
if(i is Longsword)
{
var n:int = Store.itemslot.indexOf(i);
Store.itemslot.splice(n, 1);
if(i.parent) i.parent.removeChild(i);
break; // Only remove one Longsword.
}
}
If there are multiple instances of Longsword in the array, you may want to keep a reference to each instance somewhere for better comparison.
i have a few filters on a sprite. on mouse over i would like to access one of the filters in the filters array, but i'm having a bit of trouble trying to accomplish this.
mySprite.filters = [new DropShadowFilter(), new GlowFilter(), new BlurFilter()];
mySprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEventHandler);
function mouseOverEventHandler(evt:MouseEvent)
{
//obtain indexOf the GlowFilter
trace(evt.currentTarget.filters[evt.currentTarget.filters.indexOf([Object GlowFilter])]));
}
the above code doesn't work. what's the proper way to get the index of a specific filter in a filters array?
If I understand correctly, you're essentially trying to do this:
var index:int = evt.currentTarget.filters.indexOf([Object GlowFilter]);
The bracketed part is not valid Actionscript it shouldn't even compile. What you need to do is to iterate over the filters and test them yourself since there's no way to search for a specific class with indexOf.
Try this instead:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
}
Also, if you're going to fiddle with the filters in the array Flash won't accept in-place modifications, so you need to re-set the array once you've changed it:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
if (!glowFilter) return;
glowFilter.blurX = 10;
var filters:Array = evt.target.filters;
filters[i] = glowFilter;
evt.target.filters = filters;
}