Syntax Error AS3 using FlashDevelop - actionscript-3

I've search for similar but mine has none of the problems started in others- no naming using protected functions or rogue {}.
So can you help- what's wrong?
All for Row 19 (private function display2)
col: 2 Error: Syntax error: expecting identifier before leftbrace.
col: 2 Error: Syntax error: expecting leftparen before leftbrace.
col: 2 Error: Syntax error: expecting identifier before leftbrace.
col: 2 Error: Syntax error: expecting rightparen before leftbrace.
{
package
{
import flash.accessibility.AccessibilityImplementation;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
/**
* ...
* #author Michael
*/
public class Start extends Sprite {
[Embed(source="../lib/Start.jpg")]
private var StartClass:Class
private function display2():void
{
addChild(StartClass());
myTextBox.text = "Jabble. Click to Scroll Down (下にスクロールする]をクリック). Press Enter to Instructions alternate between English and Japanese (translations). Press H for the help web page or put http://wp.me/P3FUQl-n in your web browser. Beneath is the Board and to the right is the Box. Click and Drag Tiles to move it and double click it set it on a square space on the Board or Box and click the Box to change its mode. Jabble- 英語と日本語(訳)との間で交互に指示。を押して、ヘルプWebページのHまたはWebブラウザでhttp://wp.me/P3FUQl-nを置く。下には、理事会で、右側のボックスである。クリックして、それを移動するにはタイルをドラッグし、ダブル会またはボックス上の正方形のスペースには、それを設定してクリックし、そのモードを変更するには、ボックスをクリックしてください" ;
myTextBox.width = Box.width;
myTextBox.height = Box.height;
myTextBox.multiline = true;
myTextBox.wordWrap = true;
myTextBox.background = true;
myTextBox.border = true;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 10;
myTextBox.defaultTextFormat = format;
addChild(myTextBox);
myTextBox.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownScroll);
}
}
}

You have made a few mistakes there.
First of all you are adding a class without using the new before it.
It needs to be addChild(new StartClass()) instead of addChild(StartClass()).
And seconly, you haven't declared the variable myTextBox.
Probably something like var myTextBox:TextField = new TextField();.

This happens because before package you opened {

Related

FlashDevelop Syntax Error: Expecting Identifier Before Public

I'm new to AS3, and I would really appreciate any help you can give me! I am trying to create a button. However, FlashDevelop keeps telling me I have a Syntax Error in:
Line 11 Col. 2 (Syntax Error: expecting identifier before public)
Line 11 Col. 40 (Syntax Error: expecting identifier before extends)
Line 13 Col. 2 (Error: the private attribute may only be used on class property definitions)
Below is my code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Graphics;
import flash.
/**
* ...
* #author 15leungjs1
*/
public class Main extends Sprite
{
private var button:Sprite;
public function Main():void
{
//Create a new instance of a Sprite to act as the button graphic.
button = new Sprite();
//Set the color of the button graphic
button.graphics.beginFill(0xFFAD3B);
//Set the X,Y, Width, and Height of the button graphic
button.graphics.drawRect(10, 0, 200, 100);
//Apply the fill
button.graphics.endFill();
//Add Handcursor,buttonMode, and mouseChildren
button.buttonMode = true;
button.useHandCursor = true;
button.mouseChildren = false;
//Add Button Sprite to stage
this.addChild(button);
}
}
}
}
The problem is simple...
You have the following line of code (line 6) that is not finished correctly:
import flash.
You need to complete that import statement or remove it completely.

embedded font flash.text.engine

I've completely run out of ideas on this. It follows, and is part of my previous question:
embedding a font in a swf using as3
I just don't seem to be able to get the flash.text.engine to use my embedded font. NB the font has to be loaded into the application (as embedded in swf) after the user has chosen the two languages (for translation to and from). There seems to be a little info implying that it is now necessary to use the fontswf application which is in the sdk. I have tried this and produced loadable swf files but I can't find any info on how these are then loaded (i.e. the getDefinition and registerFont bits below don't work as there are no classes in these swf) and applied to text.engine objects. The source for the embedding is in my answer to my question above. This is a test as3 which demonstrates how it doesn't work!
package
{
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.TextBlock;
import flash.text.engine.TextLine;
import flash.text.engine.TextElement;
import flash.net.URLRequest;
import flash.text.Font;
public class Main extends Sprite
{
private var loader:Loader;
private var tl:TextLine;
public function Main():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,fontLoaded);
loader.load(new URLRequest("EnglishF.swf"));
}
private function fontLoaded(evt:Event):void {
var FontClass:Class
FontClass = evt.target.applicationDomain.getDefinition("EnglishF") as Class;
try {
Font.registerFont(FontClass.myFont);
trace("successfully loaded " + FontClass);
// gives 'successfully loaded EnglishF'
} catch (err:Error) {}
var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
trace(fontList[i].fontName, fontList[i].fontType);
// gives 'EnglishF embeddedCFF'
}
var block:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription("EnglishF");
var formt:ElementFormat = new ElementFormat(font, 30);
trace(FontDescription.isFontCompatible("EnglishF","normal","normal"), formt.fontDescription.fontName);
// gives 'true EnglishF'
formt.color = 0x882233;
var span:TextElement = new TextElement("Hello World. This is certainly NOT in the Font provided!", formt);
block.content = span;
tl = block.createTextLine();
tl.x = 10;
tl.y = tl.ascent + 10;
addChild(tl);
}
}
}
Am I doing anything wrong, or is this impossible?
Hopefully this will help. Under this line:
var font:FontDescription = new FontDescription("EnglishF");
add the following line:
font.fontLookup = FontLookup.EMBEDDED_CFF;
This will let the text framework know that you're using a CFF font (used in the new text framework), instead of a regular embedded font (used in TextFields).
Hope this helps.
Jordan

Debugging a release only flash issue

I've got an Adobe Flash 10 program that freezes in certain cases, however only when running under a release version of the flash player. With the debug version, the application works fine.
What are the best approaches to debugging such issues? I considered installing the release player on my computer and trying to set some kind of non-graphical method of output up (I guess there's some way to write a log file or similar?), however I see no way to have both the release and debug versions installed anyway :( .
EDIT: Ok I managed to replace my version of flash player with the release version, and no freeze...so what I know so far is:
Flash: Debug Release
Vista 32: works works
XP PRO 32: works* freeze
I gave them the debug players I had to test this
Hmm, seeming less and less like an error in my code and more like a bug in the player (10.0.45.2 in all cases)... At the very least id like to see the callstack at the point it freezes. Is there some way to do that without requiring them to install various bits and pieces, e.g. by letting flash write out a log.txt or something with a "trace" like function I can insert in the code in question?
EDIT2: I just gave the swf to another person with XP 32bit, same results :(
EDIT3:
Ok, through extensive use of flash.external.ExternalInterface.call("alert", "..."); I managed to find the exact line causing the problem (I also improved exception handling code so rather than freeze it told me there was an "unhandled" exception). The problem now is what on earth is flashes problem with this with the release player on some machines...
particles.push(p);
Which causes a TypeError #1034 on said platforms. Particles is a Vector.<Particle>, p is a Particle. I tested with getQualifiedClassName and got:
getQualifiedClassName(p) = ::Particle
getQualifiedClassName(particles) = __AS3__.vec::Vector.<::Particle>
Any ideas why this is a problem and what to do to make it work?
EDIT4:
Ok I seem to have solved this. The Particle class is just a simple internal class located after the package {...} in the action script file using it. I moved this into its own file (particle.as) and made it a proper public class in my package, and problem solved.
Maybe its a flash bug or maybe I missed the memo about not using internal classes in vectors or something, although if that's the case I would have expected something or other (either at compile time or with debug runtimes) to disallow it explicitly, e.g. some error on the "private var particles:Vector.<Particle>;" line. If I get a chance I guess I'll take a look at contacting the Adobe flash team concerning this or something.
Thanks for help giving debugging tips which I guess is more along the original questions lines :)
This is a long shot, but are the Particles the objects that you are clicking? If so then catching the event in the wrong phase of bubbling, and pushing event.target (assuming it to be a Particle) could cause that problem.
Whatever the problem, I have something that should help you debug. A class that creates a pseudo trace window in your SWF, much nicer than extinterfacing to javascript. I've forgotten who wrote it, but I feel like it's Senocular. I use it any time I need to get traces back from end users.
Just drop it in the default package for your project, call stage.addChild(new Output());, and then to trace call Output.trace("A message");
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.GradientType;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFieldAutoSize;
/**
* Creates a pseudo Output panel in a publish
* swf for displaying trace statements.
* For the output panel to capture trace
* statements, you must use Output.trace()
* and add an instance to the stage:
* stage.addChild(new Output());
*
*/
public class Output extends Sprite {
private var output_txt:TextField;
private var titleBar:Sprite;
private static var instance:Output;
private static var autoExpand:Boolean = false;
private static var maxLength:int = 1000;
public function Output(outputHeight:uint = 400){
if (instance && instance.parent){
instance.parent.removeChild(this);
}
instance = this;
addChild(newOutputField(outputHeight));
addChild(newTitleBar());
addEventListener(Event.ADDED, added);
addEventListener(Event.REMOVED, removed);
}
// public methods
public static function trace(str:*):void {
if (!instance) return;
instance.output_txt.appendText(str+"\n");
if (instance.output_txt.length > maxLength) {
instance.output_txt.text = instance.output_txt.text.slice(-maxLength);
}
instance.output_txt.scrollV = instance.output_txt.maxScrollV;
if (autoExpand && !instance.output_txt.visible) instance.toggleCollapse();
}
public static function clear():void {
if (!instance) return;
instance.output_txt.text = "";
}
private function newOutputField(outputHeight:uint):TextField {
output_txt = new TextField();
//output_txt.type = TextFieldType.INPUT;
output_txt.border = true;
output_txt.borderColor = 0;
output_txt.background = true;
output_txt.backgroundColor = 0xFFFFFF;
output_txt.height = outputHeight;
var format:TextFormat = output_txt.getTextFormat();
format.font = "_sans";
output_txt.setTextFormat(format);
output_txt.defaultTextFormat = format;
return output_txt;
}
private function newTitleBar():Sprite {
var barGraphics:Shape = new Shape();
barGraphics.name = "bar";
var colors:Array = new Array(0xE0E0F0, 0xB0C0D0, 0xE0E0F0);
var alphas:Array = new Array(1, 1, 1);
var ratios:Array = new Array(0, 50, 255);
var gradientMatrix:Matrix = new Matrix();
gradientMatrix.createGradientBox(18, 18, Math.PI/2, 0, 0);
barGraphics.graphics.lineStyle(0);
barGraphics.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
barGraphics.graphics.drawRect(0, 0, 18, 18);
var barLabel:TextField = new TextField();
barLabel.autoSize = TextFieldAutoSize.LEFT;
barLabel.selectable = false;
barLabel.text = "Output";
var format:TextFormat = barLabel.getTextFormat();
format.font = "_sans";
barLabel.setTextFormat(format);
titleBar = new Sprite();
titleBar.addChild(barGraphics);
titleBar.addChild(barLabel);
return titleBar;
}
// Event handlers
private function added(evt:Event):void {
stage.addEventListener(Event.RESIZE, fitToStage);
titleBar.addEventListener(MouseEvent.CLICK, toggleCollapse);
fitToStage();
toggleCollapse();
}
private function removed(evt:Event):void {
stage.removeEventListener(Event.RESIZE, fitToStage);
titleBar.removeEventListener(MouseEvent.CLICK, toggleCollapse);
}
private function toggleCollapse(evt:Event = null):void {
if (!instance) return;
output_txt.visible = !output_txt.visible;
fitToStage(evt);
}
private function fitToStage(evt:Event = null):void {
if (!stage) return;
output_txt.width = stage.stageWidth;
output_txt.y = stage.stageHeight - output_txt.height;
titleBar.y = (output_txt.visible) ? output_txt.y - titleBar.height : stage.stageHeight - titleBar.height;
titleBar.getChildByName("bar").width = stage.stageWidth;
}
}
}
Judging by when the freeze occurs, try to pinpoint some possibilities for what the offending code may be, and use De MonsterDebugger to check variables etc.
EDIT:
I'm pretty certain that the actual call stack is only available to you in the debug versions of the Flash Player / AIR. Still, it may be useful in the debug player to trace the stack from within the handler for the button to see if anything is out of place:
var err:Error = new Error(“An Error”);
trace(err.getStackTrace());
FYI the getStackTrace method is only available in the debug player, so there is no way to write it to a log.txt in production.

Flash/Flex error 1067: can't make a custom TextFormat object?

Because I want to avoid repetitive code, and I'm using a lot of text formats, I created a CustomTextFormat class in Flex Builder.
Another class, called CustomInputBox.as is using this object to create a format:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class CustomInputBox extends Sprite
{
public function CustomInputBox(xLoc:int, yLoc:int, width:uint, height:uint, password:Boolean = false, text:String = "", font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false)
{
var inputBox:TextField = new TextField();
inputBox.type = TextFieldType.INPUT;
inputBox.mouseEnabled = true;
inputBox.selectable = true;
inputBox.multiline = false;
inputBox.x = xLoc;
inputBox.y = yLoc;
inputBox.width = width;
inputBox.height = height;
inputBox.displayAsPassword = password;
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
inputBox.text = text;
addChild(inputBox);
}
}
}
The code of CustomTextFormat is as follows:
package
{
import flash.display.Sprite;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomTextFormat extends Sprite
{
public function CustomTextFormat(font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false, fontAlign:String = TextFormatAlign.LEFT)
{
var format:TextFormat = new TextFormat();
format.font = font;
format.color = fontColor;
format.size = fontSize;
format.bold = fontBold;
format.align = fontAlign;
}
}
}
Now, I'm getting error 1067 in the CustomInputBox.as file, it's a Dutch error unfortunately (any way to set flex errors to english?):
1067: Impliciete afgedwongen omzetting van een waarde van het type
CustomTextFormat in een niet-gerelateerd type flash.text:TextFormat.
CustomInputBox.as
It's difficult to translate, but hopefuly the error number and the code are enough to identify my problem. I'm new to Flash, and searched but couldn't find out what I am doing wrong.
Thanks in advance.
Something's wacky here. If you want to assign your custom format like this:
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
Then CustomTextFormat needs to extend TextFormat, and the code in CustomTextFormat's constructor should be modifying the inherited TF properties. Alternately, if you want to leave CustomTextFormat extending Sprite, then you need to change CustomTextFormat's "format" property to be a public property, and change your assignment to something like:
var customFormat:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = customFormat.format;
Does that make sense? Right now you're trying to set the input's default text format to a class object that extends Sprite. And the inputBox doesn't know anything about CustomTextFormat's internal "format" property, which is both private and temporary.
(Incidentally none of this precisely explains the error message you're getting, but in my experience it's somewhat rare for Flash compiler errors to really tell you what's wrong... they tend to claim you're using classes illegally when all you did is leave out a semicolon. I tend not to trust the error messages too much.)

ActionScript 3 Error 1037: Packages cannot be nested

I am new to AS3. When learning AS3, I get the below code from an Adobe example and trying to run it gives an error like
"1037: Packages cannot be nested."
What does this mean?
Please tell me how to execute? or any problem in this code?
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class TextField_alwaysShowSelection extends Sprite {
public function TextField_alwaysShowSelection() {
var label1:TextField = createTextField(0, 20, 200, 20);
label1.text = "This text is selected.";
label1.setSelection(0, 9);
label1.alwaysShowSelection = true;
var label2:TextField = createTextField(0, 50, 200, 20);
label2.text = "Drag to select some of this text.";
}
private function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x; result.y = y;
result.width = width; result.height = height;
addChild(result);
return result;
}
}
}
You need to create an action script file and then add that class to document class in your fla file property then it would not give you an error
Your code should compile, provided that it is in the root source folder ("src" in flex builder). Are you sure that's the entire file?
The error means that you have nested a package {} statement inside another package {} statement.
If you want to include the AS3 in the timeline itself, use this:
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
function TextField_alwaysShowSelection() {
var label1:TextField = createTextField(0, 20, 200, 20);
label1.text = "This text is selected.";
label1.setSelection(0, 9);
label1.alwaysShowSelection = true;
var label2:TextField = createTextField(0, 50, 200, 20);
label2.text = "Drag to select some of this text.";
}
function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x; result.y = y;
result.width = width; result.height = height;
addChild(result);
return result;
}
How are you running this file? This is not a complete file. If you are working with flex then you need the supported MXML code. However the error indicates that you are working with src folder. It would be good if you give the complete procedure.
If you are using Flash, put that code in a file named "TextField_alwaysShowSelection.as", place it next to your FLA and set that class name as the DocumentClass in the "properties" panel of your FLA... so where it says "Class:" write "TextField_alwaysShowSelection".