FlashDevelop Errors in Sample program - actionscript-3

I'm a beginner to this program, the code is
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
/**
*...
* #author FXDS
*/
public class Main extends Sprite
{
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);
// entry
var greeting:TextField = new TextField();
}
greeting:text = ("Hello World");
greeting.x = 100;
greeting.y = 100;
addChild(greeting);
}
}
However, when I run it, the program complains with the following error :
\src\Main.as(23): col: 17 Error: A constructor cannot specify a return type.
Any Help would be greatly appreciated.

You get that error because you have a little mistake in your code :
public function Main():Void { }
should be
public function Main():void { }
the compiler didn't know Void (with a capital V) which is different from void.
and to avoid that mistake in the future, you can simply write :
public function Main() { }
Hope that can help.

Related

Error in Sample program FlashDevelop

Here's the code for the sample program, It's a Hello World program.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
/**
* …
* #author Your Name
*/
public class Main extends Sprite
{
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);
// entry
var greeting:TextField = new TextField();
}
greeting.text = (“Hello, World”);
greeting.x = 100;
greeting.y = 100;
addChild(greeting);
}
}
When I run it, I get 2 syntax errors, both on line 43 \src\Main.as(43): col: 18 Error: Syntax error. What I'm doing wrong? I googled the problem and no luck, Please Help!!! Any Help would be extremely appreciated!! The link to the tutorial is here "https://precisioncode.wordpress.com/2012/09/16/a-tutorial-in-as3-with-flashdevelop/".
The quotes around “Hello, World” are copy pasted from the web and are not the character you get when you type shift and the quote key, delete the copied ones and type them yourself.

AS3 ArgumentError: Error #1063: Expected 0, got 1

Can anyone can help me with this issue.
I have no idea with this issue.
Just keep getting this issue.
Already try modify from this
function init(e:Event = null) to function init() or function init(e:Event)
Appreciated who can help me with this.
If no issue on this, it should show food not my stage.
package {
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Main extends MovieClip {
var snake:Array;
var score:Number;
public function Main() {
// constructor code
if(stage)
addEventListener(Event.ADDED_TO_STAGE, init);
else
init();
}
function init(e:Event = null):void {
//initialize
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(e:Event):void {
var food:Food = new Food();
this.addChild(food);
trace("food");
}
}
}
Your code is almost correct.
Replace if (stage) to if (!stage).
If error still happens attach FLA.

Error: Call to a possibly undefined method

I have the simplest code ever. Main class:
package
{
import field.Field;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
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);
var field:Field = new Field();
addChild(field);
field.test();
}
}
}
and a Field class:
package field
{
import flash.display.Sprite;
public class Field extends Sprite
{
public function Field()
{
super();
}
public function test():void
{
}
}
}
test method is presented.
But when I try to compile I get this:
Main.as(26): col: 10 Error: Call to a possibly undefined method test.
field.test();
How could this be happening?
field is your package, that's why you can not do field.test(). So you have to choose another name of your Field instance. You can do like this :
var _field:Field = new Field();
addChild(_field);
_field.test();
Hope that can help.

AS3 Preloader Timing

I did the following
package classes
{
// imports removed
public class Main extends MovieClip {
// vars removed
public function Main():void {
trace('--> Main Function started ...');
/**************************************
Preloader
**************************************/
var preLoader:PreLoader = new PreLoader(); // init
stage.addChild(preLoader); // to Stage
stage.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoader.preloaderProgress); // calling the function
doThis();
}
}
With the PreLoader Class looking like this:
package classes {
// imports removed
public class PreLoader extends MovieClip {
public var totalBytes:uint;
public var loadedBytes:uint;
public function PreLoader() {
trace('--> PRELOADER Started ...');
this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
this.loaderBar.scaleX = 0;
}
private function addedHandler(e:Event):void {
trace('Adding Preloader ...');
totalBytes = this.stage.loaderInfo.bytesTotal;
trace('PL: Total ' + totalBytes + ' / ' + loadedBytes);
this.removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
preloaderProgress(e);
}
public function preloaderProgress(e:Event):void {
trace('Progress...');
loadedBytes = this.stage.loaderInfo.bytesLoaded;
this.loaderBar.scaleX = loadedBytes / totalBytes;
this.loaderBar.alpha = 1 - this.loaderBar.scaleX;
if (totalBytes == loadedBytes) {
trace('Removing PreLoader ...');
this.parent.removeChild(this);
}
}
}
}
The Problem: The Preloader is not displayed BEFORE the inital SWF-file has finished loading. Even all the tracing outputs start when the main movie is finished - i did some profiling on 'slow connections'.
If you wonder: doThis() is loading data from a xml-file, from here everything is fine, tracing outputs are at the right time (but too late at all :D)
Thank you in advance!
Edit: Maybe the question is: Is there a way to determine when the main movie is starting to load?
Make sure that you don't have any content other than the preloader on stage in the first frame. Otherwise all that content will have to load before the preloader starts up.
If you are working in FlashIDE you have to put your preloader into the first frame. If you prefer to use another IDE you should use Frame metatag. Example:
Main.as
package com.sample.preloader
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author Author
*/
[Frame(factoryClass="com.sample.preloader.Preloader")]
public class Main extends Sprite
{
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);
// entry point
}
}
}
Preloader.as
package com.sample.preloader
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
/**
* ...
* #author Author
*/
public class Preloader extends MovieClip
{
public function Preloader()
{
if (stage) {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO show loader
}
private function ioError(e:IOErrorEvent):void
{
trace(e.text);
}
private function progress(e:ProgressEvent):void
{
// TODO update loader
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
stop();
loadingFinished();
}
}
private function loadingFinished():void
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
// TODO hide loader
startup();
}
private function startup():void
{
var mainClass:Class = getDefinitionByName("com.sample.preloader.Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}

#2015: Invalid BitmapData

In my class, if I create bitmapData like this:
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
I get the following error:
ArgumentError: Error #2015: Invalid BitmapData.
But if I declare the variable inside the method:
public function object():void {
var tImage:BitmapData;
tImage = new BitmapData(30,30,false,0x000000);
}
It works fine. WHY!?!?! It's driving me crazy.
Thanks guys!
I think it might be some other code in your class.
The following works, but I didn't name the function "object" (since I'm guessing that's a reserved word??)
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
This simplified version below also works too:
package
{
/**
* ...
* #author your name here
*/
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
public class TestBitmap extends MovieClip
{
private var tImage:BitmapData;
public function TestBitmap():void
{
tImage = new BitmapData(30,30,false,0x000000);
}
}
}
You declared tImage as private...
private var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Its should be
var tImage:BitmapData;
public function object():void {
tImage = new BitmapData(30,30,false,0x000000);
}
Derp