Basic Flash Question

Started by
9 comments, last by Serendipity 14 years, 2 months ago
Hi all, I'm working my way through Foundation Game Design with Flash and I'm having a little trouble with one of the early sample problems. I've created the .fla file and the .as file. I placed them both in the same project. Then I tried to run the following code: package { import flash.display.MovieClip; import flash.events.MouseEvent; public class Main extends MovieClip { public function Main() { stage.addEventListener(MouseEvent.CLICK, onClick); } function onClick(event:MouseEvent):void { trace("You clicked on the stage"); } } } However, when I test the project I only get a blank screen. Nothing happens when I click on it. Any suggestions?
Advertisement
trace() just outputs to your debug output window. If you want text to appear on screen you will need to create a TextField then use addChild to make it visible.
Are you running it within the Flash IDE by debugging your movie? If so, your trace statement will show up in the output window of the Flash IDE when you click somewhere on the blank stage. Trace statements are mostly used for debugging, and won't show up on the stage itself. If you made a mistake in your code, the output window is where you should look for errors.

I'm sure later on in your book it will show you how to add images, movieclips, and buttons to the stage and respond to click events with them.
[size="1"]Try GardenMind by Inspirado Games !
All feedback welcome.
[s]
[/s]

[size="1"]Twitter: [twitter]Owen_Inspirado[/twitter]
Facebook: Owen Wiggins

[size="1"]Google+: Owen Wiggins

I don't think you can assume the stage is already created when you're in the constructor:
public class Main extends Sprite {  public function Main():void   {    if (stage)       initialize();    else       addEventListener(Event.ADDED_TO_STAGE, initialize);  }		  private function initialize(e:Event = null):void   {    removeEventListener(Event.ADDED_TO_STAGE, initialize);    // register onclick events    stage.addEventListener(MouseEvent.CLICK, onClick);  }}

Also, I remember having problems attaching an event handler when the stage didn't have the focus. You may want to do that manually if you're still having trouble.
Ooohh...

Yes, I am running it in Flash's IDE. I'm just pushing test project in the project panel. However, even within the IDE, the output doesn't display. The output window just says: "C:\Documents and Settings\BUCK BAIN\My Documents\Games\Flash\Chapter 3\Event Listener\Mouse_click.fla:"
Have you tried Wan's suggestion? You can also put a trace right in the constructor to see if that is being hit:
public class Main extends Sprite {  public function Main():void   {    trace("Constructor hit!");    // Check if the stage is ready. If it is, initialize it.    // Otherwise, add an event that will initialize it when it is.    if (stage)       initialize();    else       addEventListener(Event.ADDED_TO_STAGE, initialize);  }		  private function initialize(e:Event = null):void   {    removeEventListener(Event.ADDED_TO_STAGE, initialize);    // register onclick events    stage.addEventListener(MouseEvent.CLICK, onClick);  }  function onClick(event:MouseEvent):void  {    trace("You clicked on the stage");  }}

If you don't see any traces in your output, then double check that you have your document class set up correctly. It needs to be set in your Flash IDE: http://livedocs.adobe.com/flash/9.0/UsingFlash/help.html?content=WS026C9121-F7D4-496d-94C8-368BF6938149.html

[Edited by - kindjie on February 7, 2010 6:55:41 PM]
[size="1"]Try GardenMind by Inspirado Games !
All feedback welcome.
[s]
[/s]

[size="1"]Twitter: [twitter]Owen_Inspirado[/twitter]
Facebook: Owen Wiggins

[size="1"]Google+: Owen Wiggins

Quote:Original post by kindjie
Have you tried Wan's suggestion?


I haven't because I've already run a program successfully without having to check the stage.

Quote:Original post by kindjie
If you don't see any traces in your output, then double check that you have your document class set up correctly. It needs to be set in your Flash IDE: http://livedocs.adobe.com/flash/9.0/UsingFlash/help.html?content=WS026C9121-F7D4-496d-94C8-368BF6938149.html


This is all greek to me. I guess I'll just leave it alone for the time being. It's just frustrating because this is supposed to be just an example program. When I try to run the version I downloaded from the site I get something like unexpected file type. When I code it myself it runs but nothing happens. Hopefully I'll be able to complete the rest of the exercises with on problems.

Thanks all for all your suggestions.
Quote:Original post by Serendipity
Quote:Original post by kindjie
Have you tried Wan's suggestion?


I haven't because I've already run a program successfully without having to check the stage.


Are you sure its working? I don't know about Flash CS (didn't want to spend $600 to make Flash programs) but with FlashDevelop unless you install the debug runtimes a Flash program can crash and burn silently. Until I figured that out many times I just saw a blank screen when something crashed and I didn't know about it. Its a couple of lines to make that suggestion work. Its better to be safe then sorry.

Just as a side note, coming from a programming background using FlashDevelop instead of having to muck around with .fla and ActionScript has made the process quite a bit easier. Downside is most books/websites are written with Flash CS in mind.
Quote:Original post by kindjie
If you don't see any traces in your output, then double check that you have your document class set up correctly.

That's a good one actually. I've tried your exact code by making one Flash file and one ActionScript file (called Main.as) and specifying the document class as pointed out by kindjie, and it worked (even without my hack).

The fact that it works on my machine only means so much of course...
Quote:Original post by Serendipity
This is all greek to me. I guess I'll just leave it alone for the time being. It's just frustrating because this is supposed to be just an example program. When I try to run the version I downloaded from the site I get something like unexpected file type. When I code it myself it runs but nothing happens. Hopefully I'll be able to complete the rest of the exercises with on problems.

Thanks all for all your suggestions.


Don't get discouraged! There's just a few simple steps you need to take to ensure your code is being executed. Like jtagge75 said, if there's a problem with your code, Flash will draw anything that's on your main timeline but NOT run any of your code!

All you need to do is tell Flash where to find and start running your code. To do this, you declare a document class. This tells Flash to look for a .as file of that name containing a class of that name. In your case, this would be Main.as containing the class Main.

What version of the Flash IDE are you using? I'm assuming Flash CS4, but it should be similar for previous versions.

1) Open your .fla file.
2) Make sure you have nothing on the stage selected.
3) You should have a panel with the following tabs (usually on the right side of the Flash IDE): Properties, and Library. Click on the Properties tab.
4) This shows some Publish settings and Stage properties. Under Publish, you should see a textbox labeled Class. This is where you tell Flash to start running your code. Type in your class name here: Main.

Now when you run your program, Flash will start by creating an instance of your Main class. This will run the constructor (the code under "public function Main()"). If you put a trace in there and everything is set up correctly, you should see the trace in your output window.
[size="1"]Try GardenMind by Inspirado Games !
All feedback welcome.
[s]
[/s]

[size="1"]Twitter: [twitter]Owen_Inspirado[/twitter]
Facebook: Owen Wiggins

[size="1"]Google+: Owen Wiggins

This topic is closed to new replies.

Advertisement