PDA

View Full Version : enterFrame function with external argument


sooon
02-13-2008, 04:11 PM
hi guys,

how can I code 'onEnterFrame' function with dynamic argument?

what i want is:

addEventListener(Event.ENTER_FRAME,onFrame)

function onFrame(event:Event,arg1:Object,arg2:Object)
{
//do something with arg1 and arg2...
}


I know this is incorrect. But I jsut cannot find a right way to do this?

any suggestion??

thanks!

panel
02-13-2008, 06:23 PM
You cannot.

What exactly you wanna achieve?

theelephantwords
02-13-2008, 06:30 PM
addEventListener(Event.ENTER_FRAME, function() { onFrame({a:9,b:10}, {c:90, d:'sdf'}); })

function onFrame(arg1:Object,arg2:Object)
{
//do something with arg1 and arg2...
}


you can't remove the listener though, so it sucks. but it'll work. i don't recommend doing this at all. do it this way:


var obj1:Object = new Object();
obj1.a = 9;
obj1.b = 10;
var obj2:Object = new Object();
obj2.c = 90;
obj2.d = 'sdf';
addEventListener(Event.ENTER_FRAME, onFrame)

function onFrame(e:Event)
{
//do something with obj1 and obj2...
}

sooon
02-14-2008, 11:24 AM
hi guys,


var obj1:Object = new Object();
obj1.a = 9;
obj1.b = 10;
var obj2:Object = new Object();
obj2.c = 90;
obj2.d = 'sdf';
addEventListener(Event.ENTER_FRAME, onFrame)
function onFrame(e:Event) { //do something with obj1 and obj2... }

hi theelephantwords, I cannot do this because my object is dynamic fromexternal XML.


i found a simplest solutions:


//say if I want to move a mc...

function mMove(m:MovieClip,X:Number,Y:Number):void
{
var onFrame:Function = function():void
{
m.x += X;
m.y += Y;
}
addEventListener(Event.ENTER_FRAME,onFrame);
}
//so now i can just put in any argument...

mMove(mc,1,5); // mc will move...


this is the simplest onEnterFrame replacement. but with a few issue:

the function somehow works like variable, so in order to use it in listener, you have to declare it before the listener.

if you wanted to remove listener you have to removeEventListener from within the same function. In above example will be within "onFrame" it self.

ya I know is not perfect but it solve my problem :D

and thanks for replying that can be good reference too...

panel
02-14-2008, 12:36 PM
works...but it feels like it isn't correct solution in OOP way ;/ (and we all know that bad habbits stay for long)