Be a Supporter!

[AS3] Anyone see a problem here?

  • 557 Views
  • 5 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
[AS3] Anyone see a problem here? May. 17th, 2012 @ 04:48 PM Reply

I'm getting this error:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
	at GameDocumentClass/addEmeralds()
	at GameDocumentClass()

I have this array declaration:

public var emeraldArray = [BlackEmerald, DarkBlueEmerald, GreenEmerald, LightBlueEmerald, RedEmerald, VioletEmerald, YellowEmerald];

This is my class constructor:

public function GameDocumentClass() {
			
			addPlayer();
			addRupees();
			addEmeralds();
			stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
		}

And here is my addEmeralds class:

private function addEmeralds():void
		{
			for(var i:int = 0; i < emeraldArray.length; i++){
				var emerald = new emeraldArray[i];
				stage.addChild(emerald);
				emeraldArray.push(emerald);
			emerald.x = Math.random()*stage.width;
    		emerald.y = Math.random()*stage.height;
			}
		}

The funny thing is that it loads all of the different Emeralds and into their random places, yet my mouse clicking is disabled.. not sure what's going on there.

Do you see anything wrong with my code with what I've provided?

Here's a link to the work in progress.

lobsterhat
lobsterhat
  • Member since: May. 4, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to [AS3] Anyone see a problem here? May. 17th, 2012 @ 05:22 PM Reply

At 5/17/12 04:48 PM, Barzona wrote: I'm getting this error:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
at GameDocumentClass/addEmeralds()
at GameDocumentClass()

I have this array declaration:

public var emeraldArray = [BlackEmerald, DarkBlueEmerald, GreenEmerald, LightBlueEmerald, RedEmerald, VioletEmerald, YellowEmerald];

This is my class constructor:

public function GameDocumentClass() {

addPlayer();
addRupees();
addEmeralds();
stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}

And here is my addEmeralds class:

private function addEmeralds():void
{
for(var i:int = 0; i < emeraldArray.length; i++){
var emerald = new emeraldArray[i];
stage.addChild(emerald);
emeraldArray.push(emerald);
emerald.x = Math.random()*stage.width;
emerald.y = Math.random()*stage.height;
}
}

The funny thing is that it loads all of the different Emeralds and into their random places, yet my mouse clicking is disabled.. not sure what's going on there.

Do you see anything wrong with my code with what I've provided?

Here's a link to the work in progress.

So the error is that you are trying to create a new instance of an object without calling its constructor (I think). This might help you out.

In your emeraldArray, try creating new instances of each of your classes (I'm assume they are classes):

public var emeraldArray = [new BlackEmerald(), new DarkBlueEmerald(), new GreenEmerald(), new LightBlueEmerald(), new RedEmerald(), new VioletEmerald(), new YellowEmerald()];

And then in your addEmeralds function:

private function addEmeralds():void
{
    for(var i:int = 0; i < emeraldArray.length; i++)
    {
        var emerald:MovieClip = emeraldArray[i] as MovieClip;
        stage.addChild(emerald);
        emeraldArray.push(emerald);
        emerald.x = Math.random()*stage.width;
        emerald.y = Math.random()*stage.height;
    }
}

Maybe that will work?

lobsterhat
lobsterhat
  • Member since: May. 4, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to [AS3] Anyone see a problem here? May. 17th, 2012 @ 05:37 PM Reply

Just noticed that in your addEmeralds function that you were pushing your local var emerald, which is an instance of one of your emerald objects, into the emeraldArray. That's going to create an infinite loop and I don't think that's what you wanted to do in the first place. Take out the line of code where you are pushing to the array.

private function addEmeralds():void
{
    for(var i:int = 0; i < emeraldArray.length; i++)
    {
        var emerald:MovieClip = emeraldArray[i] as MovieClip;
        stage.addChild(emerald);
        emerald.x = Math.random()*stage.width;
        emerald.y = Math.random()*stage.height;
    }
}
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to [AS3] Anyone see a problem here? May. 17th, 2012 @ 08:00 PM Reply

At 5/17/12 05:37 PM, lobsterhat wrote: Just noticed that in your addEmeralds function that you were pushing your local var emerald, which is an instance of one of your emerald objects, into the emeraldArray. That's going to create an infinite loop and I don't think that's what you wanted to do in the first place. Take out the line of code where you are pushing to the array.

private function addEmeralds():void
{
for(var i:int = 0; i < emeraldArray.length; i++)
{
var emerald:MovieClip = emeraldArray[i] as MovieClip;
stage.addChild(emerald);
emerald.x = Math.random()*stage.width;
emerald.y = Math.random()*stage.height;
}
}

You did it! Thanks. I put that in there because I was putting that into all the other arrays. I guess if I'm only taking something out of the array, there is no need to try to put something back into it.

lobsterhat
lobsterhat
  • Member since: May. 4, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to [AS3] Anyone see a problem here? May. 17th, 2012 @ 09:01 PM Reply

Glad to help! I like your shaky kidney by the way. Does he collect jewels to fight off renal failure?

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to [AS3] Anyone see a problem here? May. 17th, 2012 @ 11:11 PM Reply

At 5/17/12 09:01 PM, lobsterhat wrote: Glad to help! I like your shaky kidney by the way. Does he collect jewels to fight off renal failure?

I was actually thinking bean. haha

He's just an old movieclip that I'm using to learn some basic as3 stuff with.