Quick Tip: Simulate an Android Lock Screen with ActionScript

Quick Tip: Simulate an Android Lock Screen with ActionScript

Tutorial Details
  • Difficulty: Beginner
  • Program: Flash
  • Estimated Completion Time: 15 mins

The Lock Screen is a part of an operating system, mostly used in mobile devices, that prevents accidental input. This Quick Tip will show you how to simulate an Android Lock Screen with ActionScript. Let’s get going!


Final Result Preview

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

This is a very simple version of the Android pattern-lock screen. Drag your mouse along the dots in the path indicated by the semi-transparent white lines in order to “unlock” the SWF. Naturally, the white lines are only there for demonstration purposes!


Step 1: Brief Overview

We’ll make use of Mouse Events and Arrays to store and compare the user input and the correct pattern. A new Screen will be shown when the correct pattern is entered.


Step 2: Set Up Your Flash File

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


Step 3: Interface

This is the interface we’ll be using, the white dots in the image are MovieClips named from left to right one, two, three… and so on. Semi-transparent white lines are used to indicate the correct password (you may want to remove this for real usage).


Step 4: ActionScript

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

Change the values in the pass array in order to change the unlocking pattern.

package 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;

	public class Main extends Sprite
	{
		private var dots:Array = []; // Stores the in stage movieclips
		private var pattern:Array = []; //The pattern entered by the user
		private var pass:Array = [1,2,3,6,9,8,5]; //The correct pattern to proceed

		public function Main():void
		{
			dots = [one,two,three,four,five,six,seven,eight,nine]; //add the clips in stage
			addListeners();
		}

		private function addListeners():void //adds the listeners to each dot
		{
			var dotsLength:int = dots.length;

			for (var i:int = 0; i < dotsLength; i++)
			{
				dots[i].addEventListener(MouseEvent.MOUSE_DOWN, initiatePattern);
				dots[i].addEventListener(MouseEvent.MOUSE_UP, stopPattern);
			}
		}

		/* Adds a mouse over listener and uses it to add the number of the dot to the pattern */
		
		private function initiatePattern(e:MouseEvent):void
		{
			var dotsLength:int = dots.length;

			for (var i:int = 0; i < dotsLength; i++)
			{
				dots[i].addEventListener(MouseEvent.MOUSE_OVER, addPattern);
			}

			pattern.push(dots.indexOf(e.target) + 1); //adds the array index number of the clip plus one, because arrays are 0 based
		}

		private function addPattern(e:MouseEvent):void
		{
			pattern.push(dots.indexOf(e.target) + 1); //adds the pattern on mouse over
		}

		private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
		{
			var dotsLength:int = dots.length;

			for (var i:int = 0; i < dotsLength; i++)
			{
				dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
			}

			checkPattern();
		}

		private function checkPattern():void //compares the patterns
		{
			var pLength:int = pass.length;
			var correct:int = 0;

			for (var i:int = 0; i < pLength; i++) //compares each number entered in the user array to the pass array
			{
				if (pass[i] == pattern[i])
				{
					correct++;
				}
			}

			if (correct == pLength) //if the arrays match
			{
				var sView:SecondView = new SecondView(); //add a new view

				addChild(sView);

				var tween:Tween = new Tween(sView,"x",Strong.easeOut,320,0,0.8,true);
			}

			pattern = []; //clears the user array
		}
	}
}

Step 5: Document Class

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


Conclusion

You’ve created a useful Lock Screen for your applications or even a website. You can adapt the project to meet your needs or use this technique to build your custom LockScreen. How about using the graphics object of a Sprite to draw lines that follow your mouse’s path?

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

Editor’s Note: Can’t get enough of Android? Check out the latest Envato™ site: Android.AppStorm.net!

Tags: Tips
  • Brett

    Wow, nice job. It’s nice to see something useful accomplished with the basics. If it were me, I would have had the lines that connect the dots, but those are probably easy enough.

  • Adam

    If you do the correct pattern but then go over it, say 1,2,3,6,9,8,5,4,7, it still passes. Maybe checkPattern() could iterate over ‘pattern’ instead of ‘pass’?

    • http://michaeljameswilliams.com/ Michael Williams

      Nice addition, Adam!

    • David

      I think that changing this line in checkPattern() would do the trick:

      var pLength:int = pass.length
      to:
      var pLength:int = pattern.length

      • http://michaeljameswilliams.com/ Michael Williams

        What if the user only clicks dot #1?

  • Brett

    Correct me if I’m wrong, but it seems to me that if they entered it in the correct order, the arrays should be identical. If so, could you not just check to see whether or not they are equal to each other?

    • http://michaeljameswilliams.com/ Michael Williams

      Good question! Unfortunately, == doesn’t work like that. To quote the LiveDocs:

      “Variables representing objects, arrays, and functions are compared by reference. Two such variables are equal if they refer to the same object, array, or function. Two separate arrays are never considered equal, even if they have the same number of elements.”

      This means you have to check whether all the elements of the arrays match, rather than whether the arrays are equal.

  • Pingback: Flash Tutorial: How to Simulate an Android Lock Screen with ActionScript | Adobe Flash Lite

  • Ankit

    Could you please provide CS4 version source file. Since its made is CS5 I’m unable to open the source.

    Thank you, :)

  • http://davidpuerto.com David Puerto

    This is entirely new to me, I’m using CS4 as well, but I did everything per the tutorial. Made and linked the class file and named my movieClips and received this error:

    1046: Type was not found or was not a compile-time constant: SecondView.

  • Pingback: Simulate an Android Lock Screen with ActionScript : Technology – World Begins Here – Games, Movies, Apps, Tutorials And More – Visit Guglex.com

  • Fahad Jahandad

    Thanks