We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › onEnterFrame equivalent
Page Index Toggle Pages: 1
onEnterFrame equivalent (Read 909 times)
onEnterFrame equivalent
Aug 6th, 2007, 1:59am
 
I'm coming to Processing from ActionScript.

*Obligatory ActionScript specific explanation, feel free to skip this part if you already know ActionScript*

When generating animated content in ActionScript, it's quite common to subclass MovieClip and expose a host of event hooks. Example: to call a method on every frame for an instance of a class that extends MovieClip, you simply write the following in the constructor:

onEnterFrame = update;

This will call the 'update' instance method every time the Flash player enters a new frame.

*End ActionScript Explanation*

So my question is: Do Processing classes have an event hook equivalent to ActionScript's 'onEnterFrame'? It's kind of a pain to have to call an 'update()' method for every instance of a class in the main script's 'draw()' method (especially when dealing with hundreds, or even thousands of instances of a class).
Re: onEnterFrame equivalent
Reply #1 - Aug 6th, 2007, 8:26am
 
There is a sort of equivalent. Check out "registerDraw(...)" in the library information on dev.processing.org
Re: onEnterFrame equivalent
Reply #2 - Aug 6th, 2007, 8:45am
 
Awesome. That's exactly what I was looking for. I Found an example here: http://www.processinghacks.com/hacks/registerevents#things-to-watch-out-for

Now how do I 'unregisterDraw' if I delete the object? Is it handled automatically by the GC?
Re: onEnterFrame equivalent
Reply #3 - Aug 6th, 2007, 11:40am
 
Quote:
.. It's kind of a pain to have to call an 'update()' method for every instance ...


i think you're just adding trouble to your sketch. calling 'update()' on your objects is certainly more effective than registering all your objects with PApplet.

that said, there's no unregister:
http://dev.processing.org/bugs/show_bug.cgi?id=312

if you need that kind of behaviour you could just go ahead and build your own little draw / update manager:

Code:

// out of my head, untested:

class UpdateManager
{
Vector objects;

UpdateManager ( PApplet _p )
{
_p.registerDraw( this );
}

void draw ()
{
// loop thru and draw all your objects
}
}

UpdateManager um;

void setup () {
// size(), ....
um = new UpdateManager( this );
// add objects here?
}

void draw()
{
// manager get's called internally,
// objects get called by manager ...
}


best,
F
Re: onEnterFrame equivalent
Reply #4 - Aug 6th, 2007, 10:18pm
 
You could write your own version of registerDraw...

Code:
interface drawable
{
void draw();
}

class myClass implements drawable
{
//normal stuff including a draw method
myClass()
{
}

void draw()
{
rect(random(width),random(height),10,10);
}
}

public class drawManager
{
ArrayList objs;

drawManager()
{
objs=new ArrayList();
registerDraw(this);
}

void addObj(drawable d)
{
objs.add(d);
}

void remObj(drawable d)
{
for(int i=0;i<objs.size();i++)
{
if(objs.get(i)==d)
{
objs.remove(i);
break; //size of objs has now changed, don't want an exception
}
}
}

public void draw()
{
for(int i=0;i<objs.size();i++)
{
((drawable)objs.get(i)).draw();
}
}
}

drawManager dm;
myClass[] things;
void setup()
{
size(300,300);
dm=new drawManager();
things=new myClass[10];
for(int i=0;i<10;i++)
{
things[i]=new myClass();
dm.addObj(things[i]);
}
}

void draw()
{
background(0);
}
Page Index Toggle Pages: 1