-
I would like to have a MC that spawns (duplicateMovieClip) x number of instances of itself, names these instances unique names (like stat1, stat2, stat3, etc), and have all these instances run rampant within a boundary, always testing for collision. Upon collision, I would like to script them to bounce away from each other like billiard balls. I think I have the random movement, boundary, and basic hitTest ideas down, but i need help with the spawning, adding new names, and checking for multiple collisions constantly. Can someone please lend a hand? Many thanks.
-
you could have 'name of variable' = 1
duplicate "clipname"+'name of variable', level 'name of variable'
and then increase 'name of variable' by one.
and each clip would then be on a different level and have the different names
then have a
if (new var < _level0.'name of variable'){
new var ++}{else (new var=0}
if this.hitTest("_level0.clipname"+'new var')
trace("hit")
this way it willl cycle through the names of all the clips on stage
new var
don't cut and paste this because i wrote it in this window not in flash so you may have to check the syntax.
but it works because i had to make a invaders type game that does the same.
hope this helps, if this is what you mean and it does'nt i will give you more indepth help of you like.
cheers howard
-
howard54321
thanks for your reply.
i'm not sure i understand exactly how this might work. isn't having the name "new var" problematic in that it has the word "new" and "var" in it? Where is new var initiated? DO I use an onMovieClipEvent (load) {new var = 0;}? if you could provide a short fla i'd be very appreciative. Thanks a milion.
-
pheck
I'm not sure how many "spawnings" you want, but be warned that FLASH is not great at multiple collision testing when the numbers are high - even worse if you want to have them "bouncing" off each other correctly - if it's not a modelling situation I'd advise you to just "bounce" randomly, but still things will slow down dramatically with increased numbers.
Have a look at a model I made to illustrate/prove to myself.
Yes I know there are some bugs, but it doesn't affect the fact that FLASH can't cope with large numbers of moving/colliding mc's
swills
[Edited by swills on 03-06-2002 at 03:19 PM]
-
hey swills, this is exactly what i'm trying to do (minus the bugs of course). Would you mind allowing me to see your fla/code? I can try to work the bugs out of it for you and send it back.
Thanks.
-
I'm fully aware of the bugs and how to rectify them, thankyou.
As to this being "exactly" what you are trying to do....well you don't have to cos I've done it!
You have to let me have some fun out of this, which bit,"exactly", did you want to know about, and what have you got so far?
As I've said FLASH is not very good at "proper" mechanics bouncing - how many "things" did you want to spawn?
swills
-
hey swills,
thanks again for your help on this. Have all the fun you want :D
All i have right now are three MC's bouncing around in a bounded box rotating, and when they collide they stop moving even tho i this is not what i've tried to code (obviously). So I guess what 'exactly' it is i want to do is modify your code slightly to make it so i can specify in the initializing actionscript frame how many MC's i want to spawn (i.e. "spawnNum=25;") and for it to place them randomly within the box somewhere and move around, testing for collision.
this is much less for practical purposes than for my own intellectual interest. i realize it will be really slow, but I just want to learn, you know?
Sorry it took me a while to respond. For some reason, FK only gives me notice of some replies to threads, and misses a lot of replies. so i had to go to page 8 of this forum (or whatever page number) to find this thread. Thanks again!
-
figured it out
ah hah!
got it. trick i used was to set up an array.
and it randomly deflects if collison, as one of you suggested.
http://www.stanford.edu/~pme/water.swf
Here's the code i used for the MC if you're interested:
onClipEvent (load) {
ydirection=5;
xdirection=5;
bottom = 200;
left = 0;
right = 300;
_root.array.push(this);
}
onClipEvent (enterFrame) {
for(j=0; j<_root.array.length; j++) {
if(this <> _root.array[j]) {
if(this.hitTest(_root.array[j])) {
k = random(2);
l = random(2);
if(k == 0)
xdirection = -5;
else
xdirection = 5;
if(l == 0)
ydirection = -5;
else
ydirection = 5;
}
}
}
if(this._x >= right) {
xdirection = -5;
}
if(this._y >= bottom) {
ydirection = -5;
}
if(this._x <= left) {
xdirection = 5;
}
if(this.hitTest(_root.water)) {
ydirection = 5;
}
this._x=this._x + xdirection;
this._y=this._y + ydirection;
this._rotation += 30;
}
-
Well done, it looks nice, and I got no fun out of it at all!
The collisions are much better done as random in your case.
Many ways of doing these types of things, not quite sure why you used an array. You could've also used:
Code:
for(i=1;i<10;i++){
duplicateMovieClip("water", "water" + i, i);
_root.["water" + i].number = i;
}
to duplicate the mc's, then:
Code:
for(i=1;i<10;i++){
if(i!=this.number){
if(this.hitTest("water" + i)){
//do something about it
}}}
on the mc to be duplicated.
Makes no difference, whatever works, works!
swills