dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] Accessing properties from another package.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7

    resolved [RESOLVED] Accessing properties from another package.

    I am working on setting up some simple AI actions for my enemy class in the game that I am working on, but I am having trouble accessing the player's X location within the enemy package to set up a detection function.

    I have defined the player on my Level package along with the enemy, and I defined the player's and enemies X and Y locations within the Player/Enemies package respectively.

    Within my flash file I have linked to my Level.as file. Also within my flash file library I have two movie clips. Both have a square in them that are the same size but colored differently. One is called PlayerSpriteMC and the other is called EnemiesSpriteMC.

    Any help would be greatly appreciate.

    Thanks in advance.

    Here is my source code:

    In my Level.as file

    Code:
    package Scripts
    
    {
    
    	import flash.display.Sprite;
    	
    	import flash.display.MovieClip;
    	
    	import flash.display.Shape;
    	
    	import flash.net.SharedObject;
    	
    	import Scripts.Player;
    	
    	import Scripts.Enemies;
    
    	public class Level extends Sprite
    	
    	{
    		
    		protected const GRAVITY: Number = 5;
    		
    		protected const GROUND_FLOOR: Number = 519;
    	
    		protected var player:Player;
    		
    		protected var enemy:Enemies;
    		
    		protected var globalConstants:SharedObject = SharedObject.getLocal("globals");
    		
    		public function Level():void
    		
    		{
    			globalConstants.data.GRAVITY = GRAVITY;
    			
    			globalConstants.data.GROUND_FLOOR = GROUND_FLOOR;			
    		
    			createLevel();
    			
    			player = new Player();
    			
    			addChild(player);
    			
    			enemy = new Enemies();
    			
    			addChild(enemy);			
    			
    		}
    		
    		private function createLevel():void
    		
    		{
    			//public function drawRect(x:Number, y:Number, width:Number, height:Number):void
    			
    			//Temp Floor
    			
    			graphics.beginFill(0x0a0a0a);
    			
    			graphics.drawRect(0, 520, 800, 80);
    			
    			graphics.endFill();
    			
    			//Temp Left Wall
    			
    			graphics.beginFill(0x0a0a0a);
    			
    			graphics.drawRect(0, 0, 15, 600);
    			
    			graphics.endFill();
    			
    			//Temp Right Wall
    			
    			graphics.beginFill(0x0a0a0a);
    			
    			graphics.drawRect(790, 0, 15, 600);
    			
    			graphics.endFill();
    			
    		}
    		
    		/*public function moveEnemies( event:TimerEvent ):void 
    		
    		{
    			
    			Enemies.enemiesMovement();
    		
    		}*/
    	
    	}
    	
    }
    In my Player.as file

    Code:
    package Scripts
    
    {
    
    	import flash.display.Sprite;
    	
    	import flash.events.Event;
    	
    	import flash.events.KeyboardEvent;
    	
    	import flash.net.SharedObject;
    
    	public class Player extends Sprite
    	
    	{
    		
    		protected var globalConstants:SharedObject = SharedObject.getLocal("globals");
    		
    		private var isLeftKeyDown:Boolean = false;
    		
    		private var isUpKeyDown:Boolean =false;
    		
    		private var isRightKeyDown:Boolean = false;
    		
    		private var isShiftKeyDown:Boolean = false;
    		
    		
    		
    		private var playerWalkingSpeed: Number = 10;
    		
    		private var playerRunningSpeed: Number = 15;
    		
    		private var playerJumpPower: Number = 0;
    		
    		private var isPlayerJumping: Boolean = false;
    		
    		private var isPlayerAirBorne: Boolean = false;
    		
    	
    		public function Player()
    		
    		{
    			
    			createPlayer();
    			
    			trace("A new character appears!");
    			
    			addEventListener(Event.ADDED_TO_STAGE, keyboardMovements);
    
    		}
    		
    		private function createPlayer():void
    		
    		{
    			
    			trace("Player begining created!");
    			
    			this.x = 50;
    			
    			this.y = 519;
    			
    			this.width = 60;
    			
    			this.height = 60;
    			
    		}
    
    		public function keyboardMovements(event:Event)
    
    		{
    		
    			removeEventListener(Event.ADDED_TO_STAGE, keyboardMovements);
    			
    			addEventListener(Event.ENTER_FRAME, playerMovement);
    			
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressDown);
    			
    			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyPressUp);
    		
    		}
    		
    		private function playerMovement(event:Event)
    		
    		{
    			
    			updatePlayerMovement();
    			
    		}
    		
    		private function onKeyPressDown(event:KeyboardEvent)
    		
    		{
    			switch (event.keyCode)
    			
    			{
    				
    				case 37:
    				
    				{
    					
    					isLeftKeyDown = true;
    					
    					break;
    					
    				}
    				
    				case 38:
    				
    				{
    					
    					if (!isPlayerAirBorne)
    					
    					{
    					
    						isPlayerJumping = true;
    					
    						isPlayerAirBorne = true;
    					
    						playerJumpPower = 40;
    						
    					}
    					
    					else
    					
    					{
    						
    						trace("Can not jump as player is already in the air!");
    						
    					}
    				
    					break;
    					
    				}
    				
    				case 39:
    				
    				{
    					
    					isRightKeyDown = true;
    				
    					break;
    					
    				}
    				
    				case 16:
    				
    				{
    					
    					isShiftKeyDown = true;
    					
    					trace("Shift Key is being pressed");
    					
    					break;
    					
    				}
    				
    			}
    		
    		}
    		
    		private function onKeyPressUp(event:KeyboardEvent)
    		
    		{
    			switch (event.keyCode)
    			
    			{
    				
    				case 37:
    				
    				{
    					
    					isLeftKeyDown = false;
    					
    					break;
    					
    				}
    				
    				case 38:
    				
    				{
    					
    					isUpKeyDown = false;
    				
    					break;
    					
    				}
    				
    				case 39:
    				
    				{
    					
    					isRightKeyDown = false;
    				
    					break;
    					
    				}
    				
    				case 16:
    				
    				{
    					
    					isShiftKeyDown = false;
    					
    					break;
    					
    				}
    				
    			}
    		
    		}
    		
    		private function updatePlayerMovement()
    		
    		{
    			
    			if(isLeftKeyDown)
    			
    			{
    				
    				this.x -= playerWalkingSpeed;
    				
    			}
    			
    			if(isLeftKeyDown && isShiftKeyDown)
    			
    			{
    				this.x -= playerRunningSpeed;
    				
    			}
    			
    			if(isPlayerJumping)
    			
    			{  
    				this.y -= playerJumpPower;  
    				
    				playerJumpPower -= 2;  
    				
    			}  
    			  
    			if(this.y + globalConstants.data.GRAVITY < globalConstants.data.GROUND_FLOOR)  
    				
    			{
    				
    				this.y += globalConstants.data.GRAVITY;  
    				
    			}
    			
    			else
    			
    			{
    				
    				this.y = globalConstants.data.GROUND_FLOOR;  
    				
    				isPlayerJumping = false;
    				
    				isPlayerAirBorne = false;
    				
    			}   
    			
    			if(isRightKeyDown)
    			
    			{
    				
    				this.x += playerWalkingSpeed;
    			
    			}   
    			
    			if(isRightKeyDown && isShiftKeyDown)
    			
    			{
    				
    				this.x += playerRunningSpeed;
    			
    			}
    			
    		}
    	
    	}
    	
    }
    and finally in my Enemies.as file

    Code:
    package Scripts
    
    {
    	import flash.display.Sprite;
    	
    	import flash.display.MovieClip;
    	
    	import flash.events.Event;
    	
    	import flash.net.SharedObject;
    	
    	import flash.utils.Timer;
    	
    	import flash.events.TimerEvent;
    	
    	public class Enemies extends Sprite
    		
    	{
    		
    		private var enemyWalkingSpeed: Number = 5;
    		
    		private var enemyAlertedSpeed: Number = 13;
    		
    		private var enemyPatrolPathStart: Number = 750;
    		
    		private var enemyPatrolPathEnd: Number = 500;
    		
    		private var enemyPatrollingLeft: Boolean = true;
    		
    		private var enemyPatrollingRight: Boolean = false;
    		
    		private var enemyAlertStatus: Boolean = false;
    		
    		private var enemyDetectionRange: Number = 5;
    		
    		protected var gameTimer:Timer;
    		
    		public function Enemies():void
    		
    		{
    			
    			createEnemies();
    			
    			trace("A new Enemy appears!");
    			
    			gameTimer = new Timer( 25 );
    			
    			gameTimer.addEventListener( TimerEvent.TIMER, moveEnemies);
    			
    			gameTimer.start();
    			
    			addEventListener(Event.ADDED_TO_STAGE, enemyMovements);
    
    		}
    		
    		private function createEnemies():void
    		
    		{
    			
    			trace("Player begining created!");
    			
    			this.x = 750;
    			
    			this.y = 519;
    			
    			this.width = 60;
    			
    			this.height = 60;
    			
    		}
    		
    		public function moveEnemies( event:TimerEvent ):void 
    		
    		{
    			
    			enemiesMovement();
    			
    			detectPlayer();
    		
    		}
    		
    		public function detectPlayer():void
    		
    		{
    			// This is where I am having trouble
    			if (Scripts.Player.this.x <= enemyDetectionRange)
    			
    			{
    				
    				trace("Player is detected");
    				
    			}
    			
    		}
    
    		public function enemyMovements(event:Event):void
    
    		{
    		
    			removeEventListener(Event.ADDED_TO_STAGE, enemyMovements);
    		
    		}
    		
    		public function enemiesMovement():void 
    		
    		{
    			if(enemyPatrollingLeft && !enemyAlertStatus)
    			
    			{
    			
    				this.x -= (enemyWalkingSpeed/2);
    				
    				if (this.x == enemyPatrolPathEnd)
    				
    				{
    					
    					enemyPatrollingLeft = false;
    					
    					enemyPatrollingRight = true;
    					
    				}
    			
    			}
    			
    			else if (enemyPatrollingRight && !enemyAlertStatus)
    			
    			{
    			
    				this.x += (enemyWalkingSpeed/2);
    				
    				if (this.x == enemyPatrolPathStart)
    				
    				{
    					
    					enemyPatrollingLeft = true;
    					
    					enemyPatrollingRight = false;
    					
    				}
    			
    			}
    		
    		}
    				
    	}
    	
    }

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    I resolved my issue. The solution was twofold. First I restructured my code similar to the tutorial provided by Michaels Williams at his website:http://gamedev.michaeljameswilliams....me-tutorial-1/

    The second solution was I was calling the class name rather than the instance name.

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