Skip to main content
May 8, 2012
Answered

How to create an instance?

  • May 8, 2012
  • 1 reply
  • 1105 views

Hello,

I am starting ActionScript now, so I figured why not ask something here.

I previously worked with GameMaker (GML) which is much more simple I guess.

Howsoever, in order to practice a little I am trying to "convert" some aspects of my games to flash. Now I stuck with something very simple (at least in GML). That would be creating a new instance, in this case a projectile (in GML - instance_create(x,y,object);).

Now, how do I do that in ActionScript? It would be nice, if you could help me.

Sincerely, Robin

This topic has been closed for replies.
Correct answer Ned Murphy

There are a few different ways to create things, but it generally boils down to happening with the two lines of code that will follow below.

First create the object as a movieclip symbol so that it lives in the library.  Then right click it and choose Linkage.  In the panel that appears, select the option to export it for actionscript and assign it a Class name... let's say you give it a class name of "Projectile"

To create a new instance of it...

var proj:Projectile = new Projectile();

That is all you need to create a new instance.  But the next step is to add it to the display list so that it has stage presence...

addChild(proj);

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
May 8, 2012

There are a few different ways to create things, but it generally boils down to happening with the two lines of code that will follow below.

First create the object as a movieclip symbol so that it lives in the library.  Then right click it and choose Linkage.  In the panel that appears, select the option to export it for actionscript and assign it a Class name... let's say you give it a class name of "Projectile"

To create a new instance of it...

var proj:Projectile = new Projectile();

That is all you need to create a new instance.  But the next step is to add it to the display list so that it has stage presence...

addChild(proj);

May 8, 2012

Thank you, i got it on the stage.

That beeing taken care of there is something else. I initially create it through - var proj:projectile = new projectile(); -.

Next I want it to actually "exist" after pressing the fire-button, so in - function fl_Fire(event:Event) - I put - addChild(proj); -.

Now I want it to move, of course, so I use - proj.addEventListener(Event.ENTER_FRAME, fl_MoveProjectile); - (this could be the problem I suppose) and in - function fl_MoveProjectile (event:Event) - I put the moving-code.

The Problem is, it is already moving beeing created initially, before it is actually visible due to the fire button.

Probably you could help me with that, as well.

Sincerely, Robin

Ned Murphy
Legend
May 8, 2012

The easiest way would be to not assign the ENTER_FRAME listener until it is visible.

One minor mention.  I used a capital "P" for Projectile because class names are named that way as standard procedure.  It helps to more easily differentiate them from instance names and variables.