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;
}
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 have a couple of display objects moving around the screen, the all belong to the same Vector.
I would like to create a function that lets me pick an object with the lowest x value at the moment the function is called.
This is quite easy and if you're into programming, you should be able to do it yourself, but if you're a beginner, I'll give you some code:
var vec:Vector.<DisplayObject> = new Vector.<DisplayObject>();
function getLowestXObject():DisplayObject
{
var minX:DisplayObject = vec[0];
for(var i:int = 1; i < vec.length; i++)
{
if(minX.x > vec[i].x)
{
minX = vec[i];
}
}
return minX;
}
Class ShootGame implements IGame
{
}
//ShootGame Vector
var shootGames:Vector.<ShootGame> = new Vector.<ShootGame>();
for(i=0;i<20;i++)
{
shootGames.push(new ShootGame());
}
var igames:Vector.<IGame> = //What is the Simplest method to convert ShootGame to IGame Vector
//I am doing like
igames = new Vector.<IGame>();
for(i=0;i < shootGames.length;i++)
{
igames.push(shootGames[i]);
}
There is a more convenient way to do that.
var igames:Vector.<IGame> = Vector.<IGame>(shootGames);
Notice that when you use "Vector.< IGame >(shootgames)" you are not making a typecast, instead you are creating a new Vector instance and populating it with the content of the "shootGames" Vector.
For more detailed information, go here.
Sample code:
var shootGames:Vector.<ShootGame> = new Vector.<ShootGame>();
for (var i:int = 0; i < 20; i++) {
shootGames.push(new ShootGame());
}
var igames:Vector.<IGame> = Vector.<IGame>(shootGames);
trace("shootGames.length", shootGames.length); //20
trace("igames.length", igames.length); //20