Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Trouble With Classes

    Hello! I'm currently working on a 2D Platform game, but I'm having some trouble with classes.

    The main character in the game I'm making has 4 classes- one for the up sprite, down sprite, left sprite, and right sprite. I'm using keyboard functions to make my character move, but I'm running into some trouble.

    For example, let's say I'm in the CharacterRight class. I also have a function in this class for if the keyboard.LEFT function, so that when the character goes left the sprites switch and it's not just the character's right sprite walking backwards.

    The problem is that I'm trying to remove the child of CharacterRight while adding CharacterLeft, but I can't because the file gives me an error when I try and give the variations instance names. I'm assuming that you can't give instance names to symbols within their own class, but I'm still lost.

    How can I make it so my character switches sprites if I can't remove/add children this way?

    Code:
    package  {
    	
    	import flash.display.MovieClip;
    	import flash.events.KeyboardEvent
    	import flash.ui.Keyboard
    	import flash.events.Event
    	import DragonLeft
    	
    	
    	
    	public class DragonRight extends MovieClip {
    		
    		var vx:int;
    		var vy:int;
    		
    		public var dragonLeft:DragonLeft= new DragonLeft;
    		public var dragonFront:DragonFront = new DragonFront;
    		public var dragonUp:DragonUp = new DragonUp;
    		public var dragonRight:DragonRight;
    //if I try and give an instance name to dragonRight I get a nasty error and 
    //the game won't open.
    		
    		public var rightHitDragon:RightHitDragon;
    		public var blockhitbox:Blockhitbox;
    		public var maze:Maze;
    		
    		
    		
    		public function DragonRight() 
    		{
    			
    			addEventListener(Event.ADDED_TO_STAGE, addedtostage);
    			stop();
    			
    		}
    		private function addedtostage(event:Event):void
    		{
    			init();
    		}
    		public function init():void
    		{
    			
    			vx = 0;
    			vy = 0;
    			
    			trace("Right");
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressDragonRight);
    			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyLiftDragonRight);
    			addEventListener(Event.ENTER_FRAME, onEnterDragonRight);
    		}
    		function MoveRight()
    			{
    				init();
    			}
    		
    		function onEnterDragonRight(event:Event):void
    			{
    				x += vx;
    			}
    		
    		function onKeyPressDragonRight(event:KeyboardEvent):void
    			{
    				trace(x)
    				trace(y)
    				play();
    			
    			if(event.keyCode == Keyboard.RIGHT)
    				{
    					vx = 5;
    				} 
    			else if (event.keyCode == Keyboard.LEFT)
    				{
    					
    					addChild(dragonLeft);
    				}
    			else if(rightHitDragon.hitTestObject(maze.blockHitBox))
    				{
    					x = 0;
    				}
    			}
    		function onKeyLiftDragonRight(event:KeyboardEvent):void
    			{
    			
    				stop();
    			
    				if(event.keyCode == Keyboard.RIGHT)
    					{
    						vx = 0
    					}
    			}
    		}
    		
    	}
    Sorry if this is just me making a stupid error... I'm pretty new to as3.

  • #2
    New Coder djpaul1963's Avatar
    Join Date
    Apr 2012
    Location
    Athens GR
    Posts
    75
    Thanks
    0
    Thanked 8 Times in 8 Posts
    A lot of things here. First you can't instantiate a class in that same class constructor because you are entering in an eternal loop.
    Second, it's not a good idea to add/remove stage objects from a class except the main game glass. You should add instances of all your movements in the main class for that purpose.
    Third, I would suggest you to have all the character's moves in the same movieclip and then gotoAndPlay the right portion of the animation according the keyboard press. It's much more efficient that way reducing the load of adding/removing display objects on each move and let the garbage collector stay in peace for a while
    Last edited by djpaul1963; 06-04-2013 at 12:39 AM.
    Everything can be done, but I don't know how.

  • Users who have thanked djpaul1963 for this post:

    HilariousBread (06-04-2013)

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the suggestion and the help, that should be a much better way to work it.


  •  

    Posting Permissions

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