Error 1009 : Cannot access a property of method of a null object reference - actionscript-3

I don't understand what is going on
This is my Main.as
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var pirkles:Circles = new Circles()
public function Main() {
gotoAndStop(1)
playbtn.addEventListener(MouseEvent.CLICK, playscreen)
}
public function playscreen(event:MouseEvent):void {
gotoAndStop(2)
addChild(pirkles)
}
}
}
And this is my Circles.as
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.events.MouseEvent;
public class Circles extends MovieClip{
public function Circles():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
this.y = 175
this.x = 10
}
public function MOVE(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
this.x = this.x+10
}
else if (event.keyCode == Keyboard.LEFT) {
this.x = this.x-10
}
else if (event.keyCode == Keyboard.UP) {
this.y = this.y-10
}
else if (event.keyCode == Keyboard.DOWN) {
this.y = this.y+10
}
}
}
}
Now I get an error that says there is a problem at line 11 of my Circles.as and at line 8 of my Main.as. However, at those lines I don't understand what is causing the problem. I added a event listener at line 11, but when I take it out it works. Also, at line 8, I Just defined a variable.

you can't access the stage in a class constructor.
So the line
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
is causing the error.
If you need to access the stage, add a listener in the constructor for the ADDED_TO_STAGE event, and in the callback function you will be able to access the stage
So:
public function Circles():void {
this.addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
this.y = 175
this.x = 10
}
private function onAddedToStage (evt:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
}

Related

Projectiles not moving as they are supposed to

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
public function supportForce()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
}
GreenLight.x -= defaultSpeed;
}
}
}
So the projectile here is Projectile1 and it is declared as GreenLight in the keyCheck Function. So in the same function (the ENTER.FRAME function) GreenLight is supposed to move to its left. This does not happen and also an error saying TypeError: Error #1009: Cannot access a property or method of a null object reference.
at supportForce/keyCheck() appears. Thank you in advance.
In your keyCheck function, GreenLight will be null if _keyDownStatus[90] is false
so put the GreenLight as a class variable
private var lights:Array = [];
private function keyCheck(event:Event)
{
if (_keyDownStatus[90])
{
var GreenLight:Projectile1 = new Projectile1();
stage.addChild(GreenLight);
GreenLight.x = Player2Child.x;
GreenLight.y = Player2Child.y;
for each (var l:Projectile1 in lights)
{
l.x -= defaultSpeed;
}
lights.push(GreenLight);
}
else
{
//remove all Projectile1s in lights from stage and clear lights if you want
}
}
you create GreenLight in if condition scope and if keyCode isnot 90, object of Projectile1 isnot created and GreenLight.x -= defaultSpeed; there GreenLight is null

Keep receiving error: access of undefined property menu in AS3

I am following a tutorial (http://flashdeveloptutorial.blogspot.com/2011/08/chapter-2.html) for AS3 using flash develop to create a pong clone and am having trouble with displaying a main menu. I previously had a functioning prototype with a moving paddle so I believe the error(s) is in my revised(or new) Main, MainMenu, PongGame or CustomEvents file(s). When I run the code as instructed through the tutorial I receive the error ; access of undefined property menu in my Main.as file:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author
*/
public class Main extends Sprite
{
private var game:PongGame;
****private var menu:MainMenu;****
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
private function buildMenu():void {
menu = new MainMenu();
addChild(menu);
menu.addEventListener(CustomEvents.LAUNCH_GAME, startGame, false, 0, true);
}
private function startGame(e:CustomEvents):void {
removeChild(menu);
menu.removeEventListener(CustomEvents.LAUNCH_GAME, startGame);
menu = null;
game = new PongGame();
addChild(game);
}
}
}
So I added the "private var menu:MainMenu;" section but I do not know what class/ .as file to reference? or how to define var menu:
Sorry for the large amount of attached code. I did not change paddle.as which is the longest file.
assets.as
package
{
public class Assets
{
[Embed (source = '../lib/paddle.png')] public static const Pad:Class;
public function Assets(): void {
}
}
}
Paddle.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Paddle extends Sprite {
private var pic:Bitmap = new Assets.Pad();
public function Paddle():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function die(e:Event):void {
removeChild(pic);
pic = null;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
removeEventListener(Event.ENTER_FRAME, enterFrame);
removeEventListener(Event.REMOVED_FROM_STAGE, die);
}
private function go(e:Event) : void {
removeEventListener(Event.ADDED_TO_STAGE, go);
addChild(pic);
y = stage.stageHeight - pic.height;
x = stage.stageWidth * .5 - pic.width * .5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
addEventListener(Event.REMOVED_FROM_STAGE, die);
}
private var movingLeft:Boolean;
private var movingRight:Boolean;
private function enterFrame(e:Event):void {
if (movingLeft) {
if(x-speed >= 0) {
x -= speed;
}
else {
x = 0;
}
}
else if (movingRight) {
if (x + speed + pic.width <= 600) {
x += speed;
}
else {
x = 600 - pic.width;
}
}
}
private var speed:int = 10;
private function keyDownHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (!movingLeft) {
movingLeft = true;
}
}
else if (e.keyCode == 40 || e.keyCode == 83) {
if (!movingRight) {
movingRight = true;
}
}
}
private function keyUpHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (movingLeft) {
movingLeft = false;
}
}
if (e.keyCode == 40 || e.keyCode == 83) {
if (movingRight) {
movingRight = false;
}
}
}
}
}
mainmenu.as
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class MainMenu extends Sprite
{
private var pongButton:Sprite;
public function MainMenu():void{
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
pongButton = item("Play", 20, 30, launchGame, 0xFF0000);
addChild(pongButton);
}
private function launchGame(e:MouseEvent):void {
dispatchEvent(new CustomEvents(CustomEvents.LAUNCH_GAME));
}
private function item(buttonText:String, X:int, Y:int, Funct:Function, txtColor:uint = 0xFFFFFF):Sprite {
var item:Sprite = new Sprite();
item.graphics.beginFill(0);
item.graphics.lineStyle(1, txtColor, .5);
item.graphics.drawRect(0, 0, 250, 30);
var myText:TextField = new TextField();
myText.selectable = false;
myText.width = 250;
myText.height = 30;
item.addChild(myText);
myText.autoSize = "center";
myText.text = buttonText;
myText.textColor = txtColor;
item.addEventListener(MouseEvent.CLICK, Funct);
item.x = X;
item.y = Y;
return item;
}
}
}
PongGame.as
package
{
import flash.display.Sprite;
import flash.events.Event;
public class PongGame extends Sprite
{
private var paddle:Paddle;
public function PongGame():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
paddle = new Paddle();
addChild(paddle);
}
}
}
CustomEvents.as
package
{
import flash.events.Event;
public class CustomEvents extends Event
{
public static const LAUNCH_GAME:String = "launch_game";
public function CustomEvents(e:String):void {
super(e);
}
}
}
I'm not sure where to go from here to fix this error. Any help would be appreciated.
Although you've correctly added the menu property, the menu is never displayed because buildMenu() function is never called.
From your main class init(), call buildMenu() to instantiate and add menu to the stage:
Main.as
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
buildMenu();
}

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()");
}
}
}

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

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).

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;
}
}
}
}