# VG:Spawning a Sprite upon another Sprite's Deletion

**Student1:** Hey, I just was wondering how I should write it so that if I blew up an asteroid, it would spawn two smaller asteroids inside and fling them in random directions. I have already created animations for the asteroids and are about to add them, so while I did that, I was hoping someone would know how to do that so I don't pull my hair out figuring it out. Thanks.

**Student2:** use the spawn function from Smash but insted of a random postition use "`this.FrameSize.Width/2 + this.Position.X`

" and then the same for `y`

. then add some velocity with random `x`

and `y`

values from 1 to -1 then multiply both by some speed.

**Instructor:** Make sure to use This, since the lowercase is a different thing entirely.

Also, I think what you want is to make a copy of a smaller sprite when a larger one is destroyed. So we have `BigAstroid`

, `MedAstroid`

, and `SmallAstroid`

sprites that are unused so that we can copy them. Then, we can write a collision function for each that handles what happens when we get hit by a bullet.

Each will look very similar. Here's an algorithm for it, just remember not to copy asteroids when we're at our smallest.

// If we are hit by a bullet // Copy our smaller asteroids // Place them on our position // Give each a random float for x and y velocity, multiplied by some speed // Make them used // Explode this asteroid

Alternatively, we could make use of sprite scaling...

**Student1:** Thanks for outlining all my comments to fill code in on for me, it was really helpful. `:-D`

Another dumb question, does anyone have a script for making things spawn just off screen? it's so my asteroids start off the screen and then initially head towards me, with a random angle offset of ± 35 degrees. I think I have the follow thing down, but I can't figure out how to make them start off-screen. I also probably need to have it so if they are off-screen after being alive for x amount of seconds, they delete. this is because it is a view port follow free movement asteroids game. if anyone has something to help me fix this, I'd be very grateful.

**Instructor:** Basically, we can just spawn your asteroids on the edge of a circle centered on the viewport's center with a radius of half the viewport's width (since our game is wider than it is tall).

The center of the viewport is at the viewport's position, plus half of it's size. We have to do this as an x and y variable since the size is not a `Point2D`

.

Then we need a random angle (between 0 and 360 for the full circle).

Now we need to do some math. We've been using our trigonometry to solve for x and y values based on an angle, so we want to do that here. Remember, `cos( angle )`

will get us x, and sin( angle ) will get us y.

Putting it all together, we get something like this:

xPos = ( [ViewportPosition.X] + [ViewportWidth / 2] ) + [ViewportWidth / 2] * Cos( angle );

So that's what we could do for the position along the x axis. I'll leave it to you to figure out the y axis.