1119: Access of possibly undefined property monster through a reference with static type Enemy. AS3 - actionscript-3

Main.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
public var _root:MovieClip;
public var monsterContainer:MovieClip = new MovieClip();
public var delay = 30;
public function Main(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
addChild(monsterContainer);
delay -= 1;
if(delay <= 0){
var spawn:Slime = new Slime();
spawn.x = startPoint.x;
spawn.y = startPoint.y;
monsterContainer.addChild(spawn);
delay = 30;
}
}
}
Arrow.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Arrow extends MovieClip {
public var _root:MovieClip;
public var facingID;
public function Arrow(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
trace(_root.monsterContainer == null);
}
}
Enemy.as
package{
import flash.display.MovieClip;
import flash.events.*;
public class Enemy extends MovieClip {
public var _root:MovieClip;
//Status
public var monsterSpeed;
public var facing = "Right";
//CallingArrow
public var down:Down = new Down();
public function Enemy(){
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, enterFrameEvents);
}
function beginClass(e):void{
_root = MovieClip(root);
}
function enterFrameEvents(e):void{
//Facing Movement
if(_root.pausing == false){
if(facing == "Right"){
this.x += monsterSpeed;
}else if(facing == "Left"){
this.x -= monsterSpeed;
}else if(facing == "Down"){
this.y += monsterSpeed;
}else if(facing == "Up"){
this.y -= monsterSpeed;
}
}
}
}
Down.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Down extends Arrow {
public function Down(){
facingID = "Down";
}
}
Slime.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Slime extends Enemy {
public function Slime(){
monsterSpeed = 5;
}
}
and there is no additional code on timeline just stop();
I got 1119 error, when i want to access a movieClip inside slime, i give it monster for the instance name, please help !
Download Link : http://www.mediafire.com/download/hz5tptkgftwdipw/Tower_Defense.rar
It's only 15KB and using CS6 Please help !

Turn on Debugging
The code you're sharing is more than you probably need (.rar file included). To find the cause of the problem you (and those on StackOverflow) need to know what line you're programming is running into this error. If you're using Flash IDE CS6, the can be enabled by going to your publish settings and enabling "Permit Debugging". This will take your ambiguous error...
null object reference at myDocument/doSomething()
...to a much clearer...
null object reference at myDocument/doSomething() package\myClass.as:20
...which now denotes which line in your code to look for your issue.
Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without it.
Enemy.monster
This is the crux of the issue: somewhere, you're calling on Enemy.monster, and there is no property on your Enemy class that's called that (method or otherwise).

Related

ActionScript 3.0 Type Was Not Found Or Was Not A Compile-Time Constant

I just started programming a game and I when I ran my code it said
1064:Type Was Not Found Or Was Not A Compile-Time Constant:Event
1064:Type Was Not Found Or Was Not A Compile-Time Constant:Mouse Event
Here is the code:
package{
public class Script_1 {
public static const STATE_INIT:int = 10
public static const STATE_PLAY:int = 20
public static const STATE_GAME_OVER:int = 30
public var gameState:int = 0
public function gameLoop(e:Event):void{
switch(gameState) {
case STATE_INIT:
initGame();
break;
case STATE_PLAY:
playGame();
break;
case STATE_GAME_OVER:
gameOver();
break;
}
}
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
gameState = STATE_INIT;
}
stage.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
public function initGame():void{
stage.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
clicks = 0
gameState = STATE_PLAY;
}
public function playGame(){
if (clicks >= 10){
gameState = STATE_GAME_OVER;
}
}
public function onMouseClickEvent(e:MouseEvent):void{
clicks++;
trace("mouse click number:" + clicks);
}
public function gameOver():void{
stage.removeEventListener(MouseEvent.CLICK, onMouseClickEvent);
gameState = STATE_INIT;
trace("game over");
}
}
}
This is in a file called Script_1.as
You need to import those classes with the import statement. This statement is required for each class that is missing and belongs above the class definition:
package
{
// Imports.
import flash.events.Event;
import flash.events.MouseEvent;
public class Script_1
{
// ..
}
}
Also, some misc things I noticed:
You're using addEventListener() but Script_1 does not extend EventDispatcher or at least implement IEventDispatcher. Based on the events you're trying to listen for, Sprite seems most suitable.
It looks like your class should either be Game or your constructor function Game() should be Script_1().

Inherit MovieClip From Another Class

Hey I seem to be having some problems inheriting Movie-clip from a class, I'm fairly new to as3, I've had a look around and can't tell if I'm doing something fundamentally wrong or not.
Let me show you
So I have a class I want to use to move EVERYTHING but the player sprite. So I want everything but the player to extend it. (or so I'm assuming.)
So I declare my class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
function brackets and so on...
(I'm just importing everything in an attempt to avoid errors)
I then have a class I want to inherit everythingContainer and Movieclip
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
Yet when I run this I get the error:
Line 1 5000: The class 'code.Tree1' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Why am I getting this error?
Any help would be greatly appreciated!
I haven't got the full code to run yet so there may still be other obvious bugs laying about.
everythingContainer
Full code:
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
public class everythingContainer extends MovieClip {
var speed: Number = 4;
var wpressed: Boolean = false;
var apressed: Boolean = false;
var spressed: Boolean = false;
var dpressed: Boolean = false;
var xprev:int = 0;
var yprev:int = 0;
public function everythingContainer() {
// constructor code
trace ('Container started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Container init removed');
}
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
this.addEventListener( Event.ENTER_FRAME, containerEveryFrame);
}
public function containerEveryFrame (Event): void {
if (stage.contains(Main.player)) {
xprev = this.x;
yprev = this.y;
checkPlayerMovement();
}
}
// check the set keypress variables
public function checkPlayerMovement () : void {
if (wpressed) {
this.y -= this.speed;
}
if (spressed) {
this.y += this.speed;
}
if (apressed){
this.x -= this.speed;
}
if (dpressed) {
this.x += this.speed;
}
}
//assign key presses to variables
public function onKeyPress (event:KeyboardEvent):void {
//up
if (event.keyCode == 87){
wpressed = true;
}
//down
if (event.keyCode == 83) {
spressed = true;
}
//left
if (event.keyCode == 65){
apressed = true;
}
//right
if (event.keyCode == 68) {
dpressed = true;
}
}
//reset key press variables
public function onKeyRelease (event:KeyboardEvent) : void {
//up
if (event.keyCode == 87){
wpressed = false;
}
//down
if (event.keyCode == 83) {
spressed = false;
}
//left
if (event.keyCode == 65){
apressed = false;
}
//right
if (event.keyCode == 68) {
dpressed = false;
}
}
}
}
Main (there's some other stuff going on in here, but at the minute I'm just trying to get the trees working with my other class.)
package code
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.geom.Rectangle;
import flashx.textLayout.container.ContainerController;
public class Main extends MovieClip
{
//public static var main
public static var player:PC;
//public static var firstenemy: WolfEnemy;
public static var MainContainer:everythingContainer;
public function Main()
{
// constructor code
trace('main started');
if (stage)
{
init();
}
else
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
function init(eventInfo:Event = null):void
{
if (eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Main init removed');
}
MainContainer = new everythingContainer ;
MainContainer.x = stage.stageWidth / 2;
MainContainer.y = stage.stageHeight / 2;
stage.addChild(MainContainer);
player = new PC();
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
stage.addChild(player);
//firstenemy = new WolfEnemy();
//firstenemy.x = 100;
//firstenemy.y = 100;
//stage.addChild(firstenemy);
stage.addEventListener( Event.ENTER_FRAME, everyFrameMain);
}
// check if an enemy hits the player.
/*public function enemycollison(): void {
if(firstenemy.hitTestObject(player)){
trace ('hit enemy');
player.health--;
firstenemy.kill();
}
}*/
// manage events that need to haapen globally for every frame
public function everyFrameMain(Event):void
{
/*if (stage.contains(firstenemy)){
enemycollison();
} */
//root.scrollRect = new Rectangle(player.x - 400, player.y - 300, 800, 600);
}
// finish and close game
public function endgame():void
{
}
}
}
and finally my tree class
package code {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import code.Main;
import code.everythingContainer;
public class Tree1 extends everythingContainer {
public function Tree1()
{
// constructor code
trace ('Tree1 started');
if(stage) init();
else
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(eventInfo:Event = null):void
{
if(eventInfo != null)
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
trace ('Tree1 init removed');
}
this.addEventListener( Event.ENTER_FRAME, tree1EveryFrame);
}
public function tree1EveryFrame (Event): void {
playercollision();
}
public function playercollision(): void {
if(this.hitTestObject(Main.player)){
trace ('hit wall');
Main.player.x = Main.player.xprev;
Main.player.y = Main.player.yprev;
}
}
}
}
Probably due to linkage errors inside Fla-file.
RMB on library-item: Tree1
Export for ActionScript + Export in frame 1 + "class: code.Tree1"
File/Publish Settings/Actionscript Settings (the small wrench, right of Script-dropdown)
Source Path (add the linkage to where the compiler can find your package-folder), usually for me I crate a folder next to the fla file called src or something like that so the code-file would be found at "/MyProject/src/code/Tree1.as", in that case I add "./src/" inside Source path inside Advanced ActionScript 3.0 settings
Added an example project in Flash CS6 found at url:
https://user.iter.org/filesharing/?uid=927205f7-cdfe-4915-a175-bc87f64af444
that is available for ~40 days.
Project structure in that file:
"/MyProject/DeepInheritage.fla"
"/MyProject/src/code/Foobar.as"
"/MyProject/src/code/Tree1.as"
Foobar.as which extends MovieClip
Tree1 library item which extends Foobar
That should be the exact same thing that you described in your issue, meaning that there is nothing wrong with that approach, it is just a matter of finding what is wrong. Most likely that is due to errors inside FLA-file, but might be something else.
code files:
package code {
import flash.display.MovieClip;
public class Foobar extends MovieClip {
public function Foobar() {
trace("foobar ctor()");
}
}
}
package code {
import flash.display.MovieClip;
public class Tree1 extends Foobar {
public function Tree1() {
trace("Tree1 ctor()");
}
}
}

Error #1009 utilizing event listener in external class file

(New to AS3/Flash so go easy on me if I'm oblivious to something...)
Trying to utilize external class files to create a continuous scrolling background image. I got it to work by putting it in the document class file, but trying to put it in its own external class file and calling it from the document class file brings up the error in my title.
Document Class File:
package {
import flash.display.MovieClip;
import org.masteringmoneybasics.piggy._class_BG
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
new _class_BG();
}
}
}
External Class File:
package org.masteringmoneybasics.piggy {
import flash.display.*
import flash.events.Event
import flash.display.Bitmap;
import flash.display.BitmapData;
public class _class_BG{
//BG Variables
var scrollSpeed:uint = 6;
var bgLeft:Bitmap
var bgRight:Bitmap
[Embed(source="../../../assets/side_of_mountain.png")]
private var bgImage:Class;
public function _class_BG() {
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll); //<<<<<< ERROR HERE
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}
If I remove the stage. reference in the event listener, it runs without errors but the images don't appear on the stage like they are supposed to.
What am I doing wrong?
You tried to In Main class external class initialized in stage. In fact, stage not arrived. see a your _class_BG, addChild() is a little wrong. because you not check Main class, perfectly added stage.
FlashBuilder this problem must be careful. first added to stage on Main class after external class(related DisplayObject) fully loaded or initialized.
refer a following code.
In _class_BG Class Carefully addEventListener(Event.ADDED_TO_STAGE,init);
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
var sp:_class_BG = new _class_BG();
addChild(sp);
}
}
}
package {
import flash.display.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
public class _class_BG extends Sprite {
//BG Variables
private var scrollSpeed:uint = 6;
private var bgLeft:Bitmap
private var bgRight:Bitmap
[Embed(source="../asset/myTestImage.png")]
private var bgImage:Class;
public function _class_BG()
{
if(!stage)
addEventListener(Event.ADDED_TO_STAGE, init);
else
init();
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//This adds two instances of the background to the stage
bgLeft = new bgImage();
bgRight = new bgImage();
bgLeft.height = 500;
bgRight.height = bgLeft.height;
bgLeft.width = 1300;
bgRight.width = bgLeft.width;
bgLeft.x = 0;
bgRight.x = bgLeft.width;
addChild(bgLeft);
addChild(bgRight);
//Adds an event lsitener to the stage
stage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
public function moveScroll(e:Event):void{
bgLeft.x -= scrollSpeed;
bgRight.x -= scrollSpeed;
if(bgLeft.x < -bgLeft.width){
bgLeft.x = bgRight.x + bgRight.width;
}else if(bgRight.x < -bgRight.width){
bgRight.x = bgLeft.x + bgLeft.width;
}
}
}
}
Only the top level Displayable has access to scene. What's more, it is read-only, which means that you cannot pass it through the original stage attribute.
The simplest way to go would be... I don't know, perhaps passing the stage to your constructor? The relavant parts of the constructor would be:
public function _class_BG(myStage : Stage) {
// SNIP
//Adds an event lsitener to the stage
myStage.addEventListener(Event.ENTER_FRAME, moveScroll);
}
And in Main (in which you do have acces to the stage):
public class Main extends MovieClip {
public function Main() {
//Create instance of background class
addChild(new _class_BG(stage));
}
}
You should think about some other means of building your logic, passing stage around will get hairy quickly. But it should work.
EDIT:
stage -> myStage; also, addChild in Main().

Access of undefined property Keyboard (AS3)

I'm new to Actionscript 3 and I'm wanting to allow a circle to move down using the down arrow on the keyboard. Here's my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y = y+=speed;
}
}
}
}
}
When I test it, nothing happens when I press the down key. Anyone know what's wrong with the code?
Try adding KeyBoard events to the stage instead of to the class. Additionally, I would not nest functions like that, bad practice in general. Also the line y = y+=speed; is confusing, shouldn't it just be y += speed; ?
EDIT: Sorry, I guess stage will be null in the constructor, I've added a ADDED event listener.
Try this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(Event.ADDED, onAdded);
}
private function onAdded(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
}
private function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y += speed;
}
}
}
}

as3 error 1063 with timer

getting error 1061: Call to a possibly undefined method stop through a reference with static type flash.events:TimerEvent.
on my as3 class. I'm just starting to learn as3 and cant figure out whats causing the error. code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class game extends MovieClip
{
//assign types to var names
//allows values and variables to be acessed in methods
public var as1:astroid;//astroids
public var ship1:ship;//ship
public var timer:Timer;
public function game()
{
//astroid
as1=new astroid();
addChild(as1);
//ship
ship1=new ship();
addChild(ship1);
//timer
timer=new Timer(25);//every n frames
timer.addEventListener( TimerEvent.TIMER, onTick );//attach function to timer
timer.start();//start timer
}
public function onTick( timer:TimerEvent ):void
{
//animate astroid
as1.moveDown();
//move ship
ship1.x = mouseX;
ship1.y = mouseY;
if(ship1.hitTestObject(as1))
{
timer.stop();//error on this line!
}
}
}
}
Rename timer to event in your event handler:
public function onTick( event:TimerEvent ):void
Also, in Flash CS5, go to File > Publish Setting > Flash, and turn on "Permit debugging". That should give you more useful error messages.