Re: [Flashcoders] Listeners (was no subject)
Yes, the dispatcher and the listener are both in the same call stack always, but I think what we were discussing is whether the caller (the original caller, the one that calls the method that will eventually dispatch the event) is in the same callstack as the listener / event handler. That's not always the case, as it depends on whether the event is dispatched synchronously or not. Cheers Juan Pablo Califano 2010/7/28 Henrik Andersson he...@henke37.cjb.net Kerry Thompson wrote: I agree with everything you say, up to that point. There is a fundamental difference in the way callbacks and messages work. A callback puts the caller on the call stack, and control will eventually return to that calling method. A message does not put the sender on the call stack. Have you ever glanced at the callstack in the debugger while in a listener? It is very clearly the same callstack both for the dispatcher and the listener. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Hi all; I hesitated to introduce new questions to this list concerning my matter in this same thread since it has taken such interesting turns into quarks and bosons and what not, but then I thought it best if anyone were to google it to be continuous. I am in the process of joining Flash_Tiger but await moderator approval. In the interim, perhaps you could continue to help me a bit here. I am attempting the more complicated approach of custom events. I googled and the information I got was not entirely luminous (at my beginning level of understanding). Here is a slight revision of my previous code: function RotateGears() { var path:String = new String(); path = gearsPaths[displayGearsCounter]; var req:URLRequest = new URLRequest(path); var loader:Loader = new Loader(); loader.load(req); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadGearData.displayGearsCounter); } function RotateGearsLoaded(e:Event):void { var loaderInfo:LoaderInfo = e.target as LoaderInfo; var displayObject:DisplayObject = loaderInfo.content; displayObject.width = gearWidths[displayGearsCounter]; displayObject.height = gearHeights[displayGearsCounter]; //displayObject.x = - gearWidths[displayGearsCounter]; //displayObject.y = - gearWidths[displayGearsCounter]; displayObject.x = 0; displayObject.y = 0; trace(displayGearsCounter); parent_container.addChild(displayObject); parent_container.x = 0; parent_container.y = 0; parent_container.alpha = 1; addChild(parent_container); var myTimeline:TimelineLite = new TimelineLite({useFrames:true}); myTimeline.append(new TweenMax(parent_container, 1, {shortRotation:{rotation:gearAngles[displayGearsCounter]}})); } } I also import the following script, which is a slight tweak of something I googled: package { import flash.events.Event; public class LoadGearData extends flash.events.Event { public static const displayGearsCounter:int = 0; private var command:String; public function LoadGearData (command:String ) { super(displayGearsCounter); this.command = command; } } } Now, what I'm trying to do is get RotateGearsLoaded() to fire with the correct value of displayGearsCounter (as it increments). I'm lost as to how to understand how calling LoadGearData() is supposed to do that. Please elucidate. TIA. John ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Juan Pablo Califano wrote: Yes, the dispatcher and the listener are both in the same call stack always, but I think what we were discussing is whether the caller (the original caller, the one that calls the method that will eventually dispatch the event) is in the same callstack as the listener / event handler. That's not always the case, as it depends on whether the event is dispatched synchronously or not. If you call a method, you will be guaranteed that you will not resume execution while the method is executing. If that method dispatches any events, you will be in the callstack. There is no other scenarios where the method does dispatch the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Maybe you didn't read what I worte carefully or maybe I didn't explain it clearly enough. But note I made a distinction between the dispatcher - the object that performs the callback, directly or through dispatchEvent - and the code (let's refer to it as the first caller) that calls the method that contains the dispatcher. Now, what I mean, and I repeat it now, is that if the dispatch is synchronous or asynchronous changes the stack call: in the first case the first caller will be on the stack; in the second, it won't. Perhaps this code makes this easier to see (modified from the previously posted code): package { import flash.display.Sprite; import flash.events.Event; public class Main extends Sprite { public function Main():void { runTest(); } private function runTest():void { var callbackTest:Tester = new Tester(); callbackTest.run(callback); var eventTester:Tester = new Tester(); eventTester.addEventListener(Event.COMPLETE,handleComplete); eventTester.run(null); } private function handleComplete(evt:Event):void { trace(entering handler); var stacktrace:String = new Error().getStackTrace(); trace(stacktrace); trace(returning from handler); } private function callback(evt:Event):void { trace(entering callback); var stacktrace:String = new Error().getStackTrace(); trace(stacktrace); trace(returning from callback); } } } import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.setTimeout; class Tester extends EventDispatcher { public function Tester() { } public function run(callback:Function):void { setTimeout(function():void { trace(entering run); if(callback != null) { callback(new Event(Event.COMPLETE)); } else { dispatchEvent(new Event(Event.COMPLETE)); } trace(returning from run); trace(); },1); } } Just added a setTimeout to ensure it's asynchronous, but the same will happen if you load external content, for instance. As you can see, runTest() (the first caller) is not in the call stack in this case (while it was where the dispatch was synchronous): entering run entering callback Error at Main/callback()[Main.as:28] at anonymous()[Main.as:51] at Function/http://adobe.com/AS3/2006/builtin::apply() at SetIntervalTimer/onTimer() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick() returning from callback returning from run Cheers Juan Pablo Califano 2010/7/28 Henrik Andersson he...@henke37.cjb.net Juan Pablo Califano wrote: Yes, the dispatcher and the listener are both in the same call stack always, but I think what we were discussing is whether the caller (the original caller, the one that calls the method that will eventually dispatch the event) is in the same callstack as the listener / event handler. That's not always the case, as it depends on whether the event is dispatched synchronously or not. If you call a method, you will be guaranteed that you will not resume execution while the method is executing. If that method dispatches any events, you will be in the callstack. There is no other scenarios where the method does dispatch the event. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
The dispatcher is not in your code here. The method does not dispatch the event. It causes it to be dispatched later. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Sigh... Did you actually bother to read what I wrote? But note I made a distinction between the dispatcher - the object that performs the callback, directly or through dispatchEvent - and the code (let's refer to it as the first caller) that calls the method that contains the dispatcher. 2010/7/28 Henrik Andersson he...@henke37.cjb.net The dispatcher is not in your code here. The method does not dispatch the event. It causes it to be dispatched later. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
- Original Message From: Taka Kojima t...@gigafied.com To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 1:33:43 PM Subject: Re: [Flashcoders] Listeners (was no subject) John, This is going to come across as harsh, however you really should maybe go and get a book on AS3. These problems, forgetting an import, trying to pass arguments to a listener, etc. are pretty rudimentary, and not really the purpose of this list. Yep, a little rough. Got a good book. My question isn't how to pass arguments. I know how to do that. My question is far more specific. It's why don't I have to pass the e:Event argument? Why does the compiler complain that I only pass one argument when I try to pass the other since apparently the first argument is passed automatically? How can I pass that first argument manually when I can't even see what it is? Didn't notice that Moock addressed those questions, so I thought I'd ask here. Would you be so kind as to address those questions, not how to pass arguments? TIA, John Taka On Mon, Jul 26, 2010 at 10:58 AM, John Singleton johnsingleton...@yahoo.com wrote: Original Message From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 12:06:55 PM Subject: Re: [Flashcoders] (no subject) John Singleton wrote: function RotateGearsLoaded(e:Event):void Why is that? I tried to pass that var like this: loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded(displayGearsCounter)); RotateGearsLoaded returns void, not Function. The return value is not a legal listener. Yet you are calling it here to get the listener to add. Remember, you are not the one calling the listener, thusly, you can't decide the arguments. That makes sense. Unfortunately, it's not clear to me how I should proceed. Could you either recommend a tutorial or give an example? TIA, John PS. Original code for those who skipped this message because it had no subject: function Main() { InitRotateGears(); } function InitRotateGears() { for (var i = 0; i gearsPaths.length; i++) { RotateGears(); displayGearsCounter += 1; } function RotateGears() { var path:String = new String(); path = gearsPaths[displayGearsCounter]; var req:URLRequest = new URLRequest(path); var loader:Loader = new Loader(); loader.load(req); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded); } function RotateGearsLoaded(e:Event):void { var loaderInfo:LoaderInfo = e.target as LoaderInfo; var displayObject:DisplayObject = loaderInfo.content; displayObject.width = gearWidths[displayGearsCounter]; displayObject.height = gearHeights[displayGearsCounter]; // displayObject.x = - gearWidths[displayGearsCounter]; // displayObject.y = - gearWidths[displayGearsCounter]; displayObject.x = 0; displayObject.y = 0; trace(displayGearsCounter); parent_container.addChild(displayObject); parent_container.x = 0; parent_container.y = 0; parent_container.alpha = 1; addChild(parent_container); var myTimeline:TimelineLite = new TimelineLite({useFrames:true}); myTimeline.append(new TweenMax(parent_container, 1, {shortRotation:{rotation:gearAngles[displayGearsCounter]}})); } } ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders s mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Listeners (was no subject)
You'll want to learn how to create custom events and pass data with those. With a custom event, you can have it contain any amount and form of data. The custom events we use in our internal development framework all have a public data property with a type wildcard *, but you can specify more specific typecast properties than that of course. Don't feel bad about your questions, I disagree with Taka I suppose, that's the point of this list and the best way to learn. If you were constantly bombarding us with newbie questions, I would probably refer you elsewhere as he did, but you aren't. If you aren't using FlashDevelop or a similar tool of equivalent power, I would recommend it. You'll know right away if you're missing an import for example, and it also will clean up your imports, removing the unused ones. Jason Merrill Instructional Technology Architect Bank of America Global Learning Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (Note: these resources are only available for Bank of America associates) -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of John Singleton Sent: Tuesday, July 27, 2010 8:45 AM To: Flash Coders List Subject: Re: [Flashcoders] Listeners (was no subject) - Original Message From: Taka Kojima t...@gigafied.com To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 1:33:43 PM Subject: Re: [Flashcoders] Listeners (was no subject) John, This is going to come across as harsh, however you really should maybe go and get a book on AS3. These problems, forgetting an import, trying to pass arguments to a listener, etc. are pretty rudimentary, and not really the purpose of this list. Yep, a little rough. Got a good book. My question isn't how to pass arguments. I know how to do that. My question is far more specific. It's why don't I have to pass the e:Event argument? Why does the compiler complain that I only pass one argument when I try to pass the other since apparently the first argument is passed automatically? How can I pass that first argument manually when I can't even see what it is? Didn't notice that Moock addressed those questions, so I thought I'd ask here. Would you be so kind as to address those questions, not how to pass arguments? TIA, John Taka On Mon, Jul 26, 2010 at 10:58 AM, John Singleton johnsingleton...@yahoo.com wrote: Original Message From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 12:06:55 PM Subject: Re: [Flashcoders] (no subject) John Singleton wrote: function RotateGearsLoaded(e:Event):void Why is that? I tried to pass that var like this: loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLo aded(displayGearsCounter)); RotateGearsLoaded returns void, not Function. The return value is not a legal listener. Yet you are calling it here to get the listener to add. Remember, you are not the one calling the listener, thusly, you can't decide the arguments. That makes sense. Unfortunately, it's not clear to me how I should proceed. Could you either recommend a tutorial or give an example? TIA, John PS. Original code for those who skipped this message because it had no subject: function Main() { InitRotateGears(); } function InitRotateGears() { for (var i = 0; i gearsPaths.length; i++) { RotateGears(); displayGearsCounter += 1; } function RotateGears() { var path:String = new String(); path = gearsPaths[displayGearsCounter]; var req:URLRequest = new URLRequest(path); var loader:Loader = new Loader(); loader.load(req); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoad ed); } function RotateGearsLoaded(e:Event):void { var loaderInfo:LoaderInfo = e.target as LoaderInfo; var displayObject:DisplayObject = loaderInfo.content; displayObject.width = gearWidths[displayGearsCounter]; displayObject.height = gearHeights[displayGearsCounter]; // displayObject.x = - gearWidths[displayGearsCounter]; // displayObject.y = - gearWidths[displayGearsCounter]; displayObject.x = 0; displayObject.y = 0; trace(displayGearsCounter); parent_container.addChild(displayObject); parent_container.x = 0; parent_container.y = 0; parent_container.alpha = 1
Re: [Flashcoders] Listeners (was no subject)
- Original Message From: Merrill, Jason jason.merr...@bankofamerica.com To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Tue, July 27, 2010 8:23:16 AM Subject: RE: [Flashcoders] Listeners (was no subject) You'll want to learn how to create custom events and pass data with those. With a custom event, you can have it contain any amount and form of data. The custom events we use in our internal development framework all have a public data property with a type wildcard *, but you can specify more specific typecast properties than that of course. Don't feel bad about your questions, I disagree with Taka I suppose, that's the point of this list and the best way to learn. If you were constantly bombarding us with newbie questions, I would probably refer you elsewhere as he did, but you aren't. If you aren't using FlashDevelop or a similar tool of equivalent power, I would recommend it. You'll know right away if you're missing an import for example, and it also will clean up your imports, removing the unused ones. Your response was incredibly helpful. Thank you very much! John ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Custom events are usually overkill. The issue is not what event, but what the listener knows about the object that it happened to. Most often, the listener can simply access a property of the class that it lies in instead of using some complicated custom event solution. The event object should only contain data that is about the event itself. Not what the listener should do, that is up to the listener. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Paul Andrews wrote: If you try and call a function designed to be an event handler directly, you must create an event object instance to correspond with the event argument yourself when it is called. You must at the very least give the parameter a value. A null reference counts as a value. You can either make it optional or simply pass null each time you are calling it yourself. If it is valid depends on what the function does with it. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Kerry Thompson wrote: Henrik Andersson wrote: Custom events are usually overkill. If I understand you correctly, Henrik, I disagree. Custom events are incredibly useful. That they are. But they are not miracle tool. You should use them wisely and only when it makes sense. AS3 is event-driven, and I routinely have all sorts of custom events. In a recent game, I added event listeners to 7 different custom events in the main class's constructor. When an animation in a MC finishes, the MC sends the ANIMATION_FINISHED message. When the timer fires, it sends another message. When the student answers a question, I send another message. Sounds like you missed Event.COMPLETE, it is just as good to signal when a task is done. I have a class that extends Event, and custom events are defined in this class. So, when you send a custom event, you get an event object just as you would with a system event. Did you bother adding any properties in your custom class? If not, you could just have used the Event class instead. To me, this is a lot cleaner and easier to follow than callbacks or calls to other methods. It's also less prone to mysterious bugs--when you call a method, it always returns to the caller. If that caller no longer exists, you get a crash that can be very hard to diagnose. When you send a custom message, the code doesn't automatically return to the sender. Actionscript is a garbage collected language, objects can not vanish while they are used as the value of this for a method. And if you manage to pull a stunt like that in something like c++, bad you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Here's the deal: If you had googled your error message 1046: Type was not found or was not a compile-time constant: Event, you would have found the answer to your first question in the first 3 results. A 1046 error is a very common error message, seasoned developers already know what's wrong before they finish reading the message, but that's not the point. The point is that this list shouldn't be the first option for such a trivial issue. Secondly, it is basic programming knowledge that you never pass arguments to a listener/callback function by doing testFunction(callbackFunction(argument1,argument2)), because that will run the function and use the return value of that function as the argument for testFunction. In my opinion, both of his questions, albeit they were encountered while working with flash/as3, are not really flash/as3 related questions. The first is not knowing when/how to run a simple search to solve basic programming bugs/errors. The second is just a general lack of basics in programming in general. Maybe I'm alone here, which is fine and John, I am not trying to single you out or anything like that, I just see too many of these types of questions happen on this list. There are some notorious offenders in the past that I'm sure we can all think of (at least those of us that have been on this list for a while). I'm subscribed to this list to help people when I see an actual issue that really warrants help, maybe I should just ignore these types of messages in general and not get myself involved, I don't know. On Tue, Jul 27, 2010 at 11:54 AM, Henrik Andersson he...@henke37.cjb.netwrote: Kerry Thompson wrote: Henrik Andersson wrote: Custom events are usually overkill. If I understand you correctly, Henrik, I disagree. Custom events are incredibly useful. That they are. But they are not miracle tool. You should use them wisely and only when it makes sense. AS3 is event-driven, and I routinely have all sorts of custom events. In a recent game, I added event listeners to 7 different custom events in the main class's constructor. When an animation in a MC finishes, the MC sends the ANIMATION_FINISHED message. When the timer fires, it sends another message. When the student answers a question, I send another message. Sounds like you missed Event.COMPLETE, it is just as good to signal when a task is done. I have a class that extends Event, and custom events are defined in this class. So, when you send a custom event, you get an event object just as you would with a system event. Did you bother adding any properties in your custom class? If not, you could just have used the Event class instead. To me, this is a lot cleaner and easier to follow than callbacks or calls to other methods. It's also less prone to mysterious bugs--when you call a method, it always returns to the caller. If that caller no longer exists, you get a crash that can be very hard to diagnose. When you send a custom message, the code doesn't automatically return to the sender. Actionscript is a garbage collected language, objects can not vanish while they are used as the value of this for a method. And if you manage to pull a stunt like that in something like c++, bad you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
On 27/07/2010 19:37, Henrik Andersson wrote: Paul Andrews wrote: If you try and call a function designed to be an event handler directly, you must create an event object instance to correspond with the event argument yourself when it is called. You must at the very least give the parameter a value. A null reference counts as a value. You can either make it optional or simply pass null each time you are calling it yourself. If it is valid depends on what the function does with it. Well it will work, though I think it's not the best practice. If I have an event handler that does taskX, and I want to do taskX directly without an event, I would code this as function onSomeEvent(e:Event):void { taskX(); } and have a separate taskX() function to allow that to be called directly. Then there's no ambiguity over whether a function is an event handler or not. Generally speaking calling event handlers directly is to be avoided. Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Listeners (was no subject)
maybe I should just ignore these types of messages in general and not get myself involved I'd vote for that one. His questions were legit, he doesn't need condescension no matter how elementary his question seemed. From the perspective of an advanced programmer, they seem very Googleable, but from someone learning, you don't know what you don't know, so a list like this is sometimes the obvious place to ask. John - check out Flash_Tiger on Yahoo (it's a mailing list like this one and also has an online searchable forum) - where any Flash and Actionscript related question is legit. Jason Merrill Instructional Technology Architect Bank of America Global Learning Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (Note: these resources are only available for Bank of America associates) ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Jason Merrill wrote: John - check out Flash_Tiger on Yahoo (it's a mailing list like this one and also has an online searchable forum) - where any Flash and Actionscript related question is legit. Second that. Jason, you somehow neglected to mention that you are one of the Flash Tiger moderators ;-) I would like to say, once again, Flash Tiger isn't interested in stealing readers from Flash Coders. I do urge you to join--it's a smaller and more personal list--but don't leave Flash Coders. Flash Tiger is complementary to Flash Coders, not competitive. Cordially, Kerry Thompson P.S: Should I have mentioned that I'm also one of the Flash Tiger moderators? ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
and you can also do... function onSomeEvent(e:Event = null):void{ } and then just call the function directly, without creating a new Event instance. i.e.: onSomeEvent(); On Tue, Jul 27, 2010 at 12:20 PM, Paul Andrews p...@ipauland.com wrote: On 27/07/2010 19:37, Henrik Andersson wrote: Paul Andrews wrote: If you try and call a function designed to be an event handler directly, you must create an event object instance to correspond with the event argument yourself when it is called. You must at the very least give the parameter a value. A null reference counts as a value. You can either make it optional or simply pass null each time you are calling it yourself. If it is valid depends on what the function does with it. Well it will work, though I think it's not the best practice. If I have an event handler that does taskX, and I want to do taskX directly without an event, I would code this as function onSomeEvent(e:Event):void { taskX(); } and have a separate taskX() function to allow that to be called directly. Then there's no ambiguity over whether a function is an event handler or not. Generally speaking calling event handlers directly is to be avoided. Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
On 27/07/2010 20:34, Taka Kojima wrote: and you can also do... function onSomeEvent(e:Event = null):void{ } and then just call the function directly, without creating a new Event instance. i.e.: onSomeEvent(); Yes, but I specifically avoid that - I like to separate event handlers and other functionality. It's a choice, not a necessity. On Tue, Jul 27, 2010 at 12:20 PM, Paul Andrewsp...@ipauland.com wrote: On 27/07/2010 19:37, Henrik Andersson wrote: Paul Andrews wrote: If you try and call a function designed to be an event handler directly, you must create an event object instance to correspond with the event argument yourself when it is called. You must at the very least give the parameter a value. A null reference counts as a value. You can either make it optional or simply pass null each time you are calling it yourself. If it is valid depends on what the function does with it. Well it will work, though I think it's not the best practice. If I have an event handler that does taskX, and I want to do taskX directly without an event, I would code this as function onSomeEvent(e:Event):void { taskX(); } and have a separate taskX() function to allow that to be called directly. Then there's no ambiguity over whether a function is an event handler or not. Generally speaking calling event handlers directly is to be avoided. Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Henrik Andersson wrote: That they are. But they are not miracle tool. You should use them wisely and only when it makes sense. I agree completely. Custom messages are for the intermediate programmer, at least. I wouldn't recommend them for a beginner. On the other hand, I don't think you can consider yourself an advanced coder unless you understand custom messages (including when to use them). Sounds like you missed Event.COMPLETE, it is just as good to signal when a task is done. No, I know all about Event.COMPLETE. I have a lot of things happening that send a message when they finish, and I like to target the right method. Another legimate approach would be to send all the Event.COMPLETE message to a method that decides where to route them, probably in a switch statement. I just happen to prefer to target a specific listener. Did you bother adding any properties in your custom class? If not, you could just have used the Event class instead. Of course. The custom class has properties and methods that aren't in the Event class. Actionscript is a garbage collected language, objects can not vanish while they are used as the value of this for a method. And if you manage to pull a stunt like that in something like c++, bad you. Agreed completely. I've been programming for over 25 years, and I made all the stupid mistakes years ago. My blunders now are more sophisticated ;-) I think it's a valid point, though--even though it's garbage-collected, it still has a call stack. If a method in object A calls a method in object B, object A won't be eligible for garbage collection until the method in object B is finished. An advanced programmer will understand this (or at least should). It could lead to hard-to-find bugs, though. If you destroy an object while it's on the call stack, you will get, at a minimum, a memory leak. Things like a call stack, garbage collection, memory leaks, and the like are more advanced concepts. And, as Jason says, we should be willing to accomodate less-advanced coders here. Cordially, Kerry Thompson ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
Original Message From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 12:06:55 PM Subject: Re: [Flashcoders] (no subject) John Singleton wrote: function RotateGearsLoaded(e:Event):void Why is that? I tried to pass that var like this: loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded(displayGearsCounter)); RotateGearsLoaded returns void, not Function. The return value is not a legal listener. Yet you are calling it here to get the listener to add. Remember, you are not the one calling the listener, thusly, you can't decide the arguments. That makes sense. Unfortunately, it's not clear to me how I should proceed. Could you either recommend a tutorial or give an example? TIA, John PS. Original code for those who skipped this message because it had no subject: function Main() { InitRotateGears(); } function InitRotateGears() { for (var i = 0; i gearsPaths.length; i++) { RotateGears(); displayGearsCounter += 1; } function RotateGears() { var path:String = new String(); path = gearsPaths[displayGearsCounter]; var req:URLRequest = new URLRequest(path); var loader:Loader = new Loader(); loader.load(req); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded); } function RotateGearsLoaded(e:Event):void { var loaderInfo:LoaderInfo = e.target as LoaderInfo; var displayObject:DisplayObject = loaderInfo.content; displayObject.width = gearWidths[displayGearsCounter]; displayObject.height = gearHeights[displayGearsCounter]; //displayObject.x = - gearWidths[displayGearsCounter]; //displayObject.y = - gearWidths[displayGearsCounter]; displayObject.x = 0; displayObject.y = 0; trace(displayGearsCounter); parent_container.addChild(displayObject); parent_container.x = 0; parent_container.y = 0; parent_container.alpha = 1; addChild(parent_container); var myTimeline:TimelineLite = new TimelineLite({useFrames:true}); myTimeline.append(new TweenMax(parent_container, 1, {shortRotation:{rotation:gearAngles[displayGearsCounter]}})); } } ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Listeners (was no subject)
John, This is going to come across as harsh, however you really should maybe go and get a book on AS3. These problems, forgetting an import, trying to pass arguments to a listener, etc. are pretty rudimentary, and not really the purpose of this list. Taka On Mon, Jul 26, 2010 at 10:58 AM, John Singleton johnsingleton...@yahoo.com wrote: Original Message From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Mon, July 26, 2010 12:06:55 PM Subject: Re: [Flashcoders] (no subject) John Singleton wrote: function RotateGearsLoaded(e:Event):void Why is that? I tried to pass that var like this: loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded(displayGearsCounter)); RotateGearsLoaded returns void, not Function. The return value is not a legal listener. Yet you are calling it here to get the listener to add. Remember, you are not the one calling the listener, thusly, you can't decide the arguments. That makes sense. Unfortunately, it's not clear to me how I should proceed. Could you either recommend a tutorial or give an example? TIA, John PS. Original code for those who skipped this message because it had no subject: function Main() { InitRotateGears(); } function InitRotateGears() { for (var i = 0; i gearsPaths.length; i++) { RotateGears(); displayGearsCounter += 1; } function RotateGears() { var path:String = new String(); path = gearsPaths[displayGearsCounter]; var req:URLRequest = new URLRequest(path); var loader:Loader = new Loader(); loader.load(req); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,RotateGearsLoaded); } function RotateGearsLoaded(e:Event):void { var loaderInfo:LoaderInfo = e.target as LoaderInfo; var displayObject:DisplayObject = loaderInfo.content; displayObject.width = gearWidths[displayGearsCounter]; displayObject.height = gearHeights[displayGearsCounter]; //displayObject.x = - gearWidths[displayGearsCounter]; //displayObject.y = - gearWidths[displayGearsCounter]; displayObject.x = 0; displayObject.y = 0; trace(displayGearsCounter); parent_container.addChild(displayObject); parent_container.x = 0; parent_container.y = 0; parent_container.alpha = 1; addChild(parent_container); var myTimeline:TimelineLite = new TimelineLite({useFrames:true}); myTimeline.append(new TweenMax(parent_container, 1, {shortRotation:{rotation:gearAngles[displayGearsCounter]}})); } } ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders