text stringlengths 0 897 |
|---|
if self.timeTillBurst <= 0 then |
// remove the rocket particle and its trail |
self.die |
// create the burst particles |
c = burstColors.any // (.any provided by listUtil) |
for i in range(1,40) |
p = new Ember |
p.startColor = c |
p.x = self.x |
p.y = self.y |
angle = 2 * pi * rnd |
speed = 10 + 50*rnd |
p.vx = speed * cos(angle) |
p.vy = speed * sin(angle) |
particles.push p |
end for |
// flash and bang |
flashGfx.fillEllipse self.x-10, self.y-10, 20, 20 |
globals.haveFlash = true |
burstSnd.play 1, panForX(self.x), 0.9 + 0.2*rnd |
end if |
end function |
Ember = new Particle |
p = new Rocket |
p.vx = 30 |
p.vy = 90 |
particles.push p |
for i in range(100) |
for p in particles |
p.update |
end for |
yield |
end for |
``` |
{i:"`super`"} |
Notice the `Rocket.update` function, which overrides the `Particle.update` function in the superclass. But the first thing that this function does is call `super.update`, which means that it runs the standard `Particle.update` code. |
D> `super` is a special MiniScript keyword that finds the identifier after the dot somewhere higher in the chain of classes related by `new`. It's commonly used in a subclass method to call the same method in the superclass. |
It then goes on to figure out if it's time to burst. When it is, it calls its own `die` method, then creates a bunch of burst particles (using the `Ember` class we trivially defined in the test code on line 112), as well as drawing a flash (white circle). |
The test code here defines `Ember` as a `new Particle`, but does not override any properties or methods. That's a pointless thing to do, *except* in a case like this, where we know we're going to need an ember class later, but for now are content with standard particle behavior. The test code then creates and launche... |
Note that there is nothing here yet to erase the flash on the `flashGfx` display. So once the rocket bursts, it leaves a yellow circle behind. That's OK — we'll fix that later. |
Moving on, clear out that test code starting on line 112, and let's create that Ember class for real. |
{caption: "Listing 5 (Fireworks `Ember` class).", number-from: 112} |
```miniscript |
// make another Particle subclass that represents the |
// embers that float down after the rocket bursts |
Ember = new Particle |
Ember.age = 0 |
Ember.gravity = -5 |
Ember.dragFactor = 0.90 |
Ember.startColor = color.pink |
Ember.endColor = "#88000088" // translucent red |
Ember.update = function(dt=0.1) |
// age, adjust our color, and when it's time, die |
self.age = self.age + dt |
if self.age > 3 then |
self.die |
else |
// update our color |
self.color = color.lerp(self.startColor, self.endColor, self.age/3) |
// and then do standard particle update |
super.update dt |
end if |
end function |
p = new Ember |
p.x = 120 |
p.y = 200 |
p.vx = 30 |
for i in range(50) |
p.update |
yield |
end for |
``` |
As usual, we include a bit of test code at the bottom so you can make sure it's working. When you run this, a little pink ember should appear near the top of the screen, fly off to the right, and fade out. That's what makes embers different from other particles: they change colors over time. Notice that the call to ... |
Now we're almost done! We need a function to launch a rocket, and a main loop. Since any test code for the launch function would be practically the full main loop anyway, let's just press on and finish the program. Delete the test code starting on line 134, and wrap it up with Listing 6. |
{caption: "Listing 6 (Fireworks launch method and main loop).", number-from: 134} |
```miniscript |
// function to create and launch a new firework |
launchAt = function(x) |
p = new Rocket |
p.x = x / gfx.scale |
p.y = 0 |
p.vx = 30 * rnd - 15 |
p.vy = 10 * rnd + 80 |
p.timeTillBurst = 3 + 3 * rnd |
particles.push p |
launchSnd.play 0.3, panForX(p.x), 0.9 + 0.2*rnd |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.