FlashDevelop Syntax Error: Expecting Identifier Before Public - actionscript-3

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.

Related

Dynamically display text in Flex, ActionScript 3

I've been having problems displaying text in ActionScript 3 using Flex SDK 4.6. Any method I try results in no change or a black screen; here is my project code and attempts.
MyProject.mxml is simply and mx:Application tag with the relevant parts being
<mx:Script>
<![CDATA[
include "MyProject.as"; //simply running my project file
]]>
</mx:Script>
and
<mx:Canvas id="gamePanel" x="0" y="0" width="100%" height="100%"/> //defining the canvas
In MyProject.as I have
import flash.display.*;
import flash.events.*;
import mx.events.*;
import mx.controls.*;
import Game;
public static const SCREEN_WIDTH:int = 960;
public static const SCREEN_HEIGHT:int = 720;
private var initializationCompleted:Boolean = false;
public var screenBuffer:BitmapData;
public var game:Game;
public function setup():void {
screenBuffer = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT, false, 0x00000000);
game = new Game(SCREEN_WIDTH, SCREEN_HEIGHT, screenBuffer);
initializationCompleted = true;
}
private function updateFrame():void {
if (!initializationCompleted) {
return;
}
draw();
gamePanel.graphics.clear();
gamePanel.graphics.beginBitmapFill(screenBuffer, null, false, false);
gamePanel.graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
gamePanel.graphics.endFill();
}
private function draw():void {
game.update();
}
And in Game.as, I simply draw everything using the BitmapData class, and then copy everything to the screenBuffer:
screenBuffer.copyPixels(myBitmap, new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), new Point(0,0));
(This is only the relevant code - I trimmed as much as possible to leave a "Minimal, Complete, and Verifiable example")
Now I have been having problems displaying text in my project. I know that TextField is a subclass of flash.display.Sprite which can be added to the canvas. Whenever I try using something like
var txtHello:TextField = new TextField();
txtHello.text = "Hello World";
gamePanel.addChild(txtHello)
this either changes nothing (if used in setup(), I'm assuming I'm drawing over it or else it is never displayed) or causes a black screen (if used anywhere in updateFrame(), I'm assuming I'm creating infinite sprites).
I have tried instead, creating a new file named "TextWithImage.as" with the contents
//this is ripped off the adobe help page
package {
import flash.display.Sprite;
import flash.text.*;
public class TextWithImage extends Sprite {
private var myTextBox:TextField = new TextField();
private var myText:String = "Hello World";
public function TextWithImage() {
addChild(myTextBox);
myTextBox.text = myText;
}
}
}
importing it in MyProject.as, and then using it as
gamePanel.addChild(new TextWithImage());
to the same effect as my previous attempt.
What is the simplest way to display text in Flex/AS3? Any help is appreciated, and thank you in advance!
There's a trick. Flex components, albeit having the same addChild method derived from DisplayObjectContainer class, cannot actually add regular Flash content - Shape, Sprite, MovieClip, TextField, Bitmap - directly. More to that, they don't produce any runtime error, which I personally think they totally could to not confuse new people.
Flex component can only addChild classes that extend the basic UIComponent class. At the same time, UIComponent can addChild regular Flash content. Thus you do it as following:
var proxyContainer:UIComponent = new UIComponent;
var txtHello:TextField = new TextField;
txtHello.text = "Hello World";
proxyContainer.addChild(txtHello);
gamePanel.addChild(proxyContainer);

AS3 error 2025 removechild

I made a game with AS3 where you must click on falling bombs before they explode and destroy a wall. Now, I'm trying to remove the bombs that fell just when the wall got destroyed, so I did a removeChild(blob) in my game over function, since these bombs are added to the stage with an addChild(blob), and I keep getting this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. ...line 80
...And by the way, I've already tried things like these:
this.parent.removeChild(this);
or
blob.parent.removeChild(blob);
or
stage.removeChild(blob);
but I still get the same error.
Here's my code:
package cem {
import flash.geom.*;
import flash.display.*;
import flash.events.*;
import flash.display.MovieClip;
import flash.utils.Timer;
public class Jeu extends MovieClip {
//Variables publiques
var decor: MovieClip = new Decor();
var chrono: cem.Chronometre;
var nextObject: Timer = new Timer(800, 0);
var _menu: MovieClip = new Menu();
var _btnJouer: MovieClip = new BoutonJouer();
var blob: cem.Blob;
var score: Score;
public function Jeu() {
// constructor code
//***********************************************Mettre menu***********************************************//
addChild(_menu);
addChild(_btnJouer);
_btnJouer.x = 500;
_btnJouer.y = 500;
_btnJouer.addEventListener(MouseEvent.CLICK, jouer);
}
//*****************************************************Jouer**************************************************//
function jouer(e: MouseEvent) {
removeChild(_menu);
addChild(decor);
decor.gotoAndStop(1);
chrono = new cem.Chronometre();
addChild(chrono);
chrono.demarrer();
score = new Score();
score.x = 600;
nextObject.addEventListener(TimerEvent.TIMER, creerBlobs);
nextObject.start();
}
//**************************************************Créer Bombes***********************************************//
function creerBlobs(e: TimerEvent) {
blob = new cem.Blob();
blob.x = Math.floor(Math.random() * (stage.stageWidth - blob.width));
addChild(blob);
blob.gotoAndStop(1);
blob.addEventListener("explosion", perdreVies);
}
//************************************************Perdre des vies*********************************************//
public function perdreVies(e: Event) {
decor.moinsVie();
decor.addEventListener("gameisover", _gameOver);
}
//************************************************Partie terminée*********************************************//
public function _gameOver(e: Event) {
blob.removeEventListener("explosion", perdreVies);
removeChild(blob);
chrono.arret();
addChild(_menu);
addChild(_btnJouer);
nextObject.stop();
nextObject.removeEventListener(TimerEvent.TIMER, creerBlobs);
nextObject.removeEventListener(TimerEvent.TIMER, creerBlobs);
addChild(score);
score.affichageScore.text = "votre score: " + chrono.secondes * 1000;
}
}
}
The var name blob can only reference one specific Blob object at a time.
Each time you create a new Blob you are reassigning the name blob to the last created one, losing the reference to the previous one.
That error says, at the time you call removeChild the specific Blob assigned to the name "blob" is not a child, it is not on the display list.
So its referencing the wrong Blob or its already removed from the display list.
To avoid errors u can also say something like if (blob.parent) remove child blob

variable in class not returning updated value

I'm relatively new to actionscript 3, and this one has me stumped. Here's my class;
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class Clicker extends MovieClip {
public var clicks:uint;
public var string:String;
public function Clicker() {
// constructor code
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
addEventListener(Event.ADDED_TO_STAGE, alignCentre);
string = "clicks: ";
clicks = 5; // I can change it here
}
public function clicked(e:MouseEvent):void{
clicks++;
trace(clicks); // it outputs updated value here
}
public function alignCentre(e:Event):void{
x = stage.stageWidth / 2 - width/2;
y = stage.stageHeight / 2 - height/2;
}
public function addedToStageHandler(e:Event):void{
this.stage.addEventListener(MouseEvent.CLICK, clicked);
}
public function get_clicks():uint{
trace(clicks); // gives me whatever I initialise it to in the constructor
return clicks;
}
}
}
I want to return the value of clicks from my Clicker class, but the value remains whatever I set it to in the constructor within get_clicks(), and I'm not sure why.
The variable has class scope, so why would it return the default value (in this case, 5) of clicks from get_clicks()? my clicked() method traces the correctly updated value. Is it a scope issue? I'm very confused.
This is my first frame where I create the object;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
var clicks:TextField = new TextField();
var circle:Clicker = new Clicker();
clicks.text = circle.get_clicks().toString();
trace(circle.get_clicks());
addChild(circle);
addChild(clicks);
As you'd expect from the problem I'm having, trace spits out 5 over and over, and Clicks doesn't change from 5.
Edit:
There was a mistake, but fixing it has caused clicks not to update at all. I had a library version of the movieclip on my first frame rather than adding the object to the frame with addChild. Now that I've added circle, clicks does not update as my clicked() method isn't being triggered when I click my object.
Resolved:
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
var clicks:TextField = new TextField();
var circle:Clicker = new Clicker();
var myTimer:Timer = new Timer(100,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
trace("Timer is Triggered");
trace(circle.get_clicks());
clicks.text = circle.get_clicks().toString();
addChild(circle);
addChild(clicks);
}
myTimer.start();
Rather than use the timeline and frames, I wrapped the code I wanted to repeat in a timer listener. Works exactly as intended.

Syntax Error AS3 using FlashDevelop

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 {

AS3 How to use addChild() in a separated as file

How can I add the MovieClip I have put into the array to the Stage?
The following code is a separated .as file and located at the same level with the main.fla
I have tried many times but I got the error message -
"ReferenceError: Error #1065: Variable stage is not defined. at Set1()
at main_fla::MainTimeline/frame1()"
How can I do? Thank for any help!!
package
{
import flash.display.MovieClip;
import flash.display.Stage;
public class Set1
{
private var map:Array=new Array();
public function Set1()
{
for (var i:Number=0; i<5; i++)
{
var cell_mc=new cell();
cell_mc.x = 50+ i*cell_mc.width;
cell_mc.y = 50;
cell_mc.className=i;
map[i] = cell_mc;
trace(map[i].className);
stage.addChild(map[i]);
}
}
}
}
You are a little mixed up. stage is not a magic variable, instead it is a property that is inherited from the DisplayObject base class. That property gets set internally when a display object is added to the stage. So in your case your class needs to either inherit from a DisplayObject– probably Sprite class. Or simply inject a reference to the Stage from the outside when you invoke your function
First you need to set the Main flash file class.You'll do that by clicking on stage in your fla. file and edit you class in properties(should look like this(class:Set1))
Code below should work fine
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
public class Set1 extends Sprite
{
private var map:Array=new Array();
public function Set1()
{
for (var i:Number=0; i<5; i++)
{
var cell_mc=new cell();
cell_mc.x = 50+ i*cell_mc.width;
cell_mc.y = 50;
cell_mc.className=i;
map[i] = cell_mc;
trace(map[i].className);
addChild(map[i]);
}
}
}
}