All late comers this question in still active a answer is not yet reached, what you might see below is a irrelevent syntax error a nice member found for me
error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Maintest_fla::MainTimeline/createPlayer()
When i'm attempted to add the instance name wall0x objects that are in the object with the instance name world, I find that I get a null object error.
Also ignore the long list of variables, not relevant.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.utils.Timer;
public class Player extends MovieClip
{
// player settings
private var _rotateSpeedMax:Number = 20;
public var _gravity:Number = .10;
// projectile gun settings
public var _bulletSpeed:Number = 4;
public var _maxDistance:Number = 200;
public var _reloadSpeed:Number = 250;//milliseconds
public var _barrelLength:Number = 20;
public var _bulletSpread:Number = 5;
// gun stuff
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = false;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
// array that holds walls
public var _solidObjects:Array = [];
//
private var _player:MovieClip;
private var _dx:Number;
private var _dy:Number;
private var _pcos:Number;
private var _psin:Number;
public var _trueRotation:Number;
public function Player()
{
// constructor code //Right hereVVVthe instance name is wall0x and it's in the object world on the stage.
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
/*addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);*/
}
}
}
Code I'm using in frame 2 create the player and then continuously set it's chords to another objects.
stage.addEventListener(Event.ENTER_FRAME, createPlayer);
function createPlayer(e:Event):void
{
// attach player movieclip from library
// position player in center
if (character!=null&&_player!=null)
{
_player.x = character.x + 5;
_player.y = character.y + 5;
}
else if (_player ==null && world.wall01 != null)
{
var _player:Player;
_player = new Player();
// add to display list
stage.addChild(_player);
}
}
First: You have a syntax error in these two lines:
_player.x = MovieClip.(root).character.x + 5;
_player.y = MovieClip.(root).character.y + 5;
There should not be a period after MovieClip, so it should look like this:
_player.x = MovieClip(root).character.x + 5;
_player.y = MovieClip(root).character.y + 5;
Second: You are always creating a new Player every frame. In your createPlayer method, you have the following conditional:
if(character != null && _player != null) //_player is not a defined in this scope, so it will either throw an error, or always return null/undefined
You do not have a _player var defined in the scope of that frame or the scope of the createPlayer method, you have defined it inside the scope of the else statement (which makes only available in the else statement)
Move the var _player:Player to the top of your timeline code with the other frame scoped vars.
Third: you are trying to access root in your Player constructor, the problem with this, is that when the contructor runs, your Player is not yet on the display tree, so root is null until you've added player to the stage.
example:
_player = new Player(); //this will run your contructor, but root will be null
stage.addChild(_player); //after this, your Player class will now have access to root/stage/parent object
Change your Player class so it listens for being ADDED_TO_STAGE before trying access root.
public function Player()
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
// constructor code
}
private function init(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
_solidObjects = [MovieClip(root).world.wall01,MovieClip(root).world.wall02,MovieClip(root).world.wall03,MovieClip(root).world.wall04];
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}
Related
Beginning issue: I am continuously getting an error 1136 at var timer:Timer=new Timer(10000,1);. I am not sure if its a computer error or what. I know you put (delay, and then time interval) for Timer, but it still gives me the error message.
update: I uploaded the whole code from the original post to see if anyone could find any errors. My Timers should be correct but I am still getting the error 1136 on my program and it won't even run. When I do take out the timers, the code works perfectly fine.
Is there any other way to. implement a timer to make the game stop and say game over?
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.text.*;
public class Duckhunt extends MovieClip
{
private var player1:Player;
private var counter:Number;
private var points:Number;
private var cursor:Cursor;
private var duckArmy:Vector.<Duck1>;
private var duckArmy2:Vector.<Duck2>;
private var duckCounter:Number;
private var duckCounter2:Number;
private var count:Number=10;
private var timer:Timer;
//var countDownDec:Number=1;
//private var myTimer:Timer=new Timer(0,count);
public function Duckhunt()
{
//# constructor code
// creates a new five-second Timer
var timer:Timer=new Timer(10000,1);
//add event listner to timer
timer.start();
//starts the timer
//myTimer.start();
player1=new Player();
player1.x = 375; player1.y = 400; addChild(player1);
//cursor crosshair
cursor= new Cursor();
cursor.x = 400; cursor.y = 200; addChild(cursor);
//add enemy
duckArmy=new Vector.<Duck1>();
for(var i:Number = 0; i < 30; i++)
{
var duck:Duck1 = new Duck1(400,0);
duckArmy.push(duck);
duck.y = 450;
stage.addChild(duck);
duck.gotoAndPlay("fly");
duck.addEventListener(MouseEvent.CLICK, hitEnemy);
}//end duck1 army
//add second second enemy
duckArmy2=new Vector.<Duck2>();
for(var j:Number=0;j<30;j++)
{
var duck2:Duck2 = new Duck2(0,400);
duckArmy2.push(duck2);
duck2.x = -900;
stage.addChild(duck2);
duck2.gotoAndPlay("fly");
duck2.addEventListener(MouseEvent.CLICK, hitEnemy);
}//end duck2 army
duckCounter = duckCounter2 =counter = points = 0;
addEventListener(Event.ENTER_FRAME, frameMovement);
}//end constructor
}//end class
}//end package
If you've declared your :Timer object as a public or private variable:
private var timer:Timer;
There's no need for declaring yet another new variable (with same name) in later functions;
var timer:Timer=new Timer(10000,1); //creates a new 2nd var.. causes error..
timer.start();
Shoud be:
timer = new Timer (10000,1); //uses existing private var
timer.start();
Also note:
That timer variable name is too similar to the Timer datatype name, consider changing the name's spelling to avoid "clashing" with built-in system names. Never do var int :int = 0;.
Try naming as _timer :Timer; or even myTimer :Timer;
This is in the main class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class main extends MovieClip {
public var scene = 0;
public var _money = 0;
public var gain = 1;
public var clicks = 0;
public function main() {
addEventListener(Event.ENTER_FRAME, loop);
mainbtn.addEventListener(MouseEvent.CLICK, handler);
playbtn.addEventListener(MouseEvent.CLICK, playHandler);
}
var mainbtn:button = new button();
var playbtn:playbutton = new playbutton();
var playtxt:playtext = new playtext();
var cash:money_txt = new money_txt();
var scene0:MovieClip = new MovieClip();
var scene1:MovieClip = new MovieClip();
public function loop(e:Event):void {
if(scene == 0) {
addChild(scene0)
scene0.addChild(playbtn);
playbtn.x = 300;
playbtn.y = 200;
scene0.addChild(playtxt);
playtxt.x = 300;
playtxt.y = 100;
} else {
scene0.removeChild(playbtn);
scene0.removeChild(playtxt);
}
if(scene == 1) {
addChild(scene1);
scene1.addChild(mainbtn);
mainbtn.x = 300;
mainbtn.y = 200;
scene1.addChild(cash);
cash.text = 'Money: ' + _money.toString();
} else {
scene1.removeChild(mainbtn);
}
}
public function playclickHandler(e:MovieClip) {
scene = 1;
}
public function handler(e:MouseEvent):void {
_money += gain;
clicks++;
trace('yep');
}
public function playHandler(e:MouseEvent):void {
scene = 1;
}
}
}
And This is where the error would be
C:\Users\Slime\Desktop\Art-ish\game\main.as, Line 47, Column 10 1119: Access of possibly undefined property text through a reference with static type money_txt.
Thanks for helping if you can!
these should be defined as public
public var mainbtn:button = new button();
public var playbtn:playbutton = new playbutton();
public var playtxt:playtext = new playtext();
public var cash:money_txt = new money_txt();
public var scene0:MovieClip = new MovieClip();
public var scene1:MovieClip = new MovieClip();
also it is hard to tell if money_txt, playtext, playbutton and button are classes or MovieClip instances. Convention dictates that Classes should start with a capital letter and instances with lower.
update
The issue is that if button and playbutton are buttons and playtext and money_txt are MovieClips, you should instantiate them as such.
for example if you have
public var mainbtn:button = new button();
but there is no class with name of button, mainbtn will be null. What you may need to do is
public var mainbtn:Button;
public var cash:MovieClip;
and as a part of your main or some other function, assign the instances
mainbtn = this['button'];
cash = this['money_txt'];
you can check if this worked by checking trace(cash);, which will return null if the assignment did not work.
I should stress again though, it is hard to to know what exactly is going wrong without knowing what your setup is. I'm assuming money_txt and the other classes you are defining are not actually classes with their own linkage IDs, but buttons and movieclips inside the MovieClip or stage you are putting this code in.
C:\Users\Lab3project\MainDocument.as, Line 103, Column 7 1013: The
private attribute may be used only on class property definitions.
The lines of code concerned:
package {
//these are flash built-in classes
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.Event;
//Our own custom class
import MainTimer;
public class MainDocument extends MovieClip{
private var gameTimer:MainTimer;
private var thePlayer:Player;
private var theEnemy:Enemy;
private var maxEnemies: int = 3;
private var e:int = 0;
private var childrenOnStage:int;
public function MainDocument() {
// constructor code
trace("the main document is alive");
// new instance MainTimer class
gameTimer = new MainTimer();
// must add it to the stage
addChild(gameTimer);
//adjust its postion on stage
gameTimer.x = 20;
gameTimer.y = 20;
//add the player
thePlayer = new Player();
addChild(thePlayer);
// adjust its postion on the stage
thePlayer.x = stage.stageWidth * 0.5;
//assign the name property
thePlayer.name = "player";
while(e < maxEnemies){
createEnemy();
e++;
} //end while
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
// Add event listener to control timing of main game loop
addEventListener(Event.ENTER_FRAME,mainGameLoop);
}//end function MainDocument
private function createEnemy():void{
trace("create enemy");
theEnemy = new Enemy();
addChild(theEnemy);
//Place in a random spot on stage
theEnemy.x = (Match.random() * stage.stageWidth);
theEnemy.y = 0;
//assign the name property
theEnemy.name = "enemy";
//Update this variable every time a child is added to the stage
childrenOnStage = this.numChildren
} //end function createEnemy
//the main loop for the game
private function mainGameLoop(event:Event): void{
checkForGameReset();
processCollisions();
scrollStage();
} // end functiom mainGameLoop
private function checkForGameReset():void{
//define conditions
} //end function checkForGameReset
private function processCollisions():void{
//set up the main loop to look through all collidale objects on stage
for(var c:int;c < childOnStage;c++){
//trace ("Child on stage c= " + c +
//test for a player of enemy child on stage
if (getChildAt(c).name == "player" || getChildAt(c).name == "enemy"){
//see if object is touching the game stage
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)){
//while it is still touching the game stage
while( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==true){
//called from CollisionObject class,so force the connection
CollisionObject(getChildAt(c)).incrementUpward();
if( theGameStage.hitTestPoint(getChildAt(c).x, getChildAt(c).y,true)==false){
}CollisionObject(getChildAt(c)).keepOnBoundary(); //make it stick
} // end if
} // end while
} //end if touching
} //end if player or enemy
} //end for loop
} //end function processCollisions
The lines of code concerned: is here where im getting the error
private function scrollStage(): void
{
// figure out logic
}
// end function scrollStage
//add the enemy for testing
theEnemy = new Enemy();
addChild(theEnemy);
// adjust its postion on the stage
theEnemy.x = stage.stageWidth * 0.5;
theEnemy.y = 200;
} // end public function MainDocument
} // end public class
} // end package
After I have change that code ,it then says I have a extra character at the end but the characters I have to have at the end.
Problems like this often occur because of a slight typo in syntax. Perhaps you have accidentally declared your private function within another function?
public function doSomething() {
//a whole bunch of stuff
//then we forget to close the function with a brace "}"
private function scrollStage() {
//but we're still defining doSomething! Throw error!
}
Go and look for errors like this, and hopefully you can find the problem.
I am making a game in ActionScript 3. I have a Menu Class with a method that renders a Menu.
I make an instance of Menu in my Main class, and then call the method. When I debug the application I get a null reference error. This is the code of the menu class:
package
{
import flash.display.MovieClip;
import menucomponents.*;
public class Menu extends MovieClip
{
public function Menu()
{
super();
}
public function initMenuComponents():void{
var arrMenuButtons:Array = new Array();
var btnPlay:MovieClip = new Play();
var btnOptions:MovieClip = new Options();
var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook();
var btnShareOnFacebook:MovieClip = new ShareOnFacebook()
arrMenuButtons.push(btnPlay);
arrMenuButtons.push(btnOptions);
arrMenuButtons.push(btnLikeOnFacebbook);
arrMenuButtons.push(btnShareOnFacebook);
var i:int = 0;
for each(var item in arrMenuButtons){
item.x = (stage.stageWidth / 2) - (item.width / 2);
item.y = 100 + i*50;
item.buttonMode = true;
i++;
}
}
}
}
Thanks in advance.
Your issue is likely that stage is not populated yet when your for loop runs. Try the following:
public class Main extends MovieClip {
public function Main() {
super();
var menu:Menu = new Menu();
//it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have)
if(stage){
addedToStage(null);
}else{
//stage isn't ready, lets wait until it is
this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
}
}
private function addedToStage(e:Event):void {
menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called.
stage.addChild(menu);
}
private function menuAdded(e:Event):void {
menu.initMenuComponents();
}
}
I am making a website in Flash, coded in flashbuilder. Whenever I try to export my code I get the same error again and again (TypeError = see below).
I think the problem has something to do with the stage of my project. Whenever I change the var stageMiddenX = (stage.stageWidth / 2); into var stageMiddenX = 512;, the code works. but I wan't the var to be dynamic.
TypeError
Error #1009: cannot access a property or method of a null object reference at main()
package {
import flash.display.MovieClip;
public class main extends MovieClip{
var stageMiddenX = (stage.stageWidth / 2);
var stageMiddenY = (stage.stageHeight / 2);
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
}
}
The issue here is that stage is not yet available at the time you are requesting it.
You'll want to wait until the Event.ADDED_TO_STAGE event is fired before attempting to acccess stage.
package {
import flash.display.MovieClip;
public class main extends MovieClip{
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
private var stageMiddenX:Number;
private var stageMiddenY:Number;
public function main(){
if(stage) init(null);
else addEventListener(Event.ADDED_TO_STAGE, init)
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stageMiddenX = (stage.stageWidth / 2);
stageMiddenY = (stage.stageHeight / 2);
}
}
}
Put the stuff accessing stage into a constructor (assuming this is your document class)..
package
{
import flash.display.MovieClip;
public class main extends MovieClip
{
public var stageMiddenX:int;
public var stageMiddenY:int;
private var object1:Object1 = new Object1();
private var object2:Object2 = new Object2();
private var object3:Object3 = new Object3();
public function main()
{
stageMiddenX = stage.stageWidth / 2;
stageMiddenY = stage.stageHeight / 2;
}
}
}