- Learn
- Build a Game
- Shatterfield
- 4 Asteroids
4 Asteroids
The Ship flies, shoots and jumps. Now the field arrives. By the end of this chapter, four big rocks tumble across the screen, and a Shot breaks any rock it hits. A Large leaves two Medium, a Medium two Small, and a Small only sparks.
The game as it is at the end of this chapter. Click the game to play.
If your game and this chapter part ways, here is the chapter’s finished project. Open it and compare, or start Chapter 5 from it.
To open it, right-click the downloaded file, choose Extract All... and then Extract. Start Myth Engine, choose File › Open Project... and pick the .mythproj file in the new folder. Beside it, OFL.txt is the license of the project’s font, Press Start 2P.
4.1 The Idea
Four rocks hang near the corners of the dark, each one showing a different face and turned its own way. Then they move. One drifts toward the top edge, one slides past the Ship, and one turns slowly as it crawls along.
Fire at one. The Shot touches it, and the rock cracks open. Sparks spray out, white and cyan and gold, the same as a hyperspace jump. There’s a deep boom, the camera jolts, and two smaller rocks tumble away from where the big one was.
Shoot those, and each one leaves two more. Shoot one of the smallest, and nothing is left but sparks.
That’s three sizes of rock. Does each size need Logic of its own? It doesn’t. Two pieces of Logic run all three. Asteroid Drift sets a rock tumbling, and Break Apart breaks it. What changes from size to size is what each one hands them: how fast it drifts, which sound it breaks with, and what it breaks into.
The three sizes share something else too. Each one wears a Tag, the word Asteroid, the way a row of jars in a pantry might all carry the same handwritten label. Large, Medium and Small wear it alike. So when a Shot asks whether it touched anything tagged Asteroid, any of the three counts.
And the Shot never breaks anything itself. It tells the rock it was hit, with a Custom Event named hit. The rock decides what that means.
4.2 The Plan
Here’s how the pieces work together, read from left to right.
- The game starts -> each Asteroid Large’s Asteroid Drift picks a face, a turn, a spin, a heading and a speed, all at random.
- A Shot touches anything tagged Asteroid -> Shot Impact sends hit to that one Asteroid, and the Shot is gone.
- The message hit arrives -> Break Apart sprays sparks, plays the break sound, shakes the camera for the bigger sizes, makes the fragments and removes the Asteroid.
- A fragment appears -> its own Asteroid Drift sends it off on its own heading.
4.3 Set Up
This chapter needs one picture and three sounds, and they’re all in your project already. The Asteroid Standard sprite is the rock. ExplosionLarge is the deep boom of a big rock, ExplosionSmall a smaller one, and Asteroid Break the dry crack of the smallest.
There are no new keys this time, and PLACE THESE waits until the rocks exist.
4.4 The Rocks
First come the rocks themselves.
Asteroid Large comes first, and the other two sizes are copies of it. Make it a 2D Object, like the Ship and the Shot. Its picture is the Asteroid Standard sprite, a gray rock drawn in thin white lines. Its Collider is a circle with a Radius of 1.5, a little inside the rock’s outline.
Three lines on that card are new. Collision is Sensor, the middle one of the three buttons beside it. A Sensor notices what it touches, and doesn’t push it. So when two rocks drift into each other, they slide right through, without a bump.
Allow Rotation is the second. It’s a switch in the Transform’s Physics section, and it starts off. Turn it on, so the rock is free to spin. Without it, a rock keeps its angle, however hard its Logic tries to turn it.
The third is the Tag. Attach Tags from the Data group of the Component Toolbox. Asteroid is a new Tag, so in its Tag to Add list choose Create New Tag…, then press the + beside the list. Type Asteroid in the Create Tag window. The passage on Object cards has the same steps.
Its Draw Order is 10, the lowest in the game so far. So the Shots and the Ship always pass in front of a rock, never behind it.
X 3.875, Y 3.875OnSensorCircle1.510Asteroid StandardX 3.875, Y 3.875AsteroidAsteroid MediumMake Asteroid Large without its Logic for now. Asteroid Drift and Break Apart don’t exist yet, so leave all three off, even Wrap Around World. You’ll see why before you add it. The Shared Variable at the bottom comes with the Logic too.
Now the copies. In the Resources, right-click Asteroid Large and choose Duplicate. The copy is called Asteroid Large2. Rename it Asteroid Medium, from the same menu or with F2. The copy keeps everything: the Sensor, Allow Rotation and the Tag all came along. Only three lines change. The two Sizes and the Collider’s Radius shrink, because each size is about half as wide as the one before it.
X 2.0625, Y 2.0625OnSensorCircle0.78110Asteroid StandardX 2.0625, Y 2.0625Asteroid220.006ExplosionSmallAsteroid Small2.54.219Asteroid Small is one more copy.
X 1.125, Y 1.125OnSensorCircle0.40610Asteroid StandardX 1.125, Y 1.125Asteroid140Asteroid BreakAsteroid Small03.756.094Now place the rocks, from PLACE THESE. Four Asteroid Large go near the four corners, at X 4 or 28 and Y 4 or 20.
Run the game. Four rocks sit in the dark, perfectly still, all turned the same way. It looks like a field. It isn’t one yet.
4.5 Tumbling
Now the rocks come alive, each in its own way.
4.5.1 Asteroid Drift
When this part is done, no two rocks will look alike. Each one wears its own face, turned its own way, and spins at its own rate while it drifts in its own direction.
All of that happens in a Create Event, the same kind of Event Shot Lifetime starts with. It runs once, at the moment a rock appears. Asteroid Drift makes five random choices there, one after another, and each choice takes two Actions. One picks a number, and the next one uses it.
The first choice is the rock’s face. The Asteroid Standard sprite holds a long row of poses of one rock as it tumbles, each a little further over than the last. Neighbors look almost alike, so a rock uses every fourth one. Generate Random Integer picks a whole number from 0 to 7 and keeps it as poseNumber. Set Frame multiplies it by 4. So the rock wears one of eight faces, and keeps it for good.
The other four choices use Generate Random Number, which picks any number in its range, decimals and all. Two of them turn the rock: its starting angle, and how fast it spins. The angle runs from 0 to 359. Why stop there? One more degree is all the way round, back where it started.
The spin can go either way. Its range runs from -@self.maxSpin to @self.maxSpin, so a minus number turns the rock one way, and a plus number the other.
The last two send it moving: a heading, and a speed somewhere between minSpeed and maxSpeed. Each choice keeps its number under its Action Name. The Action after it reads that name with .value on the end, as in poseNumber.value.
You might expect a rock to drift the way it faces. It doesn’t. Set Rotation turns the picture, and Set Direction aims the motion, and they come from two separate random numbers. So a rock can slide along sideways, or backward, and spin as it goes.
The SHARED VARIABLES hold the speeds. Small rocks drift fastest and Large ones slowest, and every size spins up to 55 degrees a second. On the card, they start at Asteroid Large’s numbers. That’s why Asteroid Large’s own card has no lines for them. Medium and Small each bring their own.
A new Asteroid starts on a random tumble pose and turn, spins, and drifts in a random direction.
Any Object- Action Name
poseNumber- Maximum
7
- Frame
poseNumber.value * 4
- Action Name
turnDegrees- Maximum
359
- Degrees
turnDegrees.value
- Action Name
spinSpeed- Minimum
-@self.maxSpin- Maximum
@self.maxSpin
- Spin
spinSpeed.value
- Action Name
headingDegrees- Maximum
359
- Direction
headingDegrees.value
- Action Name
driftSpeed- Minimum
@self.minSpeed- Maximum
@self.maxSpeed
- Speed
driftSpeed.value
Now build it. Make it the way you made Ship Hyperspace, with Add > Logic Component and Empty Logic Component, and its SHARED VARIABLES first. Each is a Number, Constant, with the card’s Starting Value. The When bar is Create, and no second window opens.
Every Action goes in from a right-click on Mode 1, or on the last block of its line, as in Chapter 3. Each Generate block’s last box, after the word as, is its Action Name, the name the next Action reads. Generate Random Integer arrives with 0 to 100, and Generate Random Number with 0 to 1. Type the card’s values over them. Where the card has no Minimum, the 0 stays.
Now attach Asteroid Drift to Asteroid Large, Asteroid Medium and Asteroid Small, first on each. Then switch back to Components. Under Variables, its three Shared Variables have joined each size, at Asteroid Large’s numbers. Asteroid Large keeps them. On Medium and Small, type minSpeed and maxSpeed from each one’s own card, back in The rocks. maxSpin stays 55 on all three.
Run the game.
The rocks wake up. Each one turns to its own face and drifts away, spinning. Stop the game and run it again, and every rock chooses all over again, from its face to its speed.
Then, one by one, they drift off the edges. They don’t come back. You saw this with the Ship in Chapter 1. The fix is the same one, and it comes in the next part, beside Break Apart.
4.6 Breaking
Now a rock learns to break.
4.6.1 Break Apart
A Shot touches a Large rock. Sparks spray, ExplosionLarge booms, and the camera jolts for 0.18 seconds. Two Medium rocks appear where it was, and the Large one is gone.
Break Apart listens for one message, hit. Here is the chapter’s one hard idea: the same Logic breaks all three sizes, and each size tells it what to leave behind. Take it beat by beat, because the rock does five things.
First, Burst Particles sprays Impact Sparks, sparkCount of them. Chapter 3 promised these sparks would be what’s left when a small rock breaks. Here they are, at every size.
Second, Control Sound plays breakSound. That’s a Shared Variable too, one that holds a sound, so each size can bring its own.
Third, an If asks whether shake is above 0, and when it is, Camera Shake jolts the view for shakeSeconds. A Small rock’s shake is 0. Why ask, if a shake of 0 is no jolt anyway? Because a Camera Shake takes the place of any jolt still running. Without the If, a Small breaking right after a Large would cut the big jolt short. The If leaves the camera alone.
Fourth come the fragments. Two Shared Variables decide them, one letter apart: fragment, the Object to make, and fragments, how many. A Repeat runs its Each time panel fragments times. Inside it, Create Instance makes one fragment, placed relative to Self, the rock that’s breaking. So every fragment starts at the rock’s middle, right on top of the others.
They don’t stay together. Each fragment is a new rock, so its own Asteroid Drift runs the moment it appears. It picks a heading of its own and drifts away.
Last, Destroy Instance removes the rock. It comes last because the fragments take their place from it.
An Asteroid that is hit bursts into sparks, plays its break sound, leaves its fragments and is destroyed.
Any Object- Particles
"Impact Sparks"- Count
@self.sparkCount
- Sound
@self.breakSound
@self.shake > 0- Intensity
@self.shake- Seconds
@self.shakeSeconds
@self.fragmentstimes- Object to Create
@self.fragment- Position Relative To
Self- Created As
newFragment
Now build it, from the top of the card. Make it like Asteroid Drift, with its SHARED VARIABLES first, each one Constant, and four of them Numbers. The fifth, breakSound, is a Sound. In the Type list, Sound is far down, a little past Object. Pick it, and the Starting Value turns into a list of your project’s sounds. Choose ExplosionLarge.
The last one, fragment, holds an Object, the way shot did in Chapter 2. Its Starting Value is a button that reads None. Leave it. Each size fills it in.
The When bar is a Custom Event, and hit is a new one. Type Custom into the Add Event window’s filter, pick Add Custom Event…, and press OK. A second window asks for its Event Name. Type hit. The passage on Events has the same steps.
Steps 1 and 2 go in from Mode 1’s right-click, as in Asteroid Drift. Particles and Sound are lists. Pick Impact Sparks in the first. The Sound list offers @self.breakSound, the Shared Variable you made, so pick it there.
The If works as it did in Chapter 3. Its condition is the one box on the If. Type @self.shake > 0 there. Camera Shake goes on its True line: right-click the If, choose Add inside Then, and join it with Connect to sequence. Type the card’s two values over the numbers it arrives with.
The Repeat goes on the main line, below the If. So add it from a right-click on Mode 1, like steps 1 and 2, and Connect to sequence joins it at the end of that line. Its Count box sits on the block. It starts at 1. Type @self.fragments. Then right-click the Repeat and choose Add inside Repeat, for Create Instance, and join it the same way. If, Else and Repeat shows both kinds of panel.
Create Instance’s Object to Create is a list, and @self.fragment is in it. Position Relative To is a list too, and it starts on World. Pick Self, as you did for Ship Gun. Created As already holds a name of Myth’s. That’s the name a later step would use to reach the new rock, and no step here does. Type newFragment over it anyway, so the block says what it makes and matches its card when you compare.
Destroy Instance comes last. Add it from Mode 1’s right-click again, and join it below the Repeat. Its Applies To should read Self. If it names newFragment, pick Self.
Now attach Break Apart to all three sizes, second, below Asteroid Drift. Switch back to Components. Its six Shared Variables have joined each size’s, under Variables, at the card’s Starting Values, which are Asteroid Large’s numbers. Each size’s card lists only the ones that differ. Match each line by its name. No size’s card lists shakeSeconds, so it stays 0.18 on all three, the same short jolt.
The Value of fragment is a button that reads None. Press it, and pick the Object on the size’s card: Asteroid Medium, on the Large. The Value of breakSound is a list of your sounds. On Medium and Small, pick the card’s.
Look closely at Asteroid Small’s card, back in The rocks, at its fragment and fragments lines. They make an odd pair. Its fragment is Asteroid Small, and its fragments is 0. That’s no mistake, because a Repeat with a Count of 0 does nothing at all. So the name in fragment is never used, and the smallest rock leaves only sparks. Still, the card names one, so don’t leave it at None.
Last on each size comes Wrap Around World, third, below Break Apart. That’s the fix from the last part. Its seamlessEdges arrives Yes, so there’s nothing to set. It goes on each size, beside the other two, so each size’s card shows everything its rocks run.
Run the game. The rocks drift off one edge and come back on the other, and two that meet slide through each other.
Nothing breaks yet, because nothing sends hit. Break Apart is listening. The next part gives it something to hear, from the Shot.
4.7 The Shot Hits
Now the Shot tells the rock.
4.7.1 Shot Impact
From the Shot’s side, there’s very little to it. It touches a rock, says hit, and is gone before the sparks have settled. It never asks which size it hit, or what that size breaks into. That’s the rock’s business.
Its When bar is Collision Enter, with tag:Asteroid as its Object. It runs once, at the moment the Shot first touches a rock, and any size counts, since all three wear the Tag.
Trigger Custom Event on Object sends hit. Its Applies To is other, which means the rock this Shot touched, and no other. Every other rock on the screen goes on drifting. After that, Destroy Instance removes the Shot.
A Shot that touches an Asteroid tells the Asteroid it was hit and disappears.
Any Object- Applies To
other- Custom Event
hit
Make Shot Impact the same way, with Add > Logic Component and Empty Logic Component. It has no Shared Variables. Go straight to its Event, and type Collision Enter into the Add Event window’s filter to find it. No second window opens.
The new Event listens for Any Object. Its block has a Collision section, which starts closed, so click its heading to open it. The list there is Collision Target. Pick Tagged Asteroid, under Tags at the very end of the list, and Myth renames the Event to match the card’s heading. The Components card down the left now lists self.collider. Myth adds it for a collision Event, and the card leaves it out. Leave it there.
In Trigger Custom Event on Object, open the Applies To list, and pick Other, under Event Context. Then type hit into its Custom Event box, with no quotes. Break Apart already listens for that name.
The Shot needs two changes, and its Changes card shows both. Select its Collider and look in its Tags and Interactions section. The Interactions list says Collide Only With, as it has since Chapter 2, with nothing on it yet. Press Search to add… under it. Pick Tagged Asteroid in the window that opens. Without it, the Shot can’t touch a rock at all, and Collision Enter never runs. Then attach Shot Impact, fourth.
X 0.8125, Y 0.8125Circle0.156Collide Only WithTagged Asteroid15Shot SpriteX 0.8125, Y 0.8125-90The Ship gets Tagged Asteroid on its list too, the same way, as Chapter 1 promised. Nothing listens for that touch yet. Let a rock drift into the Ship, and it slides by underneath. Chapter 5 gives that touch a meaning.
X 2.4375, Y 3.0625Circle0.594Collide Only WithTagged Asteroid20Ship SpriteX 2.4375, Y 3.062513.7529013.1251.719ShotExpression$areaWidth0Expression$areaHeightRun the game, and fire at a rock.
It cracks open, and two Medium rocks tumble out of the sparks. Chase one down, then a Small. The last one leaves nothing behind but light.
Watch for each of these, one rock at a time if you like.
- Four Large rocks start near the corners. Each turns to its own face and angle, spins, and drifts its own way.
- Run the game again. Every rock chooses again, so no two games start the same.
- Rocks that meet slide through each other. A rock that leaves one edge comes back at the other.
- Shoot a Large rock. Sparks spray, ExplosionLarge booms, the camera jolts, and two Medium rocks drift apart.
- Shoot a Medium rock. ExplosionSmall, a smaller jolt, and two Small rocks.
- Shoot a Small rock. The Asteroid Break sound, sparks, no jolt, and nothing left.
- No points rise off a broken rock. Nothing keeps a score yet. Chapter 7 brings it, and the points come with it.
- The Shot that hits is gone, and the rocks around it don’t break.
- Fly into a rock. Nothing happens, yet.
Asteroid Drift sets each new rock tumbling. Shot Impact tells a rock it was hit. Break Apart sprays sparks, plays a sound, and leaves smaller rocks behind. Each smaller rock runs Asteroid Drift for itself.
Underneath them is one idea. The three sizes run the same two pieces of Logic, and the Object card makes the difference. A Large and a Small do the very same Actions. Their Shared Variables make one slow, with a deep boom, and the other quick, with a dry crack. The fragment Variable turns a Large into Mediums. And the Shot only ever says hit, so the rock decides what a hit means.
4.8 If It Didn’t Work
Something may be a little off the first time, and that’s normal. Run the game, watch what the rocks do, and take it one piece at a time.
The rocks sit still? Perhaps Asteroid Drift isn’t attached. Open each size, switch the list to Logic, and look for it first in the Attached Logic. Still not moving? Look on Asteroid Drift’s canvas for a block standing apart from the line, with no arrow into it. Right-click it and choose Connect to sequence.
Every rock wears the same face? Look at Set Frame in Asteroid Drift, where the Frame should read poseNumber.value * 4.
The rocks drift, but never spin? Allow Rotation is off, in the Transform’s Physics section. If the switch was missed on Asteroid Large, the copies missed it too. Turn it on in each size.
The rocks drift off the edges and never come back? Look for Wrap Around World in each size’s Attached Logic, third, below Break Apart.
Two rocks that meet push each other aside, instead of sliding through? Their Collision is still Solid. Pick Sensor, the middle of the three buttons, on each size.
Your Shots fly straight through the rocks? The Shot’s Collide Only With list needs Tagged Asteroid on it. If it’s there, look at the rocks: each size needs its Tags, with Asteroid listed. Then look for Shot Impact in the Shot’s Attached Logic, and check that its Collision Target says Tagged Asteroid.
A Shot touches a rock and vanishes, and nothing breaks? Perhaps Trigger Custom Event on Object still has Self in its Applies To, so hit goes to the Shot. Pick Other.
Shoot one rock, and every rock breaks? Shot Impact may be using Trigger Custom Event, which tells every rock at once. It needs Trigger Custom Event on Object. Its Applies To should be Other.
The fragments always start out at a corner of the screen, not where the rock was? Create Instance’s Position Relative To is still World. Pick Self.
A rock breaks, but nothing comes out of it, or only one rock? Look at the size’s fragment Value first, which should name the next size down, not None. Then look at Break Apart’s Repeat. If it, or its Create Instance, stands apart with no arrow into it, join it with Connect to sequence. And its Count should read @self.fragments, not the 1 it started with.
The Repeat hangs under Camera Shake, on the If’s True line, not below the If? Right-click the Repeat on its name and choose Delete. Then add it again from Mode 1’s right-click, and its Create Instance with Add inside Repeat.
A rock sprays its sparks and stays put? Look for Destroy Instance at the end of Break Apart’s line, joined. A Shot that flies on after a hit needs the same, at the end of Shot Impact.
A Large rock breaks into more Large rocks? Asteroid Large’s fragment should be Asteroid Medium. Each size breaks into the next size down, and only Asteroid Small names itself.
A rock breaks without a sound? Open that size and look at its breakSound Value, under Variables. It should name the sound on its card, where None means silence. And in Break Apart, Control Sound’s list should read @self.breakSound, the Shared Variable, not a sound of its own.
Each of these is a small fix. Make it, run the game again, and you’re back with the chapter. The catch-up download in the Goal is this chapter, finished, if you’d like to compare.
The field is yours to shape. Open Asteroid Large, and type 3 into the Value of fragments, under Variables. Every big rock leaves three behind. Raise the Small rocks’ maxSpeed, and the last pieces of the field dart about like sparks. Or end the line early: give Asteroid Medium a fragments of 0, and a Medium rock breaks into nothing. Chapter 5 starts from the cards’ numbers, so put them back when you’re done. Jot yours down first if you want them later.
Still stuck? Search the manual, or ask in the Myth Engine Discord (opens a new tab).
Rocks that tumble, and a gun that breaks them down to sparks. In Chapter 5, the rocks can hurt you.
