Copy link to clipboard
Copied
In this class I have error about two variables myFormat and AboutText. Can't understand why? There is import line, and class in its own file. Before this error all text of this class were in main.as and working fine. When I tried to move code in different class the error ocures.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.*;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.text.*;
public class MenuScreen extends Sprite
{
var myFormat:TextFormat = new TextFormat();
var AboutText:TextField = new TextField();
var btn_play:MyButtonClass = new MyButtonClass();
var btn_opt:MyButtonClass = new MyButtonClass();
var btn_inst:MyButtonClass = new MyButtonClass();
var btn_abt:MyButtonClass = new MyButtonClass();
var btn_ext:MyButtonClass = new MyButtonClass();
myFormat.font = "Times New Roman";
myFormat.size = 20;
myFormat.color = 0x000000;
AboutText.width = 550;
AboutText.height = 250;
AboutText.x = 225;
AboutText.y = 80;
AboutText.selectable = false;
AboutText.setTextFormat(myFormat);
addChild(AboutText);
public function MenuScreen()
{
super();
btn_play.name = "btn_PLAY";
btn_play.label = "Play";
btn_play.addEventListener(MouseEvent.CLICK, btnMouseHandle);
addChild(btn_play).y = 50;
btn_play.x = 80
btn_opt.name = "btn_OPTIONS";
btn_opt.label = "Options"
btn_opt.addEventListener(MouseEvent.CLICK, btnMouseHandle);
addChild(btn_opt).y = 100;
btn_opt.x = 80
btn_inst.name = "btn_INSTRUCTIONS";
btn_inst.label = "Instructions"
btn_inst.addEventListener(MouseEvent.CLICK, btnMouseHandle);
addChild(btn_inst).y = 150;
btn_inst.x = 80
btn_abt.name = "btn_ABOUT";
btn_abt.label = "About"
btn_abt.addEventListener(MouseEvent.CLICK, btnMouseHandle);
addChild(btn_abt).y = 200;
btn_abt.x = 80
btn_ext.name = "btn_EXIT";
btn_ext.label = "Exit"
btn_ext.addEventListener(MouseEvent.CLICK, btnMouseHandle);
addChild(btn_ext).y = 250;
btn_ext.x = 80
}
function btnMouseHandle(e:MouseEvent):void
{
switch(e.target.name){
case "btn_PLAY":
var TestSWC:MovieClip = new WSymbol_mc;
trace("btn_01 click");
TestSWC.x = 50;
TestSWC.y = 50;
addChild(TestSWC);
break;
case "btn_OPTIONS":
trace("btn_02 click");
break;
case "btn_INSTRUCTIONS":
trace("btn_03 click");
ShowTextBlock (e.target.name);
break;
case "btn_ABOUT":
trace("btn_04 click");
ShowTextBlock (e.target.name);
break;
case "btn_EXIT":
trace("btn_05 click");
fscommand('quit');
break;
}
}
function ShowTextBlock (strID:String):void
{
if (strID == "btn_INSTRUCTIONS")
{
trace("INS");
AboutText.text = "Some text here";
}
else
{
trace("AB");
myFormat.align = TextFormatAlign.CENTER;
myFormat.leading = 10;
AboutText.defaultTextFormat = myFormat;
AboutText.text = "Some text here";
}
}
}
}
This is the main.as:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.*;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.text.*;
public class Main extends Sprite
{
var MScreen:MenuScreen = new MenuScreen;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
addChild(MScreen);
}
}
}
One man told me that the reason in too many actions outside functions, is it true? Is it restriction in AS3?
Copy link to clipboard
Copied
Try moving everything but the variable declarations inside the first function. Make the variables private/public as needed.
Copy link to clipboard
Copied
no, it's not because of too many actions outside functions.
all your 'new' constructors should look like:
var MScreen:MenuScreen = new MenuScreen();
not
var MScreen:MenuScreen = new MenuScreen
and click file>publish settings>swf>and tick 'enable debugging'. retest.
the problematic line number will be in the error message allowing your to pinpoint the error. if you still need help after doing that, indicate the problematic line of code.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now