A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: OOP game - movieClip

  1. #1
    Member
    Join Date
    Jun 2004
    Posts
    83

    OOP game - movieClip

    Hi,

    I'm workin on a OOP game, and have a question. Is it posible to use/assign new "enemy movieClips" from Library - for each new level by using the same AS3 class (Enemy.as)?

    For example, on level1 we got like..

    var myEnemy:Enemy = new Enemy(enemy1_mc, speed, shield....etc);

    On level2:
    var myEnemy:Enemy = new Enemy(enemy2_mc, speed, shield....etc);

    /S-fish
    Last edited by swordfish123; 10-02-2008 at 09:32 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sure, why not? You'll have to add linkage properties for enemy1_mc and enemy2_mc so that they have classes. Then your Enemy constructor could look like this:
    Code:
    public function Enemy(enemySpriteClass:Class, speed:Number, shield:Number, ... more){
      var enemySprite:DisplayObject = new enemySpriteClass();
      addChild(enemySprite);
      // do other stuff.
    }
    You may have to cast to DisplayObject when creating enemySprite.

  3. #3
    Member
    Join Date
    Jun 2004
    Posts
    83
    I got an error, probably some casting problem (as you said).

    1046: Type was not found or was not a compile-time constant: DisplayObject.

    Here is my Enemy.as class:

    Code:
    package{
    	import flash.display.MovieClip;
    	import flash.display.Sprite; 
    	
    	public class Enemy extends MovieClip{
    		
    		public var enemySpriteClass:Class
    		public var speed:Number;
    		public var shield:Number;
    		public var size:Number;
    		
    		public function Enemy(enemySpriteClass, speed, shield, size){
    			var enemySprite:DisplayObject = new enemySpriteClass();
    			addChild(enemySprite);
    			this.speed = speed;
    			this.shield = shield;
    			this.scaleX = this.scaleY = size;
    		}
    
    	}
    }

    //create a new enemy in main app, Enemy is also the class-name of the movieClip "enemy_mc" in library
    var enemy_mc:Enemy = new Enemy(Enemy, 10, 15, 1);
    addChild(enemy_mc);


    Thankful for some advise
    /S-fish

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The direct cause of your error is that you need an import line for DisplayObject like you have for MovieClip and Sprite.

    But after that you've got some other issues.
    First, you should type your parameters. It allows compile time checking that you're passing the right stuff.

    Second, you can't pass Enemy as the class to instantiate when creating an Enemy. It a) has no no-argument constructor so "new enemySpriteClass()" will fail. And b) Even if that worked, you'd go into an infinite loop since each new Enemy would attempt to create another Enemy. You need instead make Enemy a class without a linkage (just have the .as file in your classpath), and have different classes linked to your library enemy clips, like you had in your first post.

  5. #5
    Member
    Join Date
    Jun 2004
    Posts
    83
    Thank you for that detalied information!

    Let me see if I understand this:
    1. I've added the import flash.display.DisplayObject; ontop of the class Enemy.as
    2. No linkage for the library enemy clips (enemy_mc)
    3. Create the new enemy instance/object like:
    var enemy_mc:Enemy = new Enemy(enemy_mc, 10, 15, 1);
    addChild(enemy_mc);

    This gives me an error output:


    TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at Enemy$iinit()
    at main_animation_fla::MainTimeline/main_animation_fla::frame1()


    I'm not sure what you mean with "just have the .as file in your classpath" ?

    /S-fish

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No linkage for Enemy, meaning that there is no library item whose class is Enemy. Your library items like enemy_mc SHOULD be linked to a class, but that class should be something, anything, other than Enemy. Enemy_mc1 for instance.
    Code:
    var enemy_mc:Enemy = new Enemy(Enemy_mc1, 10, 15, 1);
    Those "enemies" are, in order:
    1: a variable named "enemy_mc"
    2: which is typed as class Enemy
    3: and set to the result of calling the Enemy class constructor
    4: which is passed the Enemy_mc1 class

  7. #7
    Member
    Join Date
    Jun 2004
    Posts
    83
    Thank you, this is working great!

    One question, can I reach another movieClip inside the "enemy_mc instance"? For example like in AS 2, enemy_mc.enemyHit_mc.gotoAndPlay(2);

    This wont work here...

    /S-fish

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Not directly. Since your Enemy class contains an instance of the class passed in (Enemy_mc1 in this example), there's an extra layer. Instead of trying to do that directly, what I'd suggest is to put a named method in Enemy which does what you're trying to do.

    Code:
    package{
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	
    	public class Enemy extends MovieClip{
    		
    		public var enemySprite:MovieClip;
    		public var speed:Number;
    		public var shield:Number;
    		public var size:Number;
    		
    		public function Enemy(enemySpriteClass, speed, shield, size){
    			this.enemySprite:MovieClip = MovieClip(new enemySpriteClass());
    			addChild(enemySprite);
    			this.speed = speed;
    			this.shield = shield;
    			this.scaleX = this.scaleY = size;
    		}
    
                    public function showHit():void{
                      this.enemySprite.enemyHit_mc.gotoAndPlay(2);
                    }
    	}
    }
    Even better would be to define an EnemyAppearance interface for your individual enemy graphic classes to implement with methods like showHit, fire, etc. But that's probably a bit more abstracted than you need in this project.

    I changed enemySprite from DisplayObject to MovieClip because since you're now reaching into it you need to a) know that it can have properties, and b) its convenient to access those properties dynamically, because you don't have a specific type which declares them.

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