A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37

Thread: critique my class please

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Actually, you shouldn't need a Building_1 class as an as file. If you set Building as the base class and Building_1 as the class, flash will generate the Building_1 class for you. Since all the behavior is defined in Building, this works out fine.

  2. #22
    Member
    Join Date
    May 2009
    Posts
    37
    Okay, so all I need to do is put Building_1 as the class name and simply Building as the base class. Gotcha...this is the building class...is it alright so far? How would I attach a building to the planet movie clip?

    Code:
    package{
    	import flash.display.MovieClip;
    	
    	public class Building extends MovieClip {
    		private var _buildingMaxHealth:Number = 100;
    		private var _buildingMinHealth:Number = 0;
    		private var _buildingCurrHealth:Number = 100;
    		private var _i****:Boolean;
    		private var _isBeingRepaired:Boolean;
    		private var _isDestroyed:Boolean;
    		
    		public function Building() {
    			init();
    		}
    		
    		private function init():void {
    			this._i**** = false;
    			this._isBeingRepaired = false;
    			this._isDestroyed = false;
    		}
    				
    		private function init(){
    			var setX:Number = planet.x;//setting building at center of planet mc x position
    			var setY:Number = planet.y - (planet.height / 2);//setting building at top of planet mc y position
    			//this.x = setX;//setting building at center of planet mc x position
    			//this.y = setY;//setting building at top of planet mc y position
    			
    			repairHealth();//call to the repair function
    			subtractHealth();//call to the subtractHealth function
    		}
    		
    		private function repairHealth(){//function for repairing building during the Repair phase
    			//if building isn't destroyed and its health is not maxed out
    			if (this.isDestroyed != true && this.buildingCurrHealth < this.buildingMaxHealth){
    				this.buildingCurrHealth += 3;//add 3 to its current health
    			}
    		}
    		
    		private function subtractHealth(){
    			if (this.i**** = true && this.buildingCurrHealth > this.buildingMinHealth){
    				for (var i:uint; i < meteorArray.length; i++){
    					if (meteorArray[i].diameter < 76 && meteorArray[i].diameter > 54){
    						this.buildingCurrHealth -= 30;
    					}
    					if (meteorArray[i].diameter < 55 && meteorArray[i].diameter > 33){
    						this.buildingCurrHealth -= 20;
    					}
    					if (meteorArray[i].diameter < 34 && meteorArray[i].diameter > 22){
    						this.buildingCurrHealth -= 10;
    					}
    				}
    			}
    						
    		public function get buildingMaxHealth():Number {
    			return _buildingHealth;//gets the _buildingMaxHealth and makes it a number  
    		}
    		
    		public function set buildingMaxHealth(buildingMaxHealth:Number):void {
    			_buildingMaxHealth = buildingHealth;//converts the _buildingMaxHealth property to a variable 
    		}
    		
    		public function get buildingMinHealth():Number {
    			return _buildingMinHealth;//gets the _buildingMinHealth and makes it a number  
    		}
    		
    		public function set buildingMinHealth(buildingMinHealth:Number):void {
    			_buildingMinHealth = buildingMinHealth;//converts the _buildingMinHealth property to a variable 
    		}
    		
    		public function get buildingCurrHealth():Number {
    			return _buildingHealth;//gets the _buildingCurrHealth and makes it a number  
    		}
    		
    		public function set buildingCurrHealth(buildingCurrHealth:Number):void {
    			_buildingCurrHealth = buildingCurrHealth;//converts the _buildingCurrHealth property to a variable 
    		}
    	}
    }
    Last edited by krimzondestiny; 05-17-2009 at 05:10 PM. Reason: adding in 'Array' to the meteorArray name.

  3. #23
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Just the stuff I noticed as wrong. There's plenty right too.

    I don't know what i**** is, but I hope it was the board censoring you. You obviously can't have a variable with * in it.

    You can't refer to planet like that in Building because planet is not defined in Building. In fact, you shouldn't anyway. Instead, create the building and place it from the code which calls new Building().

    Be careful with the paramter naming in the getters and setters. I don't think you can use identical identifiers for the getter/setter and the parameter.

  4. #24
    Member
    Join Date
    May 2009
    Posts
    37
    alright...now i am trying to get something together so that i can playtest/debug my code so far...what all do i need to do in order to play the game with what i have so far? I keep getting a "1046: Type was not found or was not a compile-time constant: MouseEvent" error in the Planet.as class.


    Document Class
    Code:
    //===========================================================
    //PROTOTYPE
    //===========================================================
    
    package{
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	
    	public class nHarden_project_3 extends MovieClip {
    		//background variables
    		private var bg:Bg;
    		private var bgPosX:Number = 250;
    		private var bgPosY:Number = 200;
    		private var gameStateCount:Number;
    		
    		private var meteorArray:Array;
    			
    		public function nHarden_project_3(){
    			init();
    		}
    	
    		private function init():void {
    			//adding in the background
    			bg = new Bg();
    			bg.x = bgPosX;
    			bg.y = bgPosY;
    			addChild(bg);
    												
    			gameScreen();
    		}	
    			
    		//-------------------------------------------------------------------------
    		// GAME SCREEN CODE
    		//-------------------------------------------------------------------------
    		private function gameScreen(){
    			var planet:Planet;
    			addChild(planet);
    			
    			createMeteor()
    		}
    
    		// ----------create meteor code
    		private function createMeteor(){
    		
    			meteor = new Meteor();
    			meteor.x = Math.random() * stage.stageWidth - stage.x;
    			meteor.y = Math.random() * stage.stageHeight - stage.y;
    							
    			addChild(meteor);
    			meteorArray.push(meteor);
    			
    			var angleRad:Number;
    			angleRad = Math.atan2(earth.y-meteor.y, earth.x-meteor.x);
    			meteor.addEventListener(Event.ENTER_FRAME, moveMeteor);
    		}
    		
    		private function moveMeteor(e:Event):void {
    			if (meteorArray[i].hitTestObject(planet.atmosphere)){
    				meteorArray[i].meteorSpeed += 1;
    				trace ("entered atmosphere");
    			}
    				
    			meteorArray[i].x += Math.cos(angleRad) * meteorSpeed;
    			meteorArray[i].y += Math.sin(angleRad) * meteorSpeed;
    		}  
    	
    	}
    }
    PLANET class

    Code:
    //-------newPlanet CLASS
    //instantiate  the newPlanet movie clip and attach instances of the Buildings class, 
    //and put in the rotation functions.
    
    package {
    	import flash.display.Sprite;
    	import flash.events.Event;
    
    	public class Planet extends Sprite {
    		public function Planet() {
    			init();
    		}
    		
    		public function init():void {
    			x=250;//places the planet's x postion at the center of the stage
    			y=200;//places the planet's y postion at the center of the stage
    		}
    		
    		addEventListener(MouseEvent.MOUSE_DOWN,onDown);//adds an event listener to planet for clicking on it
    		addEventListener(MouseEvent.MOUSE_UP,onUp);//adds an event listener for releaseing the planet
    			
    		public function onDown(e:MouseEvent):void {
    			addEventListener(Event.ENTER_FRAME,onLoop);//adds event listener for moving the planet
    		}
    
    		public function onUp(e:MouseEvent):void {//IT IS THIS LINE
    			removeEventListener(Event.ENTER_FRAME,onLoop);//removes event listener for moving the planet
    		}
    
    		public function onLoop(e:Event):void {//AND ALSO THIS LINE
    			var distanceX=root.mouseX - x;//subtracts the distance of the x point of the planet from that of the mouse
    			var distanceY=root.mouseY - y;//subtracts the distance of the y point of the planet from that of the mouse
    			var angle=Math.atan2(dy,dx) * 180 / Math.PI;//get the angle of the two points and divides is by the conversion of rads to degrees
    			rotation=angle;//rotates the planet accordingly
    		}
    	}
    }
    Attached Files Attached Files
    Last edited by krimzondestiny; 05-18-2009 at 09:25 PM.

  5. #25
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't have flash installed, so I couldn't open your fla, but going through the code, I notice a few things.

    The main .as file has no game logic in it. You'd probably want to put the main game loop in there.

    Meteor.as: I'm not sure, but I think you'll have to use a parameter different from the function name in set meteorSpeed

    Planet.as: The initial positioning should not be done in Planet. It should be in whatever calls new Planet()

    Healthbar.as: appears to be entirely commented out, and doesn't define a class. I'm assuming it is unused.

    Building.as: meteorArray is undefined. You'll have to pass it in if you want to keep the checkIfHit method in Building. j doesn't need to be uint, but it's okay. int would probably be slightly more efficient. In subtractHealth you have an assignment in your conditional. this.i**** = true should be this.i**** == true. i doesn't need to be uint. You might want to move the damage value calculation into Meteor, but it's okay here. Again, make your parameters have different names than your functions.

    Unless your game logic is in the fla, there's no game here. And if it is in the fla, you might want to consider moving that code into the main .as file.


    Edit: is_Hit == i****. Hahaha!

  6. #26
    Member
    Join Date
    May 2009
    Posts
    37
    Okay, this is funky...it keeps outputting "at Meteor/get angleRad" and then "Error: Error #1023: Stack overflow occurred."

    Meteor
    Code:
    package {
    	import flash.display.MovieClip;//makes it possible to treat meteor as a movieclip
    
    	public class Meteor extends MovieClip {//defines the main meteor class
    		private var _meteorSpeed:Number = 1;//sets the constant property of speed for all meteors
    		private var diameter:Number;
    		private var _angleRad:Number; 
    		public function Meteor() {//declares the Meteor() function
    			init();//calls the init() function
    		}
    		
    		private function init():void {//declares the init() funtion for the meteor
    			diameter = Math.random() * 35 - 1 //makes the size of a meteor random
    			this.width = diameter;//sets the meteor's width to the predefined size
    			this.height = diameter;//sets the meteor's height to the predefined size
    		}
    			
    		public function get meteorSpeed():Number {
    			return _meteorSpeed;//gets the meteor speed and makes it a number  
    		}
    		
    		public function set meteorSpeed(meteorSpeed:Number):void {
    			_meteorSpeed = meteorSpeed;//converts the _meteorSpeed property to a variable 
    		}
    		
    		public function get angleRad():Number {
    			_angleRad = angleRad;
    		}
    		
    		public function set angleRad(angleRad:Number){
    			return _angleRad;
    		}
    	}
    }
    Code:
    package{
    
    	import flash.display.*;
    	import flash.events.*;
    	
    	public class nHarden_project_3 extends MovieClip {
    		//background variables
    		private var bg:Bg;
    		private var bgPosX:Number = 250;
    		private var bgPosY:Number = 200;
    		private var gameStateCount:Number;
    		private var planet:Planet = new Planet();
    		private var meteor:Meteor;
    		
    		private var meteorArray:Array = new Array();
    			
    		public function nHarden_project_3(){
    			init();
    		}
    	
    		private function init():void {
    			//adding in the background
    			bg = new Bg();
    			bg.x = bgPosX;
    			bg.y = bgPosY;
    			addChild(bg);
    						
    			gameScreen();
    		}	
    			
    		//-------------------------------------------------------------------------
    		// GAME SCREEN CODE
    		//-------------------------------------------------------------------------
    		private function gameScreen(){
    			planet.x = 250;
    			planet.y = 200;
    			addChild(planet);
    			
    			createMeteor()
    		}
    
    		// ----------create meteor code
    		private function createMeteor(){
    			meteor = new Meteor();
    			meteor.x = Math.random() * stage.stageWidth - stage.x;
    			meteor.y = Math.random() * stage.stageHeight - stage.y;
    							
    			addChild(meteor);
    			meteorArray.push(meteor);
    			
    			meteor.angleRad = Math.atan2(planet.y-meteor.y, planet.x-meteor.x);			
    			meteor.addEventListener(Event.ENTER_FRAME, moveMeteor);
    		}
    		
    		private function moveMeteor(e:Event):void {
    			for (var i:uint = 0; i < meteorArray.length; i++){
    				meteorArray[i].x += Math.cos(meteorArray[i].angleRad) * meteorSpeed;
    				meteorArray[i].y += Math.sin(meteorArray[i].angleRad) * meteorSpeed;
    			}
    		}  
    	}
    }

  7. #27
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You've got get and set backwards, and you're still using the same identifier for parameters as function names.
    Code:
    		public function get angleRad():Number {
    			return _angleRad;
    		}
    		
    		public function set angleRad(aRad:Number):void{
    			_angleRad = aRad;
    		}
    The reason it went into an infinite recursion was that
    Code:
    _angleRad = angleRad
    is equivalent to
    Code:
    _angleRad = this.angleRad;
    Which of course calls the getter from within the getter, with no termination condition.

  8. #28
    Member
    Join Date
    May 2009
    Posts
    37
    lol...right, i've been looking at this all day and everything is starting to run together...another q...how would i pass meteorArray from my document class to the meteor class? I was thinking a regular getter/setter but it's not working. "Incompatible override".

  9. #29
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need an actual setter, but you could make one if you wanted.
    Code:
    public function setMeteorArray(a:Array):void{
      this.meteorArray = a;
    }
    You'll have to declare a class level meteorArray variable, of course.

  10. #30
    Member
    Join Date
    May 2009
    Posts
    37
    aww man you've been a huge help...dont' have any problems right now though, i'm not finished... but it is slowly shaping up...i really appreciate you sticking it out with me...you can bet i'll be asking another question...probably later on tonight.

  11. #31
    Member
    Join Date
    May 2009
    Posts
    37
    so putting this at the top of the code

    PHP Code:
    private var metArr:Array = meteorArray 
    and this at the bottom

    PHP Code:
    public function setMeteorArray(a:Array):void{
              
    this.meteorArray metArr;
        } 
    will let the Building class recognize meteorArray or do I have to change something in the Building class as well?

  12. #32
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The top should be
    Code:
    private var meteorArray:Array;
    and the bottom should be
    Code:
    public function setMeteorArray(a:Array):void{
      this.meteorArray = a;
    }
    And you will actually have to call that method to pass a reference to the meteorArray class to your instance. Is this in Meteor or Building?

    The purpose of the local variable and a set method is so that you can pass the SAME meteorArray array to the things that need it.

  13. #33
    Member
    Join Date
    May 2009
    Posts
    37
    well, it is currently in the main document Class because that is where I declared and populated meteorArray as well as where I did the hit detection. The Building class is where I need to reference meteorArray so I can tell the building how much damage has been done to it...lol, that sounds funny. What is the difference between 'currentTarget' and 'this'?

  14. #34
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then the code in my last post needs to be in Building. It does not need to be in the main class, since the main class already has meteorArray.

    Unrelated to anything else in this thread:
    currentTarget is a property of an Event. It refers to the object which is currently processing the event. It is the something in something.addEventListener(...). 'this' is a reference to the instance of the class in which it appears. If you do new Building(), 'this' in any instance methods in that Building will refer to that Building.

  15. #35
    Member
    Join Date
    May 2009
    Posts
    37
    oh okay, thanks...i wouldn't say that is it unrelated because i've only seen 'this' in a specific class that wasn't a document class, and i thought you were saying put the setMeteorArray function in the main document class and my next question was going to be how does the main document class know what 'this' is.

  16. #36
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sorry, it's hard to put myself in the shoes of someone who's coming at this with no prior knowledge. Some stuff I take for granted that isn't as obvious as I think it is.

  17. #37
    Member
    Join Date
    May 2009
    Posts
    37
    it would be a lot easier to figure out why things aren't working properly if AS would output that they aren't...AS isn't outputing anything but it is not deducting the building health...it may have something to do with the way i'm handling meteorArray as 'a' i think.

    PHP Code:
    package{
        
    import flash.display.MovieClip;
        
        public class 
    Building extends MovieClip {
            private var 
    _buildingMaxHealth:Number 100;
            private var 
    _buildingMinHealth:Number 0;
            private var 
    _buildingCurrHealth:Number 100;
            private var 
    _i****:Boolean;
            private var 
    _isBeingRepaired:Boolean;
            private var 
    _isDestroyed:Boolean;
            private var 
    meteorArray = Array;
            
            public function 
    Building() {
                
    init();
            }
            
            private function 
    init():void {
                
    //this._i**** = false;
                
    this._isBeingRepaired false;
                
    this._isDestroyed false;
                
    this.stop();
            
                
    repairHealth();//call to the repair function
                
    subtractHealth();
            }
                    
            public function 
    repairHealth(){//function for repairing building during the Repair phase
                //if building isn't destroyed and its health is not maxed out
                
    if (this.isDestroyed != true && this.buildingCurrHealth this.buildingMaxHealth){
                    
    this.buildingCurrHealth += 3;//add 3 to its current health
                
    }
                if (
    this.buildingCurrHealth >= this.buildingMaxHealth){
                    
    this.buildingCurrHealth buildingMaxHealth;
                }
            }
            
            public function 
    subtractHealth():void{
                if (
    this.i**** == true && this.buildingCurrHealth this.buildingMinHealth){
                    for (var 
    i:uintmeteorArray.lengthi++){
                        if (
    a[i].diameter 36 && a[i].diameter 31){
                            
    this.buildingCurrHealth -= 30;
                            
    this.i**** = false;
                            
    trace ("this building's CurrHealth is " buildingCurrHealth);
                        }
                        if (
    a[i].diameter 32 && a[i].diameter 27){
                            
    this.buildingCurrHealth -= 20;
                            
    this.i**** = false;
                            
    trace ("this building's CurrHealth is " buildingCurrHealth);
                        }
                        if (
    a[i].diameter 28 && a[i].diameter 10){
                            
    this.buildingCurrHealth -= 10;
                            
    this.i**** = false;
                            
    trace ("this building's CurrHealth is " buildingCurrHealth);
                        }
                        if (
    this.buildingCurrHealth <= buildingMinHealth){
                            
    this.buildingCurrHealth buildingMinHealth;
                            
    this.isDestroyed true;
                            
    this.gotoAndStop(2);
                        }
                    }
                }
            }
            
            public function 
    setMeteorArray(a:Array):void{
                
    meteorArray a;
            } 

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