Skip to main content
Inspiring
September 12, 2015
Question

1046: Type was not found is grabbing random MovieClips

  • September 12, 2015
  • 6 replies
  • 437 views

I'm pretty new to AS3, and I've been making a drag and drop game. I made 2 separate classes, each never referencing the other. One is for draggable items and one is for the menu.

Menu.as contains the code to bring the menu up and down at the press of the spacebar: function menuUp and menuDown

Draggable.as contains the startDrag and stopDrag codes connected to mouse clicks. The basic stuff.

When I test the movie, I get this error:

C:\Users\######\Documents\SonicMenu\Menu.as, Line 5, Column 2 1046: Type was not found or was not a compile-time constant: explosion1.

Explosion1 is a movieclip that is attached to the base class of Draggable, but is not referenced in Menu.as

Here is the line that Flash points to when receiving the error.

package {

  import flash.display.MovieClip;

  import flash.events.KeyboardEvent;

*/this line is the error one/* public class Menu extends MovieClip {

  public function Menu() {

  stage.addEventListener(KeyboardEvent.KEY_DOWN, bring_up_menu_scn1);

  stage.addChild(this)

  }

Here are the entire class files. I know my code is really bad, but I really need this problem fixed.

Draggable.as

package {

  import flash.display.MovieClip;

  import flash.events.MouseEvent;

  public class draggable extends MovieClip {

  public function draggable() {

  addEventListener(MouseEvent.MOUSE_DOWN, drag);

  addEventListener(MouseEvent.MOUSE_UP, stop_drag);

  }

  function drag(evt: MouseEvent): void {

  stage.addChild(this)

  startDrag();

  }

  function stop_drag(evt: MouseEvent): void {

  stage.addChild(this)

  stopDrag();

  }

  }

}

Menu.as

package {

  import flash.display.MovieClip;

  import flash.events.KeyboardEvent;

  public class Menu extends MovieClip {

  public function Menu() {

  stage.addEventListener(KeyboardEvent.KEY_DOWN, bring_up_menu_scn1);

  stage.addChild(this)

  }

  var menu_up: Boolean = false

  function bring_up_menu_scn1(evt: KeyboardEvent): void {

  if (evt.keyCode == 32) {

  if (menu_up == false) {

  menuUp()

  } else {

  menuDown();

  }

  }

  }

  function menuUp(): void {

  x = 0

  y = 0

  menu_up = true

  stage.setChildIndex(this, stage.numChildren - 1)

  }

  function menuDown(): void {

  x = 550

  y = 400

  menu_up = false

  }

  }

}

I hope you can see how it works... It's really bad.

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

Ned Murphy
Legend
September 13, 2015

It might just be an issue of capitalization.  Class names do not require that you use a capital letter but it is a convention that helps others understand your code easier.  You seem to be jumping between using capitalized versions and non-capitalized versions in what you explain, so check that all out and be consistent in the way you code it.

mcdermittAuthor
Inspiring
September 13, 2015

Ok, that fixed that problem, but now I get

Error #1009: Cannot access a property or method of a null object reference.

When calling a MovieClip eraser. The eraser has the parent structure menu.eraser and the instance name is the same as the variable it has assigned to it and it's name in the library, and it's not linked to any classes. I tried changing

var eraser:MovieClip = menu.eraser

To

var eraser:MovieClip = menu.eraser_sprite

And I changed the instance name correctly, too. It still spits out the error.

I tried giving everything different names and I get the same thing. Here is the code that handles it:

var menu:MovieClip = scn1_dnd_menu

var eraser_sprite:MovieClip = menu.eraser_mc

var eraser_act:Boolean = false

eraser_sprite.addEventListener(MouseEvent.CLICK, activate_eraser);

stage.addEventListener(KeyboardEvent.KEY_DOWN, deactivate_eraser);

function activate_eraser(evt:MouseEvent):void{

  eraser_sprite.startDrag();

  Mouse.hide();

  addChild(eraser_sprite);

  eraser_act = true

}

function deactivate_eraser(evt:KeyboardEvent):void{

  if(evt.keyCode == 27){

  if(eraser_act){

  eraser_sprite.stopDrag();

  Mouse.show();

  eraser_act = false

  menu.addChild(eraser_sprite);

  eraser_sprite.x = 524

  eraser_sprite.y = 34.55

  }

  else{}

  }

  else{}

}

Ned Murphy
Legend
September 13, 2015

SInce you are confusing the expolanation as much as the names it is hard to tell you where you are going wrong... you just explained that you renamed it to be eraser.sprite, but in your code you show eraser_mc.  My advice is to not try to rename anything and just target the object you intend to without giving it another reference... meaning.,... get rid of the var eraser_sprite:MoiveClip... etc

And just target the object directly...

menu.eraser_mc.addEventListener(MouseEvent.CLICK, activate_eraser);

function activate_eraser(evt:MouseEvent):void{

  MovieClip(evt.currentTarget).startDrag();  // use the evt to target the object

  Mouse.hide();

  addChild(MovieClip(evt.currentTarget));

  eraser_act = true

}