2

I'm new to AS3 but I've done some stuff with flash before. I have a class that extends MovieClip and has some properties and functions that I've defined. How do I associate an image with this class so that I can add objects of its type to the stage and see that image (along with being able to associate mouse events, etc.)? Here's the class:

public class Tank extends MovieClip {
    public var Id:int;
    public var HP:int;
    public var Dmg:int;

    public function Tank(id:int, hp:int, dmg:int){
        // constructor code
        this.Id = id;
        this.HP = hp;
        this.Dmg = dmg;

    }
}
Jehosephat
  • 45
  • 1
  • 8
  • By `Image` do you mean that you've imported a Bitmap or created some Vector graphics with the tools in Flash and converted it to a MovieClip? – Marty Feb 04 '13 at 00:11

1 Answers1

2

Cleanest way to do this would be to drop the extends MovieClip portion of your Tank and give it a property that will refer to its display.

public class Tank
{
    private var skin:Bitmap;

    public function Tank(...)
    {
        skin = new Bitmap( new YourImage() );
    }

    public function get Skin():Bitmap
    {
        return skin;
    }

}

YourImage would be replaced with the class name that you give to your Bitmap in the library if you go to Properties -> ActionScript:

enter image description here

This way when you create a Tank, you can add it to the DisplayList like this (from somewhere that you have access to the stage, like your document class or the timeline):

var tank:Tank = new Tank(...);
stage.addChild(tank.Skin);
Marty
  • 39,033
  • 19
  • 93
  • 162