Object spawning [Flash]

Started by: Steyene | Replies: 7 | Views: 774

Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 21, 2008 6:51 AM #229011
Alright, I have a square spawning at random x and y co-ords. But every time a new square spawns the old one disappears.

Any ideas?

Here is the code if it helps at all

onClipEvent(load){
timer = 1;
timer_to = random(15)+2;
depth = random(20);
rndx = random(600);
rndy = random(200);
square_num = 0;
}
onClipEvent(enterFrame){
if(timer <> timer_to){
timer ++;
}
else if(timer == timer_to){
square_num ++;
_root.square_base.duplicateMovieClip("square"+square_num, depth);
_root["square"+square_num]._x = rndx;
_root["square"+square_num]._y = rndy;
timer = 1;
timer_to = random(15)+2;
rndx = random(600);
rndy = random(200);
square_num ++;
}
}

darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Aug 21, 2008 7:04 AM #229018
You're using the same depth. Only one element can exist at any given depth at a time, so it deletes the old one.

You can use _root.getNextHighestDepth() or create an incrementing variable to fix it.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 21, 2008 7:13 AM #229021
lawl the one variable that wasn't being renewed each time >_>
Thanks :D more questions coming later.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 21, 2008 7:55 AM #229048
Okay how do you make a hittest on the extremity of an object. I.e. when it has just touched it?
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Aug 21, 2008 8:04 AM #229055
Quote from Steyene
Okay how do you make a hittest on the extremity of an object. I.e. when it has just touched it?


Well, animation is really a series of discreet jumps, so there will not always be a point within your program when object A is touching object B without entering it. Most of time it will jump from not touching it, to intersecting it.

So, this leaves the boundless world of the theoretical: math! You'll have to calculate vectors and determine the exact points in "time" they intersect, and then rearrange the objects accordingly.

I could give you a little more help if you gave some more info on what you're trying to do.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 21, 2008 10:09 PM #229770
Pretty much ripping off your sig.
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Aug 21, 2008 10:15 PM #229782
Quote from Steyene
Pretty much ripping off your sig.


Hah, awesome.

Well, if you just want something like that, you don't need it to be exact. You can use a normal hitTest(), but that may place them a bit closer before they fight than you'd like.

For mine, since I could assume they would always overlap as far as _y's are concerned, I found the distance between their two _x coordinates, and if it was below a certain value, initiated the attack.

I set the minimum distance to a variable, so I could make it so those with longer attack ranges would stand back.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 21, 2008 10:39 PM #229828
Yup pretty much that is what I am doing. Taking the aboslute value between one object and another, and if it is less then say 10 do something.