# Random egg

**URL:** <https://forum.kirupa.com/t/random-egg/261110>\
**Category:** Uncategorized\
**Created:** [May 23, 2008, 7:48am UTC](https://forum.kirupa.com/t/random-egg/261110 "2008-05-23T07:48:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahul\_7star](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@rahul\_7star](https://forum.kirupa.com/u/rahul_7star)\
**Post date:** [May 23, 2008, 7:48am UTC](https://forum.kirupa.com/t/random-egg/261110/1 "2008-05-23T07:48:25Z")

</div>

hi  
i am having 15 eggs ovet the top of screen , and they used to fall randomly, when they collide with the ground they disapper after some time  
and again falling of egg form top continues…

How to do it in effective way

---

<div class="post-metadata">

**Author:** ![ajcates](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ajcates/32/2435_2.png) [@ajcates](https://forum.kirupa.com/u/ajcates)\
**Post date:** [May 23, 2008, 4:48pm UTC](https://forum.kirupa.com/t/random-egg/261110/2 "2008-05-23T16:48:22Z")

</div>

I understand your problem, but I don’t understand what an “effective” way would be/

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 23, 2008, 4:52pm UTC](https://forum.kirupa.com/t/random-egg/261110/3 "2008-05-23T16:52:55Z")

</div>

Umm the effective way is to have 15 eggs, and on collision with ground, just make em jump at a random spawn point on the top

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 23, 2008, 5:22pm UTC](https://forum.kirupa.com/t/random-egg/261110/4 "2008-05-23T17:22:06Z")

</div>

Just subtract a random value from the Y attribute when the egg is destroyed. Note that this value must be within a predefined range and must not be smaller than Stage.height. If you need any help with the coding, just let me know, but, judging by your posts here, you know how to do that. 😉

---

<div class="post-metadata">

**Author:** ![rahul\_7star](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@rahul\_7star](https://forum.kirupa.com/u/rahul_7star)\
**Post date:** [May 24, 2008, 7:06am UTC](https://forum.kirupa.com/t/random-egg/261110/5 "2008-05-24T07:06:45Z")

</div>

> [@bpalermo;2330212](#):
>
> Just subtract a random value from the Y attribute when the egg is destroyed. Note that this value must be within a predefined range and must not be smaller than Stage.height. If you need any help with the coding, just let me know, but, judging by your posts here, you know how to do that. 😉

well it will be nice if you can send some code

acutally i want to write a class that is applicable to all eggs and some using inheritance concept …

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 26, 2008, 12:44pm UTC](https://forum.kirupa.com/t/random-egg/261110/6 "2008-05-26T12:44:39Z")

</div>

If you send us the file, I can make it work. 🙂 With comments so you know how to modify it to your specific needs.

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 26, 2008, 7:45pm UTC](https://forum.kirupa.com/t/random-egg/261110/7 "2008-05-26T19:45:20Z")

</div>

try

```auto
var ySpeed:Number;
var myRoot:MovieClip = this;
function ymove() {
 this._y += this.ySpeed;
 if (this.hitTest(ground_mc)) {
  this.removeMovieClip();
  attachEgg();
 }
}
function attachEgg() {
 ySpeed = Math.ceil((Math.random()*4)+2);
 var mc:MovieClip = myRoot.attachMovie("egg", "egg"+myRoot.getNextHighestDepth(), myRoot.getNextHighestDepth());
 mc._y = -60;
 mc._x = Math.floor(Math.random()*(Stage.width-mc._width));
 mc.ySpeed = ySpeed;
 mc.onEnterFrame = ymove;
}
for (i=0; i<15; i++) {
 attachEgg();
}

```

---

<div class="post-metadata">

**Author:** ![rahul\_7star](https://avatars.discourse-cdn.com/v4/letter/r/e36b37/32.png) [@rahul\_7star](https://forum.kirupa.com/u/rahul_7star)\
**Post date:** [May 29, 2008, 4:42pm UTC](https://forum.kirupa.com/t/random-egg/261110/8 "2008-05-29T16:42:44Z")

</div>

[quote=neilmmm;2331376]try

```auto
var ySpeed:Number;
var myRoot:MovieClip = this;
function ymove() {
 this._y += this.ySpeed;
 if (this.hitTest(ground_mc)) {
  this.removeMovieClip();
  attachEgg();
 }
}
function attachEgg() {
 ySpeed = Math.ceil((Math.random()*4)+2);
 var mc:MovieClip = myRoot.attachMovie("egg", "egg"+myRoot.getNextHighestDepth(), myRoot.getNextHighestDepth());
 mc._y = -60;
 mc._x = Math.floor(Math.random()*(Stage.width-mc._width));
 mc.ySpeed = ySpeed;
 mc.onEnterFrame = ymove;
}
for (i=0; i<15; i++) {
 attachEgg();
}

```

[/quote]

Thanks for your Fla…this is what i was looking for …

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 30, 2008, 5:30pm UTC](https://forum.kirupa.com/t/random-egg/261110/9 "2008-05-30T17:30:08Z")

</div>

yw

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 30, 2008, 6:43pm UTC](https://forum.kirupa.com/t/random-egg/261110/10 "2008-05-30T18:43:56Z")

</div>

Just a comment. If you intend to have a variable falling interval, so the eggs don’t always fall the exactly ammount of time after they collide with the ground, set the \_y to a random value below 0, instead of a fixed -60. =)

Something like:

```auto
_y = -(mc._height + Math.floor(Math.random()*range));

```

Where range is the \_y value range.

---

<div class="post-metadata">

**Author:** ![neilmmm](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@neilmmm](https://forum.kirupa.com/u/neilmmm)\
**Post date:** [May 30, 2008, 9:23pm UTC](https://forum.kirupa.com/t/random-egg/261110/11 "2008-05-30T21:23:56Z")

</div>

each egg has a unique ymove variable attached to it … so when they fall they fall at different speeds onEnterframe not on a setInterval

```auto
ySpeed = Math.ceil((Math.random()*4)+2);

```

and

```auto
this._y += this.ySpeed;

```

the this.ySpeed is referencing

this egg’s ySpeed which is different to the next egg’s ySpeed

does this make sense?

---

<div class="post-metadata">

**Author:** ![bpalermo](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bpalermo](https://forum.kirupa.com/u/bpalermo)\
**Post date:** [May 31, 2008, 3:04pm UTC](https://forum.kirupa.com/t/random-egg/261110/12 "2008-05-31T15:04:10Z")

</div>

I wasn’t talking about the speed… The point is, if when they fall and are destroyed their y is set to -60 everytime, the interval between the destruction of the egg and it’s reappearance is always the same, no matter how long it will take to fall from the place it appears to the bottom of the screen (which is what is controlled by the random speed).

In a very optimistic view, they will be slightly different because of the time each egg will take to move those 60 off-screen pixels, which may delay or accelerate it’s actual appearance on screen. But it won’t be too much of a difference anyway.
