views:

48

answers:

1

how exactly you send an object using the "every" loop in as3?

var myFunction:Function = function(obj:Object):void {
 obj.height=400*Math.random();
 obj.width=400*Math.random();
}

function onMouseMove(event:MouseEvent):void {
    mylist.every(myFunction(this));
}

i have also tried using the:

function onMouseMove(event:MouseEvent):void {
    mylist.every(myFunction, me);
}

which is in the AS3docs but it doesn't work

A: 

I think you have a bigger problem, but first I'll answer your question as you asked it:

Your callback function needs to take three parameters and return a value.

callback:Function— The function to run on each item in the array. This function can contain a simple comparison (for example, item < 20) or a more complex operation, and is invoked with three arguments; the value of an item, the index of an item, and the Array object:

function callback(item:*, index:int, array:Array):Boolean;

source: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Array.html#every()


What you're trying to do is resize every object in your list. The every function isn't appropriate for this. You want forEach:

var myFunction:Function = function(obj:*, index:int, list:*):void {
    obj.height=400*Math.random();
    obj.width=400*Math.random();
}

function onMouseMove(event:MouseEvent):void {
    mylist.forEach(myFunction);
}

Note that you can use any function, not just one specified as a var

function myFunction(obj:*, index:int, list:*):void {
    obj.height=400*Math.random();
    obj.width=400*Math.random();
}

function onMouseMove(event:MouseEvent):void {
    mylist.forEach(myFunction);
}

Or, you could even write the function in-line:

function onMouseMove(event:MouseEvent):void {
    mylist.forEach(function(obj:*, index:int, list:*):void {
        obj.height=400*Math.random();
        obj.width=400*Math.random();
    });
}

As a final point... Note that this is also possible:

function onMouseMove(event:MouseEvent):void {
    for each (var obj:* in mylist) {
        obj.height=400*Math.random();
        obj.width=400*Math.random();
    }
}
Gunslinger47