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!
source share