Learning ActionScript 3.0: Chapter 7, Motion
Pages: 1, 2, 3, 4, 5, 6, 7, 8
Lines 29 through 31 assign the particle's color by way of the ColorTransform class. The first step in this process is to store the particle's default colorTransform information (which allows manipulation of the red, blue, green, and alpha channels of the color), retrieved from the particle's transform object. Next, line 30 changes the color property of the colorTransform to the new color passed into the col argument.
However, that property prefers a uint data type (non-negative whole number) and the initial random color selection made when creating the particle converted the color to a Number data type. Therefore, it's a good idea to cast the number back to uint with the uint() data casting method. Finally, once the color has been changed, update the particle's own colorTransform object using the one in the variable you've been manipulating.
The last line of the constructor adds an enter frame listener to control the particle's movement. It triggers the onRun() function, which follows. This function uses the techniques discussed in the velocity and gravity examples of this chapter, but adds one thing. A conditional determines whether the next particle position is off the stage on the left, top, right, or bottom edge. If so, the event listener is removed and the particle is removed from the display list, ready for garbage collection.
35 private function onRun(evt:Event):void {
36 _yvel += _grav;
37 _xpos += _xvel;
38 _ypos += _yvel;
39 x = _xpos;
40 y = _ypos;
41
42 if (xpos < 0 || ypos < 0 || _xpos >
stage.stageWidth || ypos > stage.stageHeight) {
43 removeEventListener(Event.ENTER_FRAME, onRun);
44 parent.removeChild(this);
45 }
46 }
47 }
48 }
Particle systems are a lot of fun and can lead to many fruitful experiments. Run this system several times, modifying the values sent to the Particle class. Change the x and y velocities for a larger spread of particles, decrease the force of gravity to see what particle life is like on the moon, or even set the emitter location to the mouse location to move the system around.
Try to move some of the hard-coded properties, like alpha, scaleX, and scaleY into the argument list so they can be varied, too. As an example, we've created another version of this system for the book's companion web site that includes several new properties, including filter and blend mode settings that you'll learn about in the next chapter.
This excerpt is from Learning ActionScript 3.0. Learning ActionScript 3.0 gives you a solid foundation in the Flash language and demonstrates how you can use it for practical, everyday projects. The book does more than give you a handful of sample scripts, defining how ActionScript and Flash work. It gives you a clear look into essential topics such as logic, event handling, displaying content, migrating legacy projects to ActionScript 3.0, classes, and much more. Written for those new to the language, this book doesn't rely exclusively on prior knowledge of object-oriented programming (OOP). Instead, it helps you expand your skillset by first focusing on clear, concise examples in the timeline, evolving into OOP examples over time-allowing you to choose the programming approach with which you are most comfortable.
The project package for this chapter includes several of the basic formulas covered herein, including the degree-to-radian and radian-to-degree conversion, Zeno's paradox, Hooke's law, and more. These formulas apply to the project but can also be used in your own work.
While this chapter details a variety of ActionScript animation techniques, it only begins to cover the subject of motion through code. However, the basic building blocks are here, and it's with these concepts (and related skills that grow from the ideas herein) that greater art and industry can be achieved.
Next on the to-do list is the ability to partially free yourself from the constraints of the Flash interface and approach code-only projects with a little more latitude. When working with visual assets, we've so far relied on symbols created within Flash and stored in the library of a SWF. We will continue to do that any time complex art warrants this practice, but we'll also begin to work with vectors and bitmaps created with code. In addition to giving you more freedom, this approach can also reduce file size and make your SWFs load faster.
In the next chapter, we'll discuss:
Skip to any available Digital Media Help Center chapter of Learning ActionScript 3.0: |