Flash Compiler Errors- 1026 and 5000 - actionscript-3

I'm making a space invaders type game in flash, and am going through and messing with the code because I don't necessarily know ActionScript very well, and I'm getting two errors when I'm checking to see if my enemy will come up on the screen. One error is a 1026: Constructor functions must be instance methods and error two which is a 5000: The class SpaceVigilanteGame must subclass flash.display.MoveClip since it is linked to a library symbol of that type. If anyone knows where I may have messed up that would be greatly appreciated.
SpaceVigilanteGame.as
package
{
import flash.display.MovieClip;
public class SpaceVigilanteGame extends MovieClip
{
public var enemy:Enemy;
public function SpaceVigilanteGame()
{
enemy = new Enemy();
addChild( enemy );
}
}
}
Enemy Class
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
public function Enemy()
{
x = 10;
y = 10;
}
public function moveDownABit():void
{
y = y + 3;
}
}
}

Related

Main class was not the first class which was called in ActionScript 3.0

I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.

actionscript 3 - Error #2136 - simple issue

This is extremely basic, but to help me understand could someone please explain why this doesn't work. Trying to call a function from one as file to another, and get the following error.
Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code::Main()[C:\Users\Luke\Desktop\test\code\Main.as:12]
Error opening URL 'file:///test/Main.swf'
Main.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Enemy;
public class Main extends MovieClip
{
public function Main()
{
var enemy:Enemy = new Enemy();
}
public function test():void
{
trace("Test");
}
}
}
Enemy.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class Enemy extends Main {
public function Enemy() {
var main:Main = new Main();
main.test();
}
}
}
Assuming Main is your document class, you can't instantiate it. That might explain the SWF invalid data error.
What it looks like you are trying to do is access a function on Main from your Enemy. To do that you just need a reference to Main from inside your Enemy class. If you add the Enemy instance to the display list you can probably use root or parent to get a reference to Main. You could also pass a reference to Main through the constructor of your Enemy class:
public class Main {
public function Main() {
new Enemy(this);
}
public function test():void {
trace("test");
}
}
public class Enemy {
public function Enemy(main:Main) {
main.test();
}
}
From the constructor of the class Main you are creating the Object of Enemy. In the constructor of Enemy you are creating the Object of Main. Hence it continues to create those two objects until there is Stack overflow. It never reaches to the line where you have main.test();
if you wana get data frome main.as you can use the static var.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
// i well get this var in my Enemy as.
public var i:uint=1021;
public function txtuto() {
// constructor code
}
}
}`
// the Enemy.as
`package {
import flash.display.MovieClip;
public class Enemy extends MovieClip {
public static var tx:Main = new Main;
public function Enemy() {
trace(tx.i);
}
}
}
good luck.

calling movieclip of a class from another class, error#1009

i got error: method of null object reference. I'm so confused and don't know what is the real cause.
So i got a player movieClip on the stage which has an instance of "player_mc", which will be pass thru my Document class and into the Player class.
Player.as
import flash.display.*;
import flash.events.*;
public class Player extends MovieClip
{
public var myPlayer:MovieClip;
public function Player(player:MovieClip)
{
myPlayer = player;
addEventListener(Event.ENTER_FRAME,on_enter);
}
Document.as
import flash.display.*;
import Components.Player.Player;
public class Game_Main extends MovieClip
{
public var player:Player;
public function Game_Main()
{
player = new Player(player_mc);
}
}
now here i think is where the problem comes from. I have a green_enemy movieclip on the stage which has base class Enemy.
Enemy.as
import flash.display.MovieClip;
import Components.Player.Player;
import flash.events.Event;
public class Enemy extends MovieClip
{
var theplayer:Player;
public function Enemy()
{
this.addEventListener(Event.ENTER_FRAME,on_enter);
}
public function on_enter(e:Event):void
{
if (this.hitTestObject(theplayer.myPlayer)) //calls player_mc from Player class
{
trace("hi");
}
}
}
what I like to do on Enemy function is when Enemy collides with "player_mc" (which is on the stage) it will do something. Maybe my code is wrong.
Any help/tips will be appreciated. Thanks!
In your Enemy.as I see
var theplayer:Player;
not initialized. It is private, so you can not define it from outside. That means that exception comes from here
this.hitTestObject(theplayer.myPlayer)
You are trying to call myPlayer from null.
Try to define this variable while constructing Enemy class.
To prevent null exception you can check if theplayer is null
public function on_enter(e:Event):void
{
if (theplayer && this.hitTestObject(theplayer.myPlayer))
{
trace("hi");
}
}
To expand on what ZuyEL said, your variable isn't globally defined in your enemy class since you're just adding a var inside your enemy function. Instead, do the following
public var thePlayer:Player;
write the above after your Enemy class, before the Enemy function. Now it is available to the whole class and will no longer be null when you call it later.

Flash error 1046

I am new into as3, was following some tutorials and I made this code:
Class 1 (Enemy.as):
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
public function Enemy()
{
x = 100;
y = 0;
}
public function moveDownABit():void
{
y = y + 3;
}
}
}
and in main Class (AvoiderGame.as) :
package
{
import flash.display.MovieClip;
public class AvoiderGame extends MovieClip
{
public var enemy:Enemy;
public function AvoiderGame()
{
enemy = new Enemy();
addChild( enemy );
}
}
}
I Linked my Instant with it and all checked and working. also I added the main class to Classdocument of my fla file, though I am getting this error :
C:\Users\x\Documents\Flash\tuts\game1- avoider\Classes\AvoiderGame.as, Line 2 1046: Type was not found or was not a compile-time constant: Enemy.
C:\Users\x\Documents\Flash\tuts\game1- avoider\Classes\AvoiderGame.as, Line 6 1046: Type was not found or was not a compile-time constant: Enemy.
any ideas ?
Make sure that your classes AvoiderGame and Enemy are in the same package(folder) and its file names are the same as class names (AvoiderGame.as & Enemy.as).

AS3 - What is the best, or recommended way of wrapping Bitmaps into Sprite containers?

I'm learning AS3 using FlashDevelop IDE and Flex to compile.
I add images by creating a Bitmap class and then embedding a .png in the code, example:
Enemy.as
package enemies
{
import flash.display.Bitmap;
import flash.events.MouseEvent
[Embed(source="../../assets/gardengnome.png")]
public class Enemy extends Bitmap
{
public function Enemy()
{
trace("enemy constructed");
}
}
}
I have learned that in order to be able to handle MouseEvent I need to put this Bitmap into a Sprite container.
Now, not knowing any better this is the way I have done it:
I create a new variable to hold enemyContainer in Main.as and add it to the stage:
package
{
import enemies.Enemy;
import enemies.EnemyContainer;
import flash.display.Sprite;
import Player.Player;
public class Main extends Sprite
{
public var enemyContainer:EnemyContainer = new EnemyContainer();
public function Main():void
{
addChild(enemyContainer);
}
}
}
And then the EnemyContainer class calls the Enemy Bitmap that holds the graphic and adds it to itself as a child:
package enemies
{
import flash.display.Sprite;
import flash.events.MouseEvent
public class EnemyContainer extends Sprite
{
private var enemy:Enemy = new Enemy();
public function EnemyContainer()
{
trace("enemyContainer constructed");
addChild(enemy);
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Enemy");
}
}
}
I don't have enough experience yet to see any problems doing it this way. I can change the graphic in the Enemy Bitmap class without having to deal with anything else, and Main.as handles positioning of the EnemyContainer.
However, if there is a recommended, or more efficient way to handle this I'd like to learn it now before I get into a habit. Is there a better way to do this?
Thanks for any advice!
In your example it looks like you are creating 3 classes to implement the bitmap inside a sprite:
Enemy extends Bitmap
EnemyContainer extends Sprite
Main extends Sprite
Your method is totally valid, and you will not have a problem with it. In fact, there is no one best way to do this. What is best depends on the context. However, you may find it more easy to manage if you implement Enemy as part of EnemyContainer so that you have only two classes:
Main
EnemyContainer
In EmemyContainer you make a private class for the enemy bitmap like so:
package enemies
{
import flash.display.Sprite;
import flash.events.MouseEvent
public class EnemyContainer extends Sprite
{
[Embed(source="../../assets/gardengnome.png")]
private EnemyBitmap:Class;
public function EnemyContainer()
{
trace("enemyContainer constructed");
addChild(new EnemyBitmap()); // Creates a new instance of your bitmap class.
addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
trace("Clicked Enemy");
}
}
}
You may additionally find it helpful to use the mouseChildren and buttonMode property of the Sprite, but that will depend on how you want the mouse interaction to work.
== In reply To Comments ==
To position the bitmap on registration center:
var temp:Bitmap = new EnemyBitmap() as Bitmap;
temp.x = -temp.width/2;
temp.y = -temp.height/2;
addChild(temp);
What you are doing is OK
If you want less classes you could also embed the bitmap in your container class
public class Enemy extends Sprite
{
[Embed(source="../../assets/gardengnome.png")]
private var assetClass:Class;
private var asset:Bitmap;
public function Enemy()
{
//init the class
//do this in a seperate method, because the constructor is executed by a just in time compiler. Means less code better performance.
init();
}
private function init():void
{
asset = new assetClass();
addChild(asset);
}
}
A noble effort, but try this instead as a best practice:
public class Enemy extends Sprite
{
public function Enemy()
{
var bmp = new EnemyBitmap(); // what is currently your Enemy class should be EnemyBitmap
addChild(bmp);
}
}
This technique is called "implementation" where the desired class (Bitmap) is loaded into a container instead of the container extending the Bitmap class itself ("inheritance").