Copy link to clipboard
Copied
This is my first attempt at using a class file in a flash project. My intent is to keep all of my navigation elements in class file called "Navigation". However, I keep getting an error message when I publish.
I am using a button to go back to the main screen and I gave that button the instance name of "bnt_home". I have also linked that button in the library to the class "Navigation".
Here is the code:
package
{
import flash.display.SimpleButton;
public class Navigation extends SimpleButton
{
public function Navigation()
{
bnt_home.addEventListener(MouseEvent.MOUSE_DOWN, goNavigation);
}
private function goNavigation(event:MouseEvent):void
{
gotoAndPlay(1,"Main");
}
}
}
Copy link to clipboard
Copied
2 problems:
public function Navigation()
{
bnt_home.addEventListener(MouseEvent.MOUSE_DOWN, goNavigation);
// should be : this.addEventListener(MouseEvent.CLICK, goNavigation)
}
private function goNavigation(event:MouseEvent):void
{
gotoAndPlay(1,"Main");
//should be : this.parent.gotoAndPlay(1,"Main");
//or root.gotoAndPlay(1,"Main");
}
Copy link to clipboard
Copied
Better still, the Class shouldn't know or try to know anything about what its environment is, and the parent MC should listen for a custom event that the Navigation class dispatches based on a click on itself or the child button.
Note that it's easier for people to see if your Class is set up properly if you uncheck "declare stage instances automatically" and you then declare the instances in the Class. It also helps with auto code-completion, especially if you're using an external IDE like Flash Builder or FlashDevelop to help you code more efficiently.
Copy link to clipboard
Copied
When I changed the code I got error (1046: Type was not found or was not a compile-time constant: MouseEvent).
package
{
import flash.display.SimpleButton;
public class Navigation extends SimpleButton
{
public function Navigation()
{
this.addEventListener(MouseEvent.MOUSE_DOWN, goNavigation);
}
private function goNavigation(event:MouseEvent):void
{
root.gotoAndPlay(1,"Main");
}
}
}
Copy link to clipboard
Copied
...
import flash.display.SimpleButton;
import flash.events.MouseEvent
...
Copy link to clipboard
Copied
Note that if you look at the screen while you are typing or press Ctrl-Space while you are typing, you can take advantage of code completion, which will do your imports for you.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now