AS3 - What is the best or recommended way to pack bitmaps into Sprite containers?

I am learning AS3 using the FlashDevelop IDE and Flex to compile.

I add images by creating a Bitmap class and then embedding .png in the code, for example:

Enemy.as

 package enemies { import flash.display.Bitmap; import flash.events.MouseEvent [Embed(source="../../assets/gardengnome.png")] public class Enemy extends Bitmap { public function Enemy() { trace("enemy constructed"); } } } 

I found out that to handle MouseEvent I need to put this Bitmap in a Sprite container.

Now, not knowing anything better, this is how I did it:

I create a new variable that holds the enemyContainer in Main.as and adds it to the scene:

 package { import enemies.Enemy; import enemies.EnemyContainer; import flash.display.Sprite; import Player.Player; public class Main extends Sprite { public var enemyContainer:EnemyContainer = new EnemyContainer(); public function Main():void { addChild(enemyContainer); } } } 

And then the enemyContainer class calls Enemy Bitmap , which contains the graphics and adds it to itself as a child:

 package enemies { import flash.display.Sprite; import flash.events.MouseEvent public class EnemyContainer extends Sprite { private var enemy:Enemy = new Enemy(); public function EnemyContainer() { trace("enemyContainer constructed"); addChild(enemy); addEventListener(MouseEvent.CLICK, handleClick); } private function handleClick(e:MouseEvent):void { trace("Clicked Enemy"); } } } 

I do not have enough experience to see any problems with this. I can change the graphics in the Enemy Bitmap class without having to deal with anything else, and Main.as handles the enemyContainer positioning.

However, if there is a recommended or more effective way to handle this, I would like to know it now before I get used to it. Is there a better way to do this?

Thanks for any advice!

+4
source share
3 answers

In your example, it looks like you are creating 3 classes to implement a bitmap inside a sprite:

  • Enemy Expands Bitmap Images
  • EnemyContainer Extends Sprite
  • Sprite core extensions

Your method is fully valid and you will not have a problem with it. In fact, there is no better way to do this. Which is best depends on the context. However, it may be easier to manage if you use Enemy as part of EnemyContainer, so you only have two classes:

  • home
  • Enemycontainer

In EmemyContainer, you make a private class for the raster image of the enemy as follows:

 package enemies { import flash.display.Sprite; import flash.events.MouseEvent public class EnemyContainer extends Sprite { [Embed(source="../../assets/gardengnome.png")] private EnemyBitmap:Class; public function EnemyContainer() { trace("enemyContainer constructed"); addChild(new EnemyBitmap()); // Creates a new instance of your bitmap class. addEventListener(MouseEvent.CLICK, handleClick); } private function handleClick(e:MouseEvent):void { trace("Clicked Enemy"); } } } 

You can also usefully use the mouseChildren property and buttonMode Sprite property, but this will depend on how you want the mouse interaction to work.

== In response to comments ==

To place a bitmap in the registration center:

 var temp:Bitmap = new EnemyBitmap() as Bitmap; temp.x = -temp.width/2; temp.y = -temp.height/2; addChild(temp); 
+2
source

What are you doing ok

If you want fewer classes, you can also insert a bitmap into your container class

 public class Enemy extends Sprite { [Embed(source="../../assets/gardengnome.png")] private var assetClass:Class; private var asset:Bitmap; public function Enemy() { //init the class //do this in a seperate method, because the constructor is executed by a just in time compiler. Means less code better performance. init(); } private function init():void { asset = new assetClass(); addChild(asset); } } 
0
source

Noble effort, but try this as a best practice instead:

 public class Enemy extends Sprite { public function Enemy() { var bmp = new EnemyBitmap(); // what is currently your Enemy class should be EnemyBitmap addChild(bmp); } } 

This method is called "implementation", where the desired class (bitmap) is loaded into the container instead of the container that extends the Bitmap class itself ("inheritance").

-1
source

Source: https://habr.com/ru/post/1481230/


All Articles