Author Archive for Rich



12
Jan

Site Testers


We would like to start a small list of folks who would be willing to put this site through its paces. If anyone is interested/willing to test the site over time- (no commitment required, just test as you go) please send us a note through the Contact form. Thanks.

14
Dec

Sending Data from AVM2 to AVM1


In Chapter 13, we showed how to use a LocalConnection to communicate between an AS3-based movie in Flash Player’s AVM2, and an AS2-based movie in Flash Player’s AVM1. The example was basic, using functions to stop and start an animated movie clip. In this first of future additional examples, we’ll pass data into a function in the AVM! movie.

Continue reading ‘Sending Data from AVM2 to AVM1′

21
Nov

Mandatory Argument Types


Loosely related to the Dispatching Custom Events post, here's a quick example of how to deal with the data type checking of mandatory arguments, such as those used with event listeners.

Below is all the source code required for this example, including the errant segments. Lines 1 through 7 setup a simple timer that calls a function every 3 seconds, until the timer is triggered three times. If you test just that portion of the code, you'll see an initial delay of three seconds, followed by three traces to the Output panel.

Suppose, however, that you also want to trigger the same event once, immediately. If you just call the function, as seen in line 10, you will get an error. This is because you can't call a listener with required arguments, without sending the required values. That is, the listener function requires an event object for the evt parameter, and an error will be thrown if it is not supplied.

ActionScript 3.0:
  1. var timr:Timer = new Timer(3000, 3);
  2. timr.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
  3. timr.start();
  4.  
  5. function onTimer(evt:TimerEvent):void {
  6.     trace("timer function triggered");
  7. }
  8.  
  9. //scenario 1
  10. //onTimer();
  11. //error: 1136: Incorrect number of arguments.  Expected 1.
  12. onTimer(null);
  13.  
  14. //scenario 2
  15. onTimer(new TimerEvent(TimerEvent.TIMER));
  16.  
  17. //scenario 3
  18. //stage.addEventListener(MouseEvent.CLICK, onTimer, false, 0, true);
  19. //TypeError: Error #1034: Type Coercion failed: 
  20. //   cannot convert flash.events.MouseEvent to flash.events.TimerEvent.
  21. stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
  22.  
  23. function onClick(evt:MouseEvent):void {
  24.     onTimer(new TimerEvent(TimerEvent.TIMER));
  25. }

One easy way to accomplish this goal, is to send null with the function call, as seen in line 12. This will prevent an error from being thrown. If the event itself can be of use to you, you can create a new Timer event and send that to the listener argument. This can be seen in line 15, and is also covered in the previous post. All you have to do is follow the new keyword, with the desired event class, and the specific event you want to be created.

The third scenario is an example of wanting to augment the timer with mouse clicks. If you simply add a mouse listener to the stage, as seen in line 21, you will also get an error. The onTimer function will be called, but the event passed to the function will be a mouse event, rather than the required timer event. One way to get around this is to create an interim function, that correctly receives the mouse event and sends the desired timer event to the ultimate function, as seen in lines 23 through 25.




Speaking



Archives