Sign in to follow this  
Serendipity

Basic Flash Question

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:"

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by kindjie All you need to do is tell Flash where to find and start running your code...


That did it! Thank you so much for your help and for the explanation of why it was necessary. And thanks again to everyone who chimed in.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this