dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: CS4 Non-document class can't access library

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2

    CS4 Non-document class can't access library

    Got two classes "Engine.as" and "Player.as". Engine.as is the document class that is calling Player.as.

    The point is to have Player.as create a movieclip on the stage, but it seems it cannot access the library or something. Because when the player class is at the createHero() method it gives me the
    "error 1009 cannot access a property or method of a null object reference"

    Engine class:
    Code:
    package test{
    	import flash.display.*;
    	import flash.events.*;
    	
    	public class Engine extends MovieClip{
    		
    		
    		
    		public function Engine():void{
    			var player:Player = new Player();
    			
    		}
    		
    	}
    }
    Player class:
    Code:
    package test{
    	import flash.events.*;
    	import flash.display.*;
    	
    	public class Player extends MovieClip{
    		var hero:Hero;
    		
    		var velocity:int=10
    		var keyW:Boolean=false;
    		var keyA:Boolean=false;
    		var keyS:Boolean=false;
    		var keyD:Boolean=false;
    		
    		public function Player() {
    			
    			createHero();
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
    			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
    			stage.addEventListener(Event.ENTER_FRAME, keyPressedFunction);
    			stage.addEventListener(Event.ENTER_FRAME, pointAtCursor);
    		}
    
    		public function createHero() {
    			
    			hero = new Hero();
    			hero.x=425;
    			hero.y=325;
    			addChild(hero);
    		}
    		
    		function keyDownFunction(event:KeyboardEvent):void {
    			if(event.keyCode == 87){
    			keyW=true;
    			}else if(event.keyCode == 83){
    			keyS=true;
    			}else if(event.keyCode == 65){
    			keyA=true;
    			}else if(event.keyCode == 68){
    			keyD=true;
    			}
    		}
    		
    		function keyUpFunction(event:KeyboardEvent):void {
    			if(event.keyCode == 87){
    			keyW=false;
    			}else if(event.keyCode == 83){
    			keyS=false;
    			}else if(event.keyCode == 65){
    			keyA=false;
    			}else if(event.keyCode == 68){
    			keyD=false;
    			}
    		}
    		
    		function keyPressedFunction(e:Event):void{
    			if(keyW){
    			hero.y-=velocity;
    			}
    			if(keyS){
    			hero.y+=velocity;
    			}
    			if(keyA){
    			hero.x-=velocity;
    			}
    			if(keyD){
    			hero.x+=velocity;
    			}
    		}
    		
    		function pointAtCursor(event:Event){
    			var dx:Number = mouseX - this.x;
    			var dy:Number = mouseY - this.y;
    			
    			var cursorAngle:Number = hero.atan2(dy,dx);
    			var cursorDegrees:Number =     360*(cursorAngle/(2*Math.PI));
    			hero.rotation = cursorDegrees;
    		}
    	
    	}
    }
    Help would be appreciated.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    package test{
    	import flash.display.*;
    	import flash.events.*;
    
    	public class Engine extends MovieClip {
    
    		public function Engine():void {
    
    			var player:Player = new Player();
    			addChild(player);
    
    		}
    	}
    }
    Code:
    package test{
    	import flash.events.*;
    	import flash.display.*;
    
    	public class Player extends MovieClip {
    		var hero:Hero;
    		var velocity:int=10;
    		var keyW:Boolean=false;
    		var keyA:Boolean=false;
    		var keyS:Boolean=false;
    		var keyD:Boolean=false;
    
    		public function Player() {
    			createHero();
    			addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
    		}
    		private function stageAddHandler(e:Event):void {
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
    			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
    			stage.addEventListener(Event.ENTER_FRAME, keyPressedFunction);
    			stage.addEventListener(Event.ENTER_FRAME, pointAtCursor);
    			removeEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
    		}
    		private function createHero() {
    			hero = new Hero();
    			hero.x=125;
    			hero.y=125;
    			addChild(hero);
    		}
    
    		private function keyDownFunction(event:KeyboardEvent):void {
    			if (event.keyCode==87) {
    				keyW=true;
    			} else if (event.keyCode == 83) {
    				keyS=true;
    			} else if (event.keyCode == 65) {
    				keyA=true;
    			} else if (event.keyCode == 68) {
    				keyD=true;
    			}
    		}
    
    		private function keyUpFunction(event:KeyboardEvent):void {
    			if (event.keyCode==87) {
    				keyW=false;
    			} else if (event.keyCode == 83) {
    				keyS=false;
    			} else if (event.keyCode == 65) {
    				keyA=false;
    			} else if (event.keyCode == 68) {
    				keyD=false;
    			}
    		}
    
    		private function keyPressedFunction(e:Event):void {
    			if (keyW) {
    				hero.y-=velocity;
    			}
    			if (keyS) {
    				hero.y+=velocity;
    			}
    			if (keyA) {
    				hero.x-=velocity;
    			}
    			if (keyD) {
    				hero.x+=velocity;
    			}
    		}
    
    		private function pointAtCursor(event:Event) {
    			var dx:Number=mouseX-this.x;
    			var dy:Number=mouseY-this.y;
    			var cursorAngle:Number=Math.atan2(dy,dx);
    			var cursorDegrees:Number = 360*(cursorAngle/(2*Math.PI));
    			hero.rotation=cursorDegrees;
    		}
    	}
    }

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Works!

    Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center