Flash Toony prototype ported to Haxe

Read all posts about "" game

It seems Flash Toony prototype is getting quite an interest because some days after the iPhone port made with Corona SDK, it’s the time for Julian Liebl from Nortlight Games to show us the Haxe port of the prototype.

Haxe can be compiled to all popular programming platforms with its fast compiler – JavaScript, Flash, NekoVM, PHP, C++, C# and Java (soon) – which means your apps will support all popular mobile devices, such as iOS, Android, Windows Mobile, webOS and more.

package;

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import haxe.Timer;
import flash.Lib;
import flash.Vector;
import flash.geom.Point;

/**
 * @author Emanuele Feronato ported by Julian Liebl from Flash to Haxe
 */

class Toony extends Sprite {
	
	var timer:Timer;
	var returnPoint:Point;
	var movingToony:Sprite;
	var draggableToonies:Array;
	var fallingToonies:Vector;

	public function new() {
		super();
		init();
		construct();
		create();
	}

	public function init(){
		movingToony = null;
		draggableToonies = new Array();
		fallingToonies = new Vector();

		//init Timer
		timer = new Timer(2000);
		timer.run = newToony;
	}

	public function construct(){
		//lets create a toony with the shape of a circle
		var circle:Sprite = constructShape(0);

		//lets create a toony with the shape of a rectangle
		var rectangle:Sprite = constructShape(1);

		//lets create a toony with the shape of a round rectangle
		var roundRectangle:Sprite = constructShape(2);

		draggableToonies.push(circle);
		draggableToonies.push(rectangle);
		draggableToonies.push(roundRectangle);
	}

	//let's code the shapes ourself. That's badass ;)
	public function constructShape(index:Int):Sprite{
		var returnSprite:Sprite = new Sprite();
		switch(index){
		    case 0:
		    	returnSprite.name = "circle";
				returnSprite.graphics.beginFill(0x0621a9);
				returnSprite.graphics.drawCircle(40, 40, 40);
				returnSprite.graphics.endFill();
			case 1:
				returnSprite.name = "rectangle";
				returnSprite.graphics.beginFill(0xff0000);
				returnSprite.graphics.drawRect(0 , 0, 80, 80);
				returnSprite.graphics.endFill();
			case 2:
				returnSprite.name = "round rectangle";
				returnSprite.graphics.beginFill(0xFFCC00);
				returnSprite.graphics.drawRoundRect(0 , 0, 80, 80,40,40);
				returnSprite.graphics.endFill();
		    default:
		        trace("Not known index. Returing empty Sprite.");
		}
		return returnSprite;
	}

	public function create(){
		// creation of the four draggable toonies
		var loops:Int = 0;

		for (toony in draggableToonies) {
			toony.x=loops*100+80;
			toony.y=370;
			toony.buttonMode=true;

			addChild(toony);
			toony.addEventListener(MouseEvent.MOUSE_DOWN,toonyClicked);

			loops++;
		}
		// main game loop
		addEventListener(Event.ENTER_FRAME,update);

		// event to be triggered when the player releases the mouse button
		Lib.current.addEventListener(MouseEvent.MOUSE_UP,toonyReleased);
	}

	private function newToony():Void {
		// it's simple: I just create a new Toony instance and place it
		// randomly in the game field with a random frame shown
		var toony:Sprite = constructShape(Math.ceil(Math.random()*draggableToonies.length)-1);
		addChild(toony);
		toony.x=Math.random()*580;
		toony.y=-32;
		
		toony.alpha=0.5;
		// pushing the newly created toony into toonies vector
		fallingToonies.push(toony);
	}

	private function toonyClicked(e:MouseEvent):Void {
		returnPoint = new Point(e.target.x, e.target.y);
		// if I am not moving any toony...
		if (movingToony==null) {
			// setting the toony I am about to move to the toony I just pressed the mouse on
			movingToony=e.target;
		}
	}

	private function toonyReleased(e:MouseEvent):Void {
		// if I am moving a toony...
		if (movingToony!=null) {
			// looping through toonies vector
			for (toony in fallingToonies) {
				// if I am touching a falling toony with the same shape as the toony I am currently moving...
				// (that is: if both toonies are showing the same frame...)
				if (toony.hitTestPoint(mouseX,mouseY,true) && movingToony.name==toony.name) {
					// the toonies match!! Highlighting the falling toony
					toony.alpha=1;
				}
			}
			// putting the moved toony back to its place
			movingToony.y=returnPoint.y;
			movingToony.x=returnPoint.x;
			// setting the variable which hold the moving toony to null
			// I am not moving any toony now
			movingToony=null;
		}
	}

	private function update(e:Event):Void {
		// if I am moving a toony...
		if (movingToony!=null) {
			// updating toony position according to mouse position
			movingToony.x=mouseX-movingToony.width/2;
			movingToony.y=mouseY-movingToony.height/2;
		}

		var loop:Int = 0;

		for (toony in fallingToonies) {
			// moving toonies down
			toony.y++;
			// removing toonies from the stage if they are too close to the bottom of the stage
			if (toony.y>280) {
				toony.parent.removeChild(toony);
				fallingToonies.splice(loop,1);
			}
			loop++;
		}
	}

	public static function main () {
		Lib.current.addChild(new Toony ());
	}
}

and the result, compiled as SWF, is:

You should know how to play, drag and drop the shapes in the bottom row to match falling shapes.

In the zipped archive Julian shares with us, you can find the complete source code as well as the Flash and Mac App builds.

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