Introducing Kiwi.js framework with the creation of an HTML5 Spellfall prototype

Read all posts about "" game

If you liked the Spellfall prototype I showed you earlier this week made with Phaser, i am going to show you the same prototype made with another HTML5 framework which works in a very similar manner: Kiwi.js.

kiwi

Among its features you will find:

2D CANVAS AND WEBGL RENDERING: Your games can target both canvas and WebGL. Use canvas for older browsers and most mobile browsers. Use WebGL for the latest browsers, and in conjunction with Cocoon.js to build fast native mobile apps.

TARGET COCOON.JS: Make games for app stores by configuring your game to compile within the Cocoon.js system.

FLEXIBLE GAME OBJECTS: Support for spritesheets, texture atlases and individual images give you plenty of options for creating and managing animations and static images.

FULL DISPLAY LIST: Place your game objects within groups and subgroups to manage their transformation and z – ordering.

ENTITY / COMPONENT SYSTEM: Build your own game objects by extending the core game objects and attaching components such as physics to give them additional functionality.

MULTITOUCH SUPPORT: Make the most of touch device capability and make games for multitouch and well as single touch.

STATE MANAGEMENT: Easily create and manage your game states. States are the main way you arrange and manage your resources.

FILE MANAGEMENT AND LOADING: Easily add images, audio and data resources. Includes resource loaders, and easy access and management of your files.

The making of Spellfall prototype was very similar to the Phaser version, so here is the source code organized in the same way as the Phaser counterpart for you to compare them:

<!doctype html>
<html>
	<head>
    		<script src="kiwi.min.js"></script>
    		<style>
    			body{margin:0}
    		</style>
    		<script type="text/javascript">
						
				var game = new Kiwi.Game();
				var gameState = new Kiwi.State("gameState");
                    //var stage = new Kiwi.Stage(game,"game",300,300,SCALE_FIT);
				
				// variables to customize the game
				
				var tileSize = 50;				// tile size, in pixels
				var fieldSize = 6;     			// number of tiles per row/column
				var tileTypes = 6;				// different kind of tiles allowed
				var pickedZoom = 1.1;              // zoom ratio to highlight picked tile
				
				// variables used by game engine
				
				var dragging = false;			// are we dragging?
				var movingRow;					// row of the moving tile
				var movingCol;					// col of the moving tile
				var tileArray = [];				// array with all game tiles
                    var tileGroup; 				// group containing all tiles
                    var movingTileGroup;               // group containing the moving tile
				
				game.states.addState(gameState, true);
				
				gameState.preload = function(){	
                         this.game.stage.resize(300, 300);
                         this.game.stage.setRGBColor(0,0,0)
					Kiwi.State.prototype.preload.call(this);
					this.addSpriteSheet("tiles", "tiles.png", 50, 50);				
				}
				
				gameState.create = function(){
					Kiwi.State.prototype.create.call(this);
					for(i=0;i<fieldSize;i++){
						tileArray[i]=[];
						for(j=0;j<fieldSize;j++){
							var randomTile = Math.floor(Math.random()*tileTypes)
							var theTile=new Kiwi.GameObjects.Sprite(this, this.textures["tiles"], j*tileSize, i*tileSize);
							theTile.centerAnchorPoint();
							theTile.animation.switchTo(randomTile);
							this.addChild(theTile);
							//theTile.anchor.setTo(0.5,0.5);
							tileArray[i][j]=theTile;
                                   //tileGroup.add(theTile);	
						}
					}
					this.game.input.onDown.add(pickTile);					
				}
				
				function pickTile(inputX, inputY){
					// save input coordinates
					startX = inputX;
					startY = inputY;
					// retrieve picked row and column 
					movingRow = Math.floor(startY/tileSize);
					movingCol = Math.floor(startX/tileSize);
					// zoom the tile
					tileArray[movingRow][movingCol].scaleToWidth(tileSize*pickedZoom);
					tileArray[movingRow][movingCol].scaleToHeight(tileSize*pickedZoom);
                         // moving the tile in front of the stage
					gameState.swapChildren(tileArray[movingRow][movingCol],gameState.getChildAt(gameState.numChildren()-1));
					this.game.input.onDown.remove(pickTile);
					this.game.input.onUp.add(releaseTile);
					dragging=true;				
				}
				
				function releaseTile(){
					// remove the listener
					this.game.input.onUp.remove(releaseTile);
					// determine landing row and column
					var landingRow = Math.floor((tileArray[movingRow][movingCol].y+tileSize/2)/tileSize);
					var landingCol = Math.floor((tileArray[movingRow][movingCol].x+tileSize/2)/tileSize);
					// reset the moving tile to its original size
					tileArray[movingRow][movingCol].scaleToWidth(tileSize);
					tileArray[movingRow][movingCol].scaleToWidth(tileSize);
					// swap tiles, both visually and in tileArray array...
					tileArray[movingRow][movingCol].x=landingCol*tileSize;
					tileArray[movingRow][movingCol].y=landingRow*tileSize;
					if(movingRow!=landingRow || movingCol!=landingCol){
                              // but only if you actually moved a tile
                              gameState.swapChildren(tileArray[landingRow][landingCol],gameState.getChildAt(gameState.numChildren()-1));
						var moveTween = this.game.tweens.create(tileArray[landingRow][landingCol]);
                              moveTween.to({x: movingCol*tileSize,y: movingRow*tileSize}, 800, Kiwi.Animations.Tweens.Easing.Exponential.Out);
                              moveTween.start();
                              moveTween.onComplete(function(){
                                   var temp = tileArray[landingRow][landingCol];
                                   tileArray[landingRow][landingCol] = tileArray[movingRow][movingCol];
                                   tileArray[movingRow][movingCol] = temp;	
                                   this.game.input.onDown.add(pickTile);
                              })	
					}
					// we aren't dragging anymore
					dragging = false;
				}
				
				gameState.update = function(){
					// if we are dragging a tile
					if(dragging){
						// check x and y distance from starting to current input location
						distX = this.game.input.x-startX;
                    		distY = this.game.input.y-startY;
                    		// move the tile
                              tileArray[movingRow][movingCol].x=movingCol*tileSize+distX;
                              tileArray[movingRow][movingCol].y=movingRow*tileSize+distY;   
					}			
				}
	    	
		</script>
    </head>
    <body>
    </body>
</html>

As you can see, this framework too is really easy to use.

And this is the final result:

You should know how to play: just select a tile and drag onto another to swap them.

Download the source code and give a try to Kiwi.js

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