Thursday, September 07, 2006

Flash Debugging (code everywhere)

I periodically receive questions about Flash from students and colleagues, so i thought that i'd post the answers online for all to see. Hopefully these posts won't merely be a fix to each individual problem, but will also give some sort of ideas for troubleshooting methodolgy when working in Flash.

In this problem, when running a movie, error messages were appearing as follows:

**Error** Scene=Scene 1, layer=button, frame=50:Line 1: Mouse events are permitted only for button instances on (press) {

Here is the way to diagnose such problems in Flash. The first step is to look closely at the error message:

**Error** Scene=Scene 1, layer=button, frame=50:Line 1: Mouse events are permitted only for button instances on (press) {

This tells us a lot. The first thing, is that we're reasonably sure that the problem here is on frame 50, line 1. So the first thing to do is to look at the code on frame 50. There we find two keyframes with code in it. The second one has the same code included in the error message:

on (press) { gotoAndPlay (10);}

Now, the error message says that "Mouse events are permitted only for button instances". But this code is on a frame. Here is the problem. Looking a little closer, we see that the event "on(press)" logically would be hard to apply to a frame, since there is nothing to press. So we can then take the code out and see if the error still exists. In this case, the error goes away, and the problem is solved. You actually had redundant code both on the timeline AND on the button in frame 50.

The overall thing to be careful in Flash is code in places where you don't want it. Flash allows you to put code on movieClips, timelines, and other places where certain types of code will not work. Therefore be careful. Best practice is to put ALL of your code on one layer in the timeline, and create event handlers that refer to clips on the stage. i.e., for a button with the instance name "myButton" on the stage, the code would be

myButton_mc.onPress = function () {
trace ("onPress called");
//put the code here to effect change in the movie

The error messages from the Flash IDE are generally fairly easy to follow, and can be used to logically deduce the problem. The first step is therefore to look closely and to see what it is telling you. The next step is to take a deep breath. Third, use the "trace" function liberally to try and get at the problem. Trace is your friend.

No comments: