Be a Supporter!

As2: Spawning Mc With Code

  • 1,691 Views
  • 14 Replies
New Topic Respond to this Topic
UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
As2: Spawning Mc With Code 2009-12-01 06:32:47 Reply

I'm a capable coder so am not asking for the code of what I am about to ask, but how to structure it, and how to come about actually doing it.

What I want to happen, is have enemies (MC's) which randomly spawn in a certain place (I can manage controlling where they spawn)
My problem is that I want them to have their own code which they execute, and other objects in the stage must be able to react with them.

I want to know how using a controller MC (or a better way) to cause them to spawn, give them a name which is unique to them, (by adding 1 to the name of the spawned enemy), and have them execute their own code using _global. variables.

Once again, I am not asking for code, just a structure to how to come about doing it (like where to put the code that I want all the mc's of the enemy to spawn with and run)

Thanks for your help, I will mention you in the credits with a link to your Newgrounds Page (or your site) but only if you are helpful in solving my problems.

Thanks NG

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to As2: Spawning Mc With Code 2009-12-01 06:54:37 Reply

Read about classes and arrays. You should create a class file for the different enemies you have, and when you create the enemies on stage you should add them to an array from which you can control them.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Bogoblin
Bogoblin
  • Member since: Nov. 21, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 06:54:47 Reply

Take a look in these places:

AS: Main
AS: Duplicated Movie Clips


crob
I do Let's Plays on youtube!

BBS Signature
UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 06:57:35 Reply

At 12/1/09 06:54 AM, ProfessorFlash wrote: Read about classes and arrays. You should create a class file for the different enemies you have, and when you create the enemies on stage you should add them to an array from which you can control them.

I shall have a look at them, I do understand arrays, but how would an array control them?

Also thanks for the two links Bogoblin.

Bogoblin
Bogoblin
  • Member since: Nov. 21, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 06:58:36 Reply

At 12/1/09 06:57 AM, UndefinedArt wrote:
Also thanks for the two links Bogoblin.

No problem. Bookmark AS: Main. It really comes in handy.


crob
I do Let's Plays on youtube!

BBS Signature
UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 07:02:55 Reply

This is just a small ask? but say I was exporting a flash game as an swf, and it had external script, how would I apply the external script to the swf if on a site like newgrounds.
=D

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to As2: Spawning Mc With Code 2009-12-01 07:07:18 Reply

At 12/1/09 06:57 AM, UndefinedArt wrote:
I shall have a look at them, I do understand arrays, but how would an array control them?

Well array doesn't actually 'control' them. If you know arrays you know they are used to store stuff neatly in one variable from which you can access many different values inside of it. So I meant that you can access the enemies easily when they are in an array and then do whatever you want with them.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

grafik2d
grafik2d
  • Member since: Jun. 29, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 08:04:12 Reply

Personnaly I would do it this way

var myMc:MovieClip=attachMovie(all the usual stuff and maybe some custom parameter in {} like Xspeed:xxx, EnnemyType:xxx etc)
myMc.onEnteFrame=function(){ your object behavior function call, I usually use a Switch case if I have diferent type of ennemy spawning}

if you want to use array you can use push() to push the new movie clip in an array then use a for loop to asign them a function like this

var myMc:MovieClip=attachMovie(all the usual stuff and maybe some custom parameter in {} like Xspeed:xxx, EnnemyType:xxx etc)
myArr.push(myMc)

onEnterFrame=function(){
myEnnemyFunction()
}

function myEnnemyFunction(){
for(i=0;i<myArr.lenght;i++){
myEnnemyBehavior(myArr[i])
}
}

function myEnnemyBehavior(myEnn:MovieClip){
your ennemy code
}

UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 22:02:03 Reply

Thanks guys,
I'll try out these methods.

I'm also wondering, when u duplicate a movieclip, does the movieclip keep all it's code, since I'm probably going to try and have all the movieclip's keep there code, and since the code has to be on the mc itself, does that mean I have to put an mc in an mc, if you know what I mean.

I'd love to hear suggestions on how to attempt that.

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-01 22:27:06 Reply

You can actually code on the first frame of the clip itself, referencing itself and it will have access to all of the global variables via _root.

Not the most efficient/organized way, but incredibly easy to implement.


Perpetually looking for time to return to the arts.

BBS Signature
UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-02 00:39:00 Reply

At 12/1/09 10:27 PM, Johnny wrote: You can actually code on the first frame of the clip itself, referencing itself and it will have access to all of the global variables via _root.

Not the most efficient/organized way, but incredibly easy to implement.

Yes I thought that myself. It is abit messy but I think thats the way I'm gonna go.
Thanks.

Innermike
Innermike
  • Member since: Sep. 11, 2009
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-02 05:41:57 Reply

via _root.
thats the way I'm gonna go.

Shame, good luck with that though


nobody

UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-02 10:10:21 Reply

At 12/2/09 05:41 AM, hdxmike wrote:
via _root.
thats the way I'm gonna go.
Shame, good luck with that though

If you have a better opinion I am all ears.
=D

Johnny
Johnny
  • Member since: Apr. 17, 2004
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-02 12:07:40 Reply

At 12/2/09 10:10 AM, UndefinedArt wrote: If you have a better opinion I am all ears.
=D

They'll tell you to code on only the root frames only, or isolate everything into classes... ect... ect... which is actually a more organized way of doing it.

"Better" though, is relative to the person and the project.

If coding inside clips on those frames allows you to finish the game on time and under budget (Or just actually finish it) it's a better option.

If you can manage to separate everything into classes properly without getting bogged down with syntax and scope errors, than that's a better option.


Perpetually looking for time to return to the arts.

BBS Signature
UndefinedArt
UndefinedArt
  • Member since: Aug. 5, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As2: Spawning Mc With Code 2009-12-02 17:28:11 Reply

Well I shall look into all these things. Ty Johnny.