String Avoider Starling prototype using Starling-Extension-Graphics library and Flash graphic API methods

Read all posts about "" game

One of the things I was missing the most in Starling framework is I can’t access to AS3 graphic API. I wanted do do a quick port of Stringy game but since it’s mostly based upon graphic API, it seemed impossible… I wasn’t satisfied of my iPhone String Avoider prototype with Adobe Flash Professional CS5.5 so I dropped the project until I found Starling-Extension-Graphics which adds a suite of graphics primitives such as Planes, Fills and Strokes.

With this awesome library I was finally able to make a decent port of the string avoider prototype, have a look:

Main class:

package {
	
	import flash.display.Sprite;
	import starling.core.Starling;
	
	[SWF(width="640",height="480",frameRate="60",backgroundColor="#ffffff")]
	
	public class Main extends Sprite {
		private var _starling:Starling;
		public function Main() {
			_starling=new Starling(Game,stage);
			_starling.showStats=true;
			_starling.start();
		}
	}
}

Game class:

package {
	import flash.geom.Point;	
	import starling.display.Sprite;
	import starling.events.TouchEvent;
	import starling.events.Touch;
	import starling.events.TouchPhase;
	import starling.display.Shape; // <- this belongs to Starling-Extension-Graphics
	public class Game extends Sprite {	
		private var tailLenght:Number=1;
		private var tailNodes:Number=300;
		// Look, I am creating shapes as if they were sprites
		private var bgShape:Shape=new Shape();
		private var tailShape:Shape=new Shape();
		private var nodes:Vector.<Point>=new Vector.<Point>();
		public function Game() {
			// same thing as the good old flash graphic class
			addChild(bgShape);
			bgShape.graphics.beginFill(0x000000);
			bgShape.graphics.drawRect(0, 0,640, 480);
			bgShape.graphics.endFill();
			addChild(tailShape);
			for (var i:Number=0; i<tailNodes; i++) {
				nodes[i]=new Point(320,240);
			}
			addEventListener(TouchEvent.TOUCH,touched);
		}
		private function touched(e:TouchEvent):void {
			// checking if we are dragging the mouse
			var mouseMoved:Touch=e.getTouch(this,TouchPhase.MOVED);
			if (mouseMoved) {
				// retrieving mouse location
				var localPos:Point=mouseMoved.getLocation(this);
				// removing and disposing old shape then creating a new one  allows us to save memory
				removeChild(tailShape,true);
				tailShape=new Shape();
				tailShape.graphics.clear();
				tailShape.graphics.lineStyle(2,0xffffff);
				tailShape.graphics.beginFill(0xffffff);
				tailShape.graphics.drawCircle(localPos.x,localPos.y,5);
				tailShape.graphics.endFill();
				tailShape.graphics.moveTo(localPos.x,localPos.y);
				nodes[0]=new Point(localPos.x,localPos.y);
				for (var i:Number=1; i<tailNodes-1; i++) {
					var nodeAngle:Number=Math.atan2(nodes[i].y-nodes[i-1].y,nodes[i].x-nodes[i-1].x);
					nodes[i]=new Point(nodes[i-1].x+tailLenght*Math.cos(nodeAngle),nodes[i-1].y+tailLenght*Math.sin(nodeAngle));
					tailShape.graphics.lineTo(nodes[i].x,nodes[i].y);
				}
				addChild(tailShape);
			}
		}
	}
}

And this is the result, running at 60fps:

Click and drag the mouse to animate the string. It runs at 60fps smoothly, so tomorrow I am porting it to my iPad and will show you both the source code and the result.

Download the source code with all needed libraries

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

215 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
// Stairs
// 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