PDA

View Full Version : [AS3] Space Invaders + classes


Geoff666
09-23-2009, 01:46 PM
Hi there,

I'm kinda new to AS3 and doing my very best to learn the basics.
I'm familiar with AS2, so syntax isn't really a problem.

For learning AS3 I decided to make some sort of Space Invaders, this because it involves classes, and that's the problem I bump in now.

From a tutorial I've been told classes are written like this;


package {
import flash.display.MovieClip;
public class Alien1 extends MovieClip
{
public function InitEnemy()
{
x=100;
y=100;
}

public function MoveEnemy()
{
y++;
}
}
}

I've linked the alien sprite to that class, but still it isnt being initialized when I test the movie.
I've been using 2 files btw, 1 for the sprite (.fla), and 1 for the class (.as), and so far, my .fla doesnt contain any code.

Am I doing this the right way, or did I forget something really stupid?
Any help would be much appreciated!


Thanks in advance,

Geoff.

kingundkong
09-23-2009, 02:00 PM
name your constructor class the same as you class, so this gets fired when you construct your alien. your class should look like:

package
{
import flash.display.MovieClip;
public class Alien extends MovieClip
{
public function Alien()
{
x = 100;
y = 100;
}

public function MoveEnemy()
{
y++;
}
}
}

link your graphical alien in the library to the class Alien and
on the main timeline write:

var alien:MovieClip= new Alien();
this.addChild(alien);

hope this helps.

Geoff666
09-23-2009, 03:28 PM
I've tried your solution, but it still doesn't work.
I probably did something wrong with the "var alien:MovieClip= new Alien();
this.addChild(alien);"-part.

With 'main timeline' you mean the first frame of my .fla timeline, right?

rrh
09-23-2009, 04:39 PM
Do you have set "export on first frame" in the library properties of the Alien?

Geoff666
09-23-2009, 06:59 PM
Yeah, I did. :eek:

kingundkong
09-23-2009, 07:06 PM
did you name your class Alien not Alien1 in your classfile?

Geoff666
09-23-2009, 07:16 PM
I assume you mean my .as file?

I'll post it, might save us a few hundred posts :p

-------------.as-file:------------------

package {
import flash.display.MovieClip;
public class Alien1 extends MovieClip
{
public function Alien1()
{
x=100;
y=100;
}

public function MoveEnemy()
{
y++;
}
}
}

-----------.fla-file-------------------

var alien:MovieClip= new Alien1();
this.addChild(alien);



For the rest I've been to library -> alien-object -> linked it to "Alien1"
And set the import to first frame thingy.

I really can't seem to spot the problem.

kingundkong
09-23-2009, 07:20 PM
your class should look like this:

package
{
import flash.display.MovieClip;
public class Alien extends MovieClip
{
public function Alien()
{
x = 100;
y = 100;
}

public function MoveEnemy()
{
y++;
}
}
}

Geoff666
09-23-2009, 07:59 PM
That still does not do the trick :(

kingundkong
09-23-2009, 08:05 PM
i attached a sample fla+as file, just take a look. i guess you did not name your cymbal in the library the right way or did not link it in the right way to the class file.

Geoff666
09-23-2009, 08:15 PM
I can't open the .fla file, Flash crashes and tells me to report -.-'
Are you using CS4?

Could you maybe explain to me how to properly link a sprite to a object?
Thanks in advance!

kingundkong
09-23-2009, 08:22 PM
ups, yeah its cs4, so here is the cs3 verison.

Geoff666
09-23-2009, 08:25 PM
Can't open it either.
But I see you're haveing a Mac, and I'm still on sucky WXP, would that make a difference?

rrh
09-23-2009, 08:28 PM
What is it doing instead of working?

Is it not visually appearing on the stage, or is it appearing but not following the behaviour defined in the code, or what?

kingundkong
09-23-2009, 08:30 PM
this should not be a problem. what is your error message

Geoff666
09-23-2009, 08:34 PM
It says it encountered unexpected file content, and shuts down whenever i close the report.

@ rrh
In my .fla i've set the graphical object on my screen, and thats exactly what it shows.
It doesnt set ts x,y on 100.
So I guess the object isn;t listening to the code.

kingundkong
09-23-2009, 08:50 PM
you do not need to put the graphical object on stage. by doing
var alien:MovieClip= new Alien();
this.addChild(alien);
you put it on stage and than it will be at x, y position you were setting in the as file

Geoff666
09-23-2009, 08:54 PM
OK! Got it working now.. or atleast the initialization, it doesn't move, while it should.

EDIT: wait, i'm thinking too hard.
I need to call it;s movement function, right?
where do i do that?

kingundkong
09-23-2009, 08:57 PM
for having it move you need to set a timer or an enter frame event and call the alien.MoveEnemy() function;

Geoff666
09-23-2009, 09:00 PM
in the main timeline that is?

Geoff666
09-23-2009, 09:30 PM
Ok, got that working too, thanks all!!


Now my I have another question..
When you object oriented programming, is this the right way to do it then?

I added a eventlistener in the main timeline which calls all functions.