Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about The Impossible Line game, Actionscript 3, Flash, Game development and iOS.

Did you already play The Impossible Line?

It’s a free iOS game with more than 200 levels. It’s basically a mouse avoider game (let’s call it “finger avoider being a mobile game). All you have to do is drawing a line with your finger from a point A to a point B avoiding obstacles.

The problem is obstacles disappear once you start the level.

The really interesting thing, being a mobile game, is the way you can draw. Unlike Flash games where you move the mouse, which does not cover the level, moving your finger on the level would cover it. So The Impossible Line guys gave you the capability of moving your finger anywhere on the screen, and your movement will be replicated by the main pointer.

Try it by yourself:

Draw with the mouse anywhere and see how the arrow sprite moves according to your mouse movements.

And this is the fully commented class:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	public class Main extends Sprite {
		// canvas where we'll draw the line
		private var canvas:Sprite = new Sprite();
		// Arrow is the name class you can find in the library containing the arrow image
		private var arrow:Arrow=new Arrow();
		// mousePoint is the actual point we are moving the mouse on
		private var mousePoint:Point;
		// anglePoint is the latest point we checked to determine angle of movement
		private var anglePoint:Point;
		// starting thickness of the line we are drawing. I am changing it often to give some kind of chalk effect
		private var thickness:Number=3;
		public function Main() {
			// adding the canvas to stage
			addChild(canvas);
			// adding the arrow in the middle of the stage
			addChild(arrow);
			arrow.x=320;
			arrow.y=240;
			// at this time we only have to wait for the player to press the mouse button
			stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
		}
		private function mousePressed(e:MouseEvent):void {
			// the player pressed the mouse so we can start drawing - we don't need to wait for the player to press the mouse
			stage.removeEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
			// now we must wait for the player to move or release mouse button
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseReleased);
			// saving mouse position
			mousePoint=new Point(mouseX,mouseY);
			anglePoint=new Point(mouseX,mouseY);
			// placing the graphic pencil under arrow Sprite
			canvas.graphics.moveTo(arrow.x,arrow.y);
		}
		private function mouseMoved(e:MouseEvent):void {
			// the player is moving the mouse, so he's drawing
			// let's set the line style
			canvas.graphics.lineStyle(thickness,0xffffff);
			// finding the horizontal and vertical distances between current mouse position and last saved mouse position
			var dx:Number=mouseX-mousePoint.x;
			var dy:Number=mouseY-mousePoint.y;
			// moving arrow Sprite according to such distances
			arrow.x+=dx;
			arrow.y+=dy;
			// drawing to current arrow position
			canvas.graphics.lineTo(arrow.x,arrow.y);
			// saving current mouse position
			mousePoint.x=mouseX;
			mousePoint.y=mouseY;
			// if the distance between current mouse point and the last point where we updated arrow rotation
			// is greater than 20 pixels, let's rotate arrow sprite according to the angle and save
			// current mouse position in anglePoint Point
			if ((anglePoint.x-mouseX)*(anglePoint.x-mouseX)+(anglePoint.y-mouseY)*(anglePoint.y-mouseY)>400) {
				var angle:Number=Math.atan2(anglePoint.y-mouseY,anglePoint.x-mouseX);
				arrow.rotation=angle*57.2957795-90;
				anglePoint.x=mouseX;
				anglePoint.y=mouseY;
			}
			// increasing line thickness by a random value that can be -1, 0 or 1
			thickness=thickness+Math.floor(Math.random()*3)-1;
			// keeping thickness between 2 and 5
			if (thickness<2) {
				thickness=2;
			}
			if (thickness>5) {
				thickness=5;
			}
		}
		private function mouseReleased(e:MouseEvent):void {
			// the player released the mouse. Stop drawing and let's wait for mouse press
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
			stage.removeEventListener(MouseEvent.MOUSE_UP,mouseReleased);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
		}
	}
}

Next time, adding obstacles and the radar, as in the original game.

Download the source code.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.