This content has been marked as final.
Show 9 replies
-
1. Re: How to create many objects listening for one event
kglad Jan 22, 2008 10:51 PM (in response to sneakyimp)you have one class member (td) listening for a color1 event. when that event is dispatched and received handler executes which uses the (last) movieclip square.
if you want all 5 squares to be used in handler, use a for-loop and the getChildByName() method. -
2. Re: How to create many objects listening for one event
sneakyimp Jan 23, 2008 1:34 PM (in response to sneakyimp)td is a class member? This code is on the first frame of an FLA. Does that mean td is a 'class member' of my FLA object (whatever that is) ?
td is an instance of my custom TestDispatcher class which extends EventDispatcher. I was hoping to attach a 'handler' function to each square that I create on the screen so that each one will respond when td dispatches a 'color1' event. I'm imagining a flash game where I spawn hundreds of robots which must all respond to an event like 'SMART_BOMB' or 'PLAYER_ENTERS'.
I don't want all 5 squares to be used in handler so much as i want each of the five squares to have its own handler. if square one gets whacked by the player, then the other 4 squares must be able to continue responding to the event.
-
3. Re: How to create many objects listening for one event
kglad Jan 23, 2008 2:13 PM (in response to sneakyimp)td is an instance of the TestDispatcher class. each square is an instance of the MovieClip class.
if you want each square to have custom methods etc, each square should be a member of your custom class, say the Robot class which would extend the MovieClip class. -
4. Re: How to create many objects listening for one event
sneakyimp Jan 23, 2008 4:18 PM (in response to kglad)Some suggested the code I've attached here which creates something called a Closure. Still trying to grasp what that is.
It looks like Actionscript 3 is pretty fundamentally different from AS2. I don't see any way I can write code on the first frame of my FLA that creates objects (call the nth one myObject_n) and assigns an event listener function which references some property of myObject_n because 'this' is going to refer to the global scope.
Am I correct in thinking that my only options are to:
1) Use this wacky Closure concept
2) Create a separate class for the myObject thingies which creates the event listener function in a constructor -- either by default or based on params passed to the constructor.
Does that sound right? -
5. Re: How to create many objects listening for one event
ntbdy Jan 23, 2008 4:37 PM (in response to sneakyimp)quote:
Originally posted by: sneakyimp
How can I modify this code so that each square object changes its own color?
Hi. The step you've overlooked is to assign your handler method to each square object. So your handler is currently only a member of the main timeline (created and re-created from your code: var handler:Function = ...)
(Note: if an event was dispatched during your loop, it could actually change more than the last square.)
Just for elucidation, the quick fix is to do:
square.handler = function(){...}
and
td.addEventListener('color1', square.handler);
In that way, you're setting up each object correctly, BUT you're doing it from outside itself, in a non-OO way.
But it's better in the long run to subclass MovieClip or Sprite. The initial drawing is done in the constructor. Variability between instances can also be handled via the constructor. Then things seem more obvious and natural as you develop. That's (to me) the biggest advantage of OOD, and fits perfectly in something like a game. -
6. Re: How to create many objects listening for one event
sneakyimp Jan 23, 2008 5:28 PM (in response to ntbdy)Thanks for the input. I agree about OOP and events being especially well-suited to gaming.
You are mistaken about using square.handler=function() {...}. I tried that first. The code below behaves identically to my original post:
-
7. How to create many objects listening for one event
ntbdy Jan 23, 2008 8:00 PM (in response to sneakyimp)quote:
well, the event listening in Flash could be better, IMO - using an object as a listener rather than a function. Plus Flash could use Threading, rather than just Timer. But that's all beside the point for now.
Originally posted by: sneakyimp
Thanks for the input. I agree about OOP and events being especially well-suited to gaming.
quote:
You are mistaken about using square.handler=function() {...}. I tried that first. The code below behaves identically to my original post:
nope, not mistaken :)
But your initial instinct with the basic concept was good, although your code has logical errors in use of the 'square' handle. Below is my minimal code, comprising what we'd call "proof of concept". The listener is assigned via an object.function reference.
-
8. Re: How to create many objects listening for one event
ntbdy Jan 23, 2008 8:05 PM (in response to sneakyimp)quote:
Originally posted by: sneakyimp
square.graphics.clear();
note that refers to a different object than:
graphics.clear();
-
9. How to create many objects listening for one event
sneakyimp Jan 23, 2008 9:38 PM (in response to sneakyimp)graphics.clear in the context of that function handler in my code would refer not to the squares but to the global scope, wouldn't it? In which case you'd be clearing all the graphics from your top level movie. I tried changing 'square.graphics' to just 'graphics' in my handler and my squares just remained the original steady black.
Your code doesn't use a loop and therefore isn't re-using a variable. You've escaped my 'logical errors' through brute force. (BTW, I think my code would work in AS2 if I used the old-school draw methods). Suppose you had to create 100 objects? Would you want to manually instantiate each one, typing that function over and over again? You'd have 500 lines of highly redundant code!
Maybe try your example using a loop instead? I think you'll find it's pretty tough to sneak any vars like 'i' or 'square' from the global scope into the handler--the handler refers to the living, breathing variable rather than it's value at the time the handler was instantiated. Furthermore, you can't sneak any information about the square into your handler via the event object because the event object is created in a totally different place.



