Flash prototype: walking on disappearing tiles

Read all posts about "" game

I remember an old c64 game (and some flash and javascript remakes) where you had to walk over all tiles in a stage, never walking on the same tile twice, because as you leave a tile, it disappears.

In this far-from-being-finished prototype, I took some pieces of code I found in a tutorial published on gotoandplay and coded this prototype.

Try to walk over all tiles, without falling down.

Cursor keys to move.

The whole actionscript is on the 1st frame

map = new Array();
generateMap();
drawIt(map);
_root.attachMovie("item", "sprite", 10000);
sprite._x = 40;
sprite._y = 40;
sprite.px = 2;
sprite.py = 2;
sprite.speed = 2;
sprite.steps = (20/sprite.speed);
sprite.moving = false;
sprite.count = 0;
sprite.dead = 0;
sprite.onEnterFrame = function() {
	if (!this.dead) {
		if (!this.moving) {
			actual_tile = this.px+(this.py*7);
		}
		if ((Key.isDown(Key.LEFT)) && (!this.moving)) {
			this.px--;
			dir = "left";
			this.moving = true;
		}
		if ((Key.isDown(Key.RIGHT)) && (!this.moving)) {
			this.px++;
			dir = "right";
			this.moving = true;
		} else if ((Key.isDown(Key.UP)) && (!this.moving)) {
			this.py--;
			dir = "up";
			this.moving = true;
		} else if ((Key.isDown(Key.DOWN)) && (!this.moving)) {
			this.py++;
			dir = "down";
			this.moving = true;
		}
		if (this.moving) {
			_root["tile_"+actual_tile]._alpha -= (100/sprite.steps);
			trace(actual_tile+"-"+actual_tile%7+"-"+int(actual_tile/7));
			map[actual_tile%7][int(actual_tile/7)] = 0;
			if (this.count>=this.steps) {
				this.moving = false;
				this.count = 0;
			} else {
				switch (dir) {
				case "left" :
					this._x -= this.speed;
					break;
				case "right" :
					this._x += this.speed;
					break;
				case "up" :
					this._y -= this.speed;
					break;
				case "down" :
					this._y += this.speed;
					break;
				}
				this.count++;
			}
		}
		if (!this.moving) {
			valore = map[this.px][this.py];
			if (valore == 0) {
				this.dead = 1;
			}
		}
	}
	if (this.dead) {
		this._alpha--;
		if (this._alpha<0) {
			this.dead = 0;
			this._x = 40;
			this._y = 40;
			this.px = 2;
			this.py = 2;
			this._alpha = 100;
			generateMap();
			drawIt(map);
		}
	}
};
function drawIt(theMap) {
	for (var y = 0; y<7; y++) {
		for (var x = 0; x<7; x++) {
			pos = x+(y*7);
			if (theMap[x][y] == 1) {
				_root.attachMovie("myTile", "tile_"+pos, pos);
				_root["tile_"+pos]._x = x*20;
				_root["tile_"+pos]._y = y*20;
			}
		}
	}
}
function generateMap() {
	map[0] = new Array(0, 0, 0, 0, 0, 0, 0);
	map[1] = new Array(0, 1, 1, 1, 1, 1, 0);
	map[2] = new Array(0, 1, 1, 1, 1, 1, 0);
	map[3] = new Array(0, 1, 1, 1, 1, 1, 0);
	map[4] = new Array(0, 1, 1, 1, 1, 1, 0);
	map[5] = new Array(0, 1, 1, 1, 1, 1, 0);
	map[6] = new Array(0, 0, 0, 0, 0, 0, 0);
}

Some comments to the code:

Lines 95-103 (yes, it's better to begin from the end): I define the map, in this case a 7*7 array. "1" means there is a tile on the floor, "0" means there is no floor, and you will fall down if you step over it.

Lines 83-94: Time to draw the map, attaching the same movieclip (a 20*20 red square) when I find a "1" in the map array.

Lines 1-13: Initializing the game and creating some variables, most of all about the main sprite (the 20*20 green square, the sprite you control)

Lines 15-36: Checking if an arrow key is pressed and if the sprite was already moving, then calculating new sprite's position according to the key pressed.

Lines 37-61: Routine that moves the main sprite and makes the floor disappear behind it

Lines 62-67: Checking if the sprite is on a tile or not

Lines 69-80: If the sprite is dead, because he has not floor under its feet, re-initialize the game and start again.

That's all at the moment, download the source code and give me feedback while I unleash some more ideas...

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