Skelig Seklig
Skelig Seklig

Reputation: 49

dynamically loading bitmap from library into movieclip

I have one MovieClip class called HomingBullet which is an empty MovieClip (no graphics inside it). I'm trying to make it so that when I instantiate a HomingBullet, I can select a graphic from the library. Currently, the constructor for HomingBullet looks like this:

public function HomingBullet(speed:int)
{
    playerX = Main.instance.getPlayerX();
    playerY = Main.instance.getPlayerY();
    this.speed = speed;
    addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}

A typical instantiation of this class currently looks like this:

var tempBulletA = new HomingBullet(6);

Essentially I'm trying to make it so that when instantiating it I can choose a graphic from the library to use.

Upvotes: 1

Views: 217

Answers (2)

prototypical
prototypical

Reputation: 6751

I am going to assume that all your bullets in your library are MovieClip based. You could have your constructor as :

public function HomingBullet(speed:int, bulletClass:Class)
{
    addChild(new bulletClass());
    playerX = Main.instance.getPlayerX();
    playerY = Main.instance.getPlayerY();
    this.speed = speed;
    addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}

To create an instance :

var tempBulletA = new HomingBullet(6, someBullet);

someBullet in the above example would be whatever your Class name is for the desired bullet symbol you want to use.

Upvotes: 1

Scott
Scott

Reputation: 948

Sure. The easiest way is to create a MovieClip in your library and place the bitmap in that. Then open the properties dialogue for that movieclip, click the Advanced drop down, and then check "Export for ActionScript" and give it a Class name. For this example, let's say I chose "Image1" as my class name.

Then in your code, you can do something like:

public function HomingBullet(speed:int, image:DisplayObject)
{

    addChild(image);
    playerX = Main.instance.getPlayerX();
    playerY = Main.instance.getPlayerY();
    this.speed = speed;
    addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}

And you would instantiate it with:

var tempBulletA = new HomingBullet(6, new Image1());

You can use just a bitmap and export it the same way to use (without the movieclip wrapper) but this is a bit more annoying as you have to know the height and width of the bitmap when you're creating it.

Upvotes: 1

Related Questions