PDA

View Full Version : [AS3] Compilation Error


jpardoe
10-10-2006, 08:09 PM
I'm trying my hardest to understand ActionScript 3, and I follow the tutorials all well and good, but when it comes to creating my own files, I keep hitting this error when compiling:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
at Main$iinit()

What on earth does it mean? what have I done wrong? my Main.as code is as follows:

package {
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;

public class Main extends MovieClip {

private var cursorA:MovieClip = new MovieClip();

public function Main() {
cursorA = new cursorA();
this.addChild(cursorA);

stage.addEventListener(Event.MOUSE_LEAVE, cursorHide);
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorFollow);
Mouse.hide();
}

public function cursorHide(evt:Event):void {
cursorA.visible = false;
}

public function cursorFollow(evt:MouseEvent):void {
if (!cursor.visible) cursorA.visible = true;
cursorA.x = stage.mouseX;
cursorA.y = stage.mouseY;
evt.updateAfterEvent();
}
}
}

I've got 2 movieclips in the library, and i've the linkage stuff (only referred to cursorA in the class though)

senocular
10-10-2006, 08:26 PM
you wouldnt use
cursorA = new cursorA();
since cursorA is not a class and already an instance of MovieClip

jpardoe
10-10-2006, 08:44 PM
Then could you explain why this code actually uses it?? As far as I can see it's the same process?

package{

import flash.display.*;
import flash.events.*;

public class Car extends MovieClip{

public var mc:MovieClip;

public function Car(){
mc = new FunnyCar();
this.addChild(mc);
mc.y = stage.stageHeight/2;
mc.doubleClickEnabled = true;
mc.addEventListener("doubleClick",doubleClick_handler);
mc.addEventListener("click",click_handler);
}

private function doubleClick_handler(e:Event){
e.target.addEventListener("enterFrame",enterFrame_handler);
}
private function enterFrame_handler(e:Event){
e.target.x += 3;
}
private function click_handler(e:Event){
e.target.removeEventListener("enterFrame",enterFrame_handler);
}
}
}

senocular
10-10-2006, 08:46 PM
That code doesnt :-/

jpardoe
10-10-2006, 08:47 PM
mc = new FunnyCar(); (same situation - a movieclip in the library with Class specified as FunnyCar)

jpardoe
10-10-2006, 08:57 PM
When I remove the cursorA = new cursorA(); I no longer get the error, but I don't see my cursor on the screen. (the default cursor just disappears).

>>>very confused<<<

senocular
10-10-2006, 09:09 PM
thats a different situation. There you are assigning a variable 'mc' to be a new instance of the class FunnyCar.

variable:mc
class:FunnyCar

In your first example you're using the same identifier for both variable and class

variable:cursorA
class:cursorA

Of course thats not possible - trying to define a class to equal an instance of itself just isn't going to work. cursorA already represents the class; it can't also be an instance of the class. You have to use some other identifier... not to mention that you already defined cursorA to be a new MovieClip in the body of the class. Throwing in that cursorA = new cursorA(); just makes no sense.

jpardoe
10-11-2006, 08:02 AM
Thanks, I noticed that, but when I changed one of them to cursor1 instead, it still spat out that instantiation error.

Changed code:

public class Main extends MovieClip {

private var cursor1:MovieClip = new MovieClip();

public function Main() {
cursorA = new cursor1();
stage.addChild(cursorA);

Sorry if i'm being completely stupid...

senocular
10-11-2006, 09:06 AM
You're still confusing variables and classes.

When you use new SOMETHING() that SOMETHING is a class. That means it must be a class defined inside flash (like Array or Sprite) or it must be something you have saved in a separate .as file defined using the class keyword. Classes are what provide the blue prints for new instances.

Instances are saved to variables. Variables are usually defined in your class body (though local variables can be defined in functions as well, with the intent of only using them within that function call).

You have this line:
cursorA = new cursor1();
That is saying the variable cursorA is being made an instance of the class cursor1. cursor1, however, I know is not a class because you have it as a variable in the Main class body. There its defined as the type MovieClip and even created as a movie clip. The new keyword will not work with cursor 1 because it is an instance of a class, not a class itself. Not only that, you never defined cursorA as being a variable of the class which is in error right there. You would have to define it like you did cursor1 to use it as you had in this line - either that or put a 'var' in front of it to make it a local variable of the function.

What you need to do is drop that line of code, completely. Kill it, its not what you want. You already have your cursor1, and that would be added to the stage using addChild. The only thing that might change is you might want to change MovieClip for its type to something else if you have another class that defines your cursor.

mathew.er
11-22-2006, 04:33 PM
Sorry for bumping an older thread, but even google sent me here =)

Im getting the same error in my code. Im creating an instance of a class extending Sprite. It works when I dont use any arguments for the constructor. Is is then impossible to pass arguments to these sort of classes or am I doing something wrong?

code excerpt for constructor call...
public function Test ( ... )
{
...

new CubicBezier ( [[100, 100],
[200, 100],
[200, 200],
[300, 200]] );
}
and the constructor definition
public class CubicBezier extends Sprite
{
...

public function CubicBezier ( matrix : Array = null,
resolution : uint = 50,
thickness : Number = 1,
rgb : uint = 0x000000,
alpha : Number = 1 ) : void
{
m = matrix;
res = resolution;
lineSettings = new LineDrawSettings ( thickness, rgb, alpha );

//super ();
}

...
}

Krilnon
11-22-2006, 05:23 PM
I wasn't able to duplicate your error using the code you provided, the CubicBezier instantiated itself with arguments passed.

Are you using Flex or the AS3 preview?

senocular
11-22-2006, 06:13 PM
its possible LineDrawSettings might not be a class. Where/how is that defined?

mathew.er
11-22-2006, 08:23 PM
@Krilnon
It Flex Builder 2

@Sen
Your right, it has something to do with the LineDrawSettings class. Well, I suppose its a class :)
It doesnt mark any errors in the editor, it happens only as a runtime error. The LineDrawSettings is just a placeholder for few variables. Its defined outside of the package block and the code is
class LineDrawSettings
{
public var thickness : Number;
public var rgb : uint;
public var alpha : Number;

public function LineDrawSettings ( thickness : Number, rgb : uint, alpha : Number )
{
this.thickness = thickness;
this.rgb = rgb;
this.alpha = alpha;
}
}
and the runtime error says:
TypeError: Error #1007: Instantiation attempted on a non-constructor.
at metju.geom::CubicBezier$iinit()
at metju.geom::CubicBezier$cinit()
at global$init()
at Test3D$iinit()
at WEB$iinit()