A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Tile Based Game-- My first shot at OOP

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    11

    Tile Based Game-- My first shot at OOP

    I've been designing the layout of a pretty sweet kirby-esque platformer in pseudo-code and notes and I've been reading through the AS3.0 bible (very very good stuff). I'm trying to have every object in the block class automatically construct registration fields to interact with players enemies and other objects. Here are the classes I have built for the Block and Registration fields respectively:

    package
    {
    import flash.display.MovieClip;
    public class BlockReg extends MovieClip
    {

    //enabling block subclasses to create new regs
    public function BlockReg(newX:Number, newY:Number, newWidth:Number, newHeight:Number):void
    {
    var reg = new BlockReg;
    this.x = newX;
    this.y = newY;
    this.width = newWidth;
    this.height = newHeight;
    }
    }

    }







    package
    {
    import flash.display.MovieClip;
    public class OneBlock extends Block
    {

    public function OneBlock()
    {
    var reg = new FloorReg
    FloorReg(this.x, this.y + this.height / 4, this.width, this.height / 4)
    }

    }

    }



    I'm definitely doing something wrong here as the code isn't functioning with the blocks on the stage. It could be a problem with linking the library items to the classes as I have no idea what I'm doing with the CS5 interface.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You haven't told us what errors you're getting.

    You are calling the constructor of BlockReg within Blockreg (and without the right arguments). This will lead to an infinite recursion. You never use the local reg variable anyway.

    In OneBlock, you create a local reg variable for a FloorReg, then never use it. The next line appears to be you trying to call the FloorReg constructor again, but I can't really be sure. If that is what you're attempting, you'd need the "new" keyword. And you'd need to do something with that new FloorReg.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    you're right, that isn't very clear at all XD

    When my file runs it gets the following error

    /Users/Gabriel/Documents/Flashworks/as3/Anonimo/BlockReg.as, Line 10 1136: Incorrect number of arguments. Expected 4.

    It should also be noted that FloorReg is a subclass of BlockReg, no extra code, just a different movie clip in the library with BlockReg as baseclass.

    I suppose my main dilemma here might be understanding constructor syntax... is the constructor of a class executed whenever a new var of that class is created? or... should I call the constructor function directly. I guess I just don't understand where to put the line
    var reg = new FloorReg

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Okay, some basics: Yes, the constructor is called whenever you create a new instance of that class. It constructs the new instance. You are getting the error because the constructor function expects 4 arguments (newX, newY, newWidth, newHeight), but you are calling it with no arguments.

    If you fix that error, you will get an infinite recursion since your BlockReg constructor tries to call itself. To build a BlockReg, first you have to build a BlockReg, which requires that you build a BlockReg...

    I have no idea why you are trying to create a BlockReg inside the constructor, then throwing that BlockReg away. Just delete this line:
    Code:
    var reg = new BlockReg;
    Incidentally, I suggest always using explicit arguments in your constructor calls (use "new SomeObject();" rather than "new SomeObject;"), for uniformity and to make it easy to see that the constructor is being called. Also, you should always give your variables types, even if that type is * (here, it should have been BlockReg, if you needed that variable, which you don't).

    Can you explain the relationship between a Block and a BlockReg? OneBlock extends Block, and tries to create an instance of FloorReg, which is a BlockReg. What do you want to do with that FloorReg, once you have properly created it? As of now, you are just throwing it away because the reg variable is declared inside the OneBlock constructor. That means that it is local to that function, and since you don't do anything with it, it simply falls out of scope.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    The FloorReg will react with the Player object in the game in a hitTest and let it know that it is on the ground so the player can use the Walk, Jump, and Stand functions. I'm just trying to construct an easy way for me to throw blocks onto the stage and have them construct their own registration areas.

    What I'm trying to get the BlockReg constructor to do is create a new registration area and place it on top of itself no matter where it is on the stage.

    Can you tell me how I would go about putting the FloorReg into the global scope from within my OneBlock constructer function?

    Also:
    I updated my code using your advice which was incredibly helpful, here's the new guts. I throw the arguments for the constructer in the prens of new someVar(); right?


    Code:
    package  
    {
    	import flash.display.MovieClip;
    	public class BlockReg  extends MovieClip
    	{
    		
    		//enabling block subclasses to create new regs
    		public function BlockReg(newX:Number, newY:Number, newWidth:Number, newHeight:Number):void 
    		{
    			this.x = newX;
    			this.y = newY;
    			this.width = newWidth;
    			this.height = newHeight;
    		}
    	}
    	
    }
    Code:
    package  
    {
    	import flash.display.MovieClip;
    	public class OneBlock extends Block 
    	{
    
    		public function OneBlock() 
    		{
    			var reg = new FloorReg(this.x, this.y + this.height / 4, this.width, this.height / 4);
    		}
    
    	}
    	
    }

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    BlockReg looks good if all you want is to place and stretch it by passing values into the constructor, which means FloorReg is good too. OneBlock still needs some work.

    For starters, I don't know what Block is yet. Is it a DisplayObject like a MovieClip or Sprite, or does it just extend from Object?

    There's not really any such thing as global scope, so you can't put your FloorReg instance in global scope. I'm not sure why you'd want to either, since if you did do that, you'd simply overwrite the variable each time you made a new OneBlock.

    If OneBlock is a DisplayObject that you are positioning on stage through the IDE, and you want it to put the new FloorReg on stage too, then you'll want to use addChild. Since coordinates are relative to the display parent, you should pass 0 rather than this.x or this.y to get the new reg in the right place. And you'll want to save that reg so you can access it later.

    Code:
    package  
    {
    	import flash.display.MovieClip;
    	public class OneBlock extends Block 
    	{
    
    		public var reg:BlockReg;
    
    		public function OneBlock() 
    		{
    			reg = new FloorReg(0, this.height / 4, this.width, this.height / 4);
    			addChild(reg);
    		}
    
    	}
    	
    }

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    5Tons, you are my favorite person right now, code is running great. I think I'm going to revise this for obvious reasons, but I understand the purpose of constructors now which I was really very confused about XD

    I do have one last question though: how do I access this reg now if I were to have it acting as a hitTest target. From lynda tutorials, I pretty much got that addChild puts the object inside a movieclip but then again it is a public var, so maaaaybe I can access it directly from the Block class...?

    PS: is there some way I can rate or promote you for helping me?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To access the reg property of your OneBlock, you just need a reference to it. You could have declared reg in Block, then you could access it there. Since Block is a superclass of OneBlock, OneBlock will inherit the reg property. But are you really talking about accessing from Block, or did you mean from BlockReg?

    Anyway, if you move the declaration to Block:
    Code:
    package{
    
      public class Block extends Sprite {
    
        public var reg:BlockReg;
       
        public function Block(){
           //constructor stuff here
        }
    
        private function someFunctionWhichUsesReg():void{
          trace(reg);
        }
    
       //more class stuff here
    
      }
    
    }
    Code:
    package {
    
      public class OneBlock extends Block{
    
         //we do NOT need to redeclare reg here, because Block declared it for us.
    
         public function OneBlock(){
    	reg = new FloorReg(0, this.height / 4, this.width, this.height / 4);
    	addChild(reg);
         }
    
      }
    
    }
    I've left out import statements, but you can figure those out.

    Making a var public does not put it in any sort of global space. It is still a property of a particular instance. All that public means is that if you have a reference to that instance, you can also get to the public property.

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