1. Code
  2. JavaScript

Quick Tip: Create a Minimalistic SandClock Using ActionScript 3

Scroll to top
3 min read

Read through the easy steps in this Quick Tip to create a Minimalistic SandClock with ActionScript.


Final Result Preview

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


Step 1: Brief Overview

Using Arrays and a premade square MovieClip we will create a SandClock that will be animated by a Timer.


Step 2: Set Up Your Flash File

Launch Flash and create a new Flash Document, set the stage size to 400x250px and the frame rate to 24fps.


Step 3: Interface

This is the interface we'll be using, the squares in the image are actually one single blue square MovieClip exported for use with ActionScript, with a linkage name of Square. A simple button named startButton will be used to build and start the clock.


Step 4: ActionScript

Create a new ActionScript Class (Cmd+N), save the file as Main.as and write the following lines. Read through the comments in the code in order to fully understand the class behavior.

1
2
package 
3
{
4
	import flash.display.Sprite;
5
	import flash.utils.Timer;
6
	import flash.events.TimerEvent;
7
	import flash.events.MouseEvent;
8
9
	public class Main extends Sprite
10
	{
11
		private var clockArray:Array = [15,13,11,9,7,3,1];//Stores the number of squares per line

12
		private var top:Array = []; //will hold the squares on the top part of the sandclock

13
		private var container:Sprite = new Sprite(); //stores all the movieclips

14
		private var containerCopy:Sprite = new Sprite(); //will duplicate the top part to make the bottom

15
		private var timer:Timer = new Timer(1000); //a timer executed every second

16
		private var counter:int = 0; //will count the seconds, used to stop the timer

17
18
		public function Main():void
19
		{
20
			startButton.addEventListener(MouseEvent.MOUSE_UP, buildClock);//a listener in the start button

21
		}
22
23
		private function buildClock(e:MouseEvent):void
24
		{
25
			startButton.removeEventListener(MouseEvent.MOUSE_UP, buildClock); //disables the button

26
			startButton.enabled = false;
27
			
28
			var clockLength:int = clockArray.length;
29
30
			/* this double for browses through the clock array length AND the value of each array element

31
				creating 7 lines (length) of squares with 15, 13, 11(element value) and so on  */
32
33
			for (var i:int = 0; i < clockLength; i++)
34
			{
35
				for (var j:int = 0; j < clockArray[i]; j++)
36
				{
37
					var s:Square = new Square();
38
					var sc:Square = new Square();
39
40
					s.x = 70.5 + (s.width * j) + (1 * j) + (i * (s.width + 1));
41
					s.y = 84.5 + (s.height + 1) * i;
42
					sc.x = s.x;
43
					sc.y = s.y;
44
45
					if (i >= 5)
46
					{
47
						s.x = 70.5 + (s.width * j) + (1 * j) + (i * ((s.width) + 1)) + (s.width * 2 - 4);
48
						sc.x = s.x;
49
					}
50
51
					container.addChild(s);
52
					containerCopy.addChild(sc); //creates a copy for the bottom part

53
54
					top.push(s);
55
					sc.alpha = 0.2; //makes the bottom part semi transparent

56
				}
57
58
				addChild(container);
59
60
				containerCopy.x = 225; //positions and rotates the bottom part

61
				containerCopy.y = 247;
62
				containerCopy.rotation = 180;
63
64
				addChild(containerCopy);
65
			}
66
67
			timer.addEventListener(TimerEvent.TIMER, startClock); //start the timer

68
			timer.start();
69
		}
70
71
		/* this function executes every second, it changes the alpha of the corresponding square to make the sand effect.

72
			when the time is done stops the timer and calls a function */
73
74
		private function startClock(e:TimerEvent):void
75
		{
76
			container.getChildAt(counter).alpha = 0.2;
77
			containerCopy.getChildAt(counter).alpha = 1;
78
			counter++;
79
80
			//60 seconds

81
82
			if (counter >= 59)
83
			{
84
				timer.stop();
85
				timer.removeEventListener(TimerEvent.TIMER, startClock);
86
				timeComplete();
87
			}
88
		}
89
90
		private function timeComplete():void
91
		{
92
			//do something here

93
		}
94
	}
95
}

You can adjust the Timer and the counter value to make the sandclock duration greater or shorter.


Step 5: Document Class

Remember to add the class name to the Class field in the Publish section of the Properties panel.


Conclusion

Why not use this SandClock to give your application or game a nice touch?

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