[SOLVED] Performance question about embedding bitmaps more than one


(Bora Kasap) #1

What if i embed bitmaps more than one like that?

[Embed(source = 'assets/enemy_kamikaze.png')] private const KAMIKAZE:Class;
[Embed(source = 'assets/enemy_shooter.png')] private const SHOOTER:Class;
[Embed(source....
[Embed(sou...
[Embed...
.....
...
..
.

Even i use one of them like that?

if (kind == "kamikaze") graphic = graphicfx = new Image(KAMIKAZE);
else if (kind == "shooter") graphic = graphicfx = new Image(SHOOTER);
else if ("kind ==......
else if (.....
else if....
.....
...
..
.

Is that makes this entity lacky for low sys computers? What if i want to use specified bitmap depending on a variableā€™s value?


(Bora Kasap) #2

Iā€™ve tried to create another classes(for enemy types) extending Enemy class(should be like that?), but when i do this and try to create new Kamikaze() or new Shooter() it is giving error message because of it is a static type so i canā€™t use it in var target:Enemy = world.add(new Kamikaze()), it requiring var target:Kamikaze = world.add(new Kamikaze()) and that is not useful for me.


(Bora Kasap) #3

Iā€™m waiting for some helpā€¦


(JP Mortiboys) #4

If you keep your related embeds in a designated class, and make the embed variables public, then you can get at them like this:

public class EnemyGfx {
  [Embed(source='assets/enemy_kamikaze.png')] public static const KAMIKAZE:Class;
  [Embed(source='assets/enemy_shooter.png')] public static const SHOOTER:Class;
  // ...
}

/// Elsewhere
var kind = FP.choose("KAMIKAZE", "SHOOTER", ...);
var img = new Image(EnemyGfx[kind]);

Note that is is quite slow (relatively speaking); itā€™s fine for loading a level but you wouldnā€™t want to do this in an intensive loop.

I donā€™t know what you mean here; perhaps you should show us your code?


(Bora Kasap) #5

That looks so good, thanks for it, i canā€™t try it now, iā€™ve to wait for eveningā€¦

But i have a question about what you suggestā€¦

Are you sure thats gonna work?

var img = new Image(EnemyGfx[kind]);

Because, iā€™ve tried this and this didnā€™t work:

[Embed(source = 'assets/enemy_kamikaze.png')] private const KAMIKAZE:Class;
[Embed(source = 'assets/enemy_shooter.png')] private const SHOOTER:Class;

public var kind:String = new String("KAMIKAZE");


public function Enemy() {
graphic = new Image(kind);
}

Thats returning an error message about ā€œIā€™m trying to use a wrong string typeā€

So iā€™m gonna try your method at evening, maybe my error was about something anotherā€¦

Thanks for help!


(Bora Kasap) #6

Hey, i made it with your method. But not worked completelyā€¦

That didnā€™t work;

var graphic = new Image(EnemyGfx[kind]);

So i made it like that;

if (kind == "kamikaze") var graphic = new Image(EnemyGfx.KAMIKAZE);
else if (kind == "shooter") var graphic = new Image(EnemyGfx.SHOOTER);

Now iā€™m gonna need ā€œif statementsā€ to set it up. Not a problem, because it is not for updating functions, only works when entity spawning. Anyway if you learn how to do this, please let me know. Thanks for everything.


(David Williams) #7

If you wanted, rather than using a bunch of ā€˜if()ā€™ statements, you could use a ā€˜switch()ā€™ statement.

switch(kind)
{
    case "kamikaze":
        var graphic = new Image(EnemyGfx.KAMIKAZE);
        break;
    case "shooter":
        var graphic = new Image(EnemyGfx.SHOOTER);
        break;
}

(Bora Kasap) #8

oh thats better ^,^ thank you