Advertisement
  1. Code
  2. Animation
Code

Create a Colorful Spinning Wheel in Flash With AS3

by
Difficulty:BeginnerLength:MediumLanguages:

In this tutorial you'll learn how to create a spinning wheel using Flash and AS3, with an interface that's suitable for both mouse- and touch-based devices.


Final Result Preview

Let's take a look at the final result we will be working towards:

Click and drag your mouse vertically to spin the wheel; the longer the line you drag, the faster the wheel will spin! Once it stops, the colored bar at the bottom will display the color the wheel landed on.


Step 1: Brief Overview

Using pre-made graphic elements we'll create a colorful interface that will be powered by several ActionScript 3 classes.

The user will be able to spin the wheel using a dragging gesture represented by a line on the screen; a taller line will make a faster spin.


Step 2: Flash Document Settings

Open Flash and create a 500x300px document. Set the frame rate to 24fps.


Step 3: Interface

A colorful nice looking interface will be displayed, made up of multiple shapes, MovieClips and more.
The simple shapes were created using the Flash Pro drawing tools, and since they're easy to duplicate I won't explain their creation. Make sure the wheel's rotation point is in the center.

You can always look at the FLA in the source download files.


Step 4: Instance Names

The image above shows the Instance Names of the various MovieClips. Pay special attention to the wheel.p MovieClips; these are the little black lines that divide the colors in the wheel, and are inside the wheel MovieClip. They are named p1 to p10, going clockwise.


Step 5: TweenMax

We'll use a different tween engine than the default one included in Flash; this will make the color transition of the colorMC symbol a lot easier.

You can download TweenMax from the Greensock website.


Step 6: Set Main Class

Add the class name, Main, to the Class field in the Publish section of the Properties panel to associate the FLA with the Main document class.


Step 7: Create a new ActionScript Class

Create a new (Cmd + N) ActionScript 3.0 Class and save it as Main.as in your class folder.


Step 8: Class Structure

Create your basic class structure to begin writing your code.


Step 9: Required Classes

These are the classes we'll need to import for our class to work. The import directive makes externally defined classes and packages available to your code.


Step 10: Variables

These are the variables we'll use; read the comments in the code to know more about them:


Step 11: Constructor

The constructor is a function that runs when an object is created from a class, and is the first to execute when you make an instance of an object. Since this is our document class, it'll run as soon as the SWF loads.


Step 12: Paddles Vector

First we add the various paddle MovieClips to the vector, and add the listeners - we'll write the listeners() function next.


Step 13: Listeners

This function will add or remove the listeners according to the parameter. Mouse Listeners are set to draw the line that will control the wheel.


Step 14: Movement Line

The next function starts to create a line based on the current mouse position, and places it on the stage. It's triggered when the mouse is clicked.


Step 15: Draw Line

While the mouse is moved, the line continues in that direction.


Step 16: Spin the Wheel

The next code runs when the mouse button is released, finishing the line. The drawing listeners are removed to avoid drawing multiple lines and the speed is calculated according to the height of the line. Finally, an EnterFrame event is called to actually rotate the wheel.


Step 17: Rotate the Wheel

This is the function that will spin the wheel and detect what value it lands on:


Step 18: Detect Value

Here we detect the current value of the wheel based on the last paddle it touched.


Step 19: Decrease Speed

The wheel's speed is reduced every frame to eventually stop the spinning.


Step 20: Reset Wheel

All values are reset when the wheel stops. A function that will run an action according to the final value is called.


Step 21: Set Bar Color

This function will run a custom action according to the last value of the wheel. In this case it changes the color of the bottom bar, but you could make it do anything else.


Conclusion

Change the code to perform your own actions!

I hope you liked this tutorial, thank you for reading!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.