Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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{}
}
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
Yeah, sorry about my code, I'm really new at this... I did rename it to eraser_mc.
I tried targeting it directly, and like the idiot that I am, I realized that there more more errors above that one. The reason it wouldn't get the eraser is because of this error (I did some renaming, the variable menu now points to Movie Clip menu_scn1):
ReferenceError: Error #1056: Cannot create property menu_scn1 on flash.display.Stage.
I double checked my names, none of them conflict except the variable menu and the class Menu, but I don't think that's a problem.
menu_scn1 exists when the code is run, it is on the stage continually (but offscreen)
I also don't get why it says 'property menu_scn1' when the property is menu, and is pointing to a MovieClip.
Copy link to clipboard
Copied
It depends on how your code is targeting the object. The error makes it sound like you are trying to assign the stage a property as if you are coding...
stage.menu_scn1.... such that it looks like it is being treated as a stage property.
Copy link to clipboard
Copied
I've tried everything now, switching layers, names, code, which layer the code is on, two rewrites...
I've decided my code is so bad that I need to start over from scratch.
Thanks for helping me!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now