Wednesday, 4 September 2013

How to create multiple instances of the same class in as3?

How to create multiple instances of the same class in as3?

Noob question. Ok, so I'm trying to create two instances of my notes (it's
an imported pic) class at separate x and y coordinates, then I want both
of them to move right. Right now I programmed a loop, and the loop works
OK, but it only keeps the last instance that was created. Here's my code.
I really appreciate any help that anyone can give. Thanks!
package
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.getDefinitionByName;
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
private var speed:int = 8;
[Embed(source="../lib/Dodgethis.jpg")]
public var Notes:Class;
public var numnotes:Number;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.addEventListener(KeyboardEvent.KEY_DOWN, testevent);
}
private function testevent(e:Event = null):void {
trace("testevent has run");
appear(350, 250);
//ap2(150, 150)
//addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function appear(x:Number, y:Number) {
var arr1:Array = new Array;
numnotes = 4;
for (var i = 0; i < numnotes; i++)
{
trace (i);
var nbm:Bitmap = new Notes;
if (i == 0) {
this.x = 400;
this.y = 400;
addChild(nbm);
trace ("1 should be different");
} else {
trace ("this is working");
this.x = 150;
this.y = 150;
addChild(nbm);
arr1.push(nbm);

No comments:

Post a Comment