Create a Flash game like The Impossible Line step 1 – draw following mouse movement

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers