Flash game creation tutorial – part 5.1

Read all posts about "" game

March 14th update: part 5.3 released.
March 3rd update: part 5.2 released.

Well, since lots of readers commented and mailed me part 6a is too hard and I should split it in pieces, that’s what I am going to do. This is the part 5.1, to be read after part 5.

Of course you should read parts 1 to 5 if you didn’t already.

No walls and speed limit

Well, until now we have always seen the stage perimeter limited by walls. What if there are no walls?
Without walls, when the player exits to the top, he should appear from the bottom, if he exits to the right he should appar from the left, and so on.

You can imagine that if you leave the player in a “free fall” or move him in the same direction for a long time, he may gain a lot of speed, making the game unplayable.

So we have to limit the speed if we want to remove walls.

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	if (xspeed>15) {
		xspeed = 15;
	}
	if (xspeed<-15) {
		xspeed = -15;
	}
	if (yspeed>15) {
		yspeed = 15;
	}
	if (yspeed<-15) {
		yspeed = -15;
	}
	_y = _y+yspeed;
	_x = _x+xspeed;
	if (_x<0) {
		_x += 500;
	}
	if (_y<0) {
		_y += 350;
	}
	if (_y>350) {
		_y -= 350;
	}
	if (_x>500) {
		_x -= 500;
	}
	_rotation = _rotation+xspeed;
}

Lines 25-36: Controlling that xspeed and yspeed range between -15 and 15

Lines 39-50: Controlling when the hero exits from the stage (in this case the values are equal to movie size, but they may differ if you plan to put a score bar or something else).

Invulnerability

Sometimes we do not want the player to die twice (or more) in a couple of seconds… it would be too frustrating… the solution is to grant a limited time when the player is invulnerable after his death.

We need a flag that determines invulnerability. I decided to use the _alpha because I want to show the player transparent when it is invulnerable, but of course you can use a variable of your choice.

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	if (xspeed>15) {
		xspeed = 15;
	}
	if (xspeed<-15) {
		xspeed = -15;
	}
	if (yspeed>15) {
		yspeed = 15;
	}
	if (yspeed<-15) {
		yspeed = -15;
	}
	_y = _y+yspeed;
	_x = _x+xspeed;
	if (_x<0) {
		_x += 500;
	}
	if (_y<0) {
		_y += 350;
	}
	if (_y>350) {
		_y -= 350;
	}
	if (_x>500) {
		_x -= 500;
	}
	_rotation = _rotation+xspeed;
	if (_alpha<100) {
		_alpha += 0.5;
	}
	if ((_root.wall.hitTest(_x, _y, true)) and (_alpha>=100)) {
		xspeed = 0;
		yspeed = 0;
		_x = 30;
		_y = 30;
		_alpha = 10;
	}
}

Lines 52-54: if the _alpha is less than 100 (fully opaque), it increases the _alpha by 0.5

Ok, but when will alpha be less than 100?

Line 55-61: if the player hits the object instanced as wall and the player’s _alpha is less than 100, he moves to his starting position and his x and y speed are set to 0. Moreover, his _alpha is set to 10.

So, when the game starts, the player as _alpha to 100. If the player hits the wall, he dies and his _alpha is set to 10.

Since the _alpha is less than 100, it’s increased by 0.5… _alpha now is 10.5, then 11, 11.5 and so on until 100.

If the _alpha is still less than 100 (so the player died some seconds ago) ant the player hits the wall, he doesn’t die because his alpha is still less than 100.

That’s what I call “invulnerability”

now, a new feature not covered until now…

Scrolling

Let’s imagine a laaarge stage. How can you explore it? Easy, with the scrolling.

How can we manage scrolling? It’s easy, we just keep the player fixed in the center of the movie, and move the background.

In order not to making it too CPU expansive, I suggest you to draw the background in a 1/10 scale, then resize it once on the stage.

Look at the actionscript:

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	_root.wall._y -=yspeed;
	_root.wall._x -=xspeed;
	_rotation = _rotation+xspeed;
	if (_root.wall.hitTest(_x, _y, true)) {
		xspeed = 0;
		yspeed = 0;
		_root.wall._x = 2500;
		_root.wall._y = 1681;
	}
}

The only difference is on lines 25 and 26: the player does not moves, the wall does it!! When in previous examples the player moved to the left, now we move the wall to the right, when the player moved down, we now move the wall up, and so on.

When the player hits the wall (line 28) we do not reset hero’s position but wall position (lines 31-32).

Can you run through all the tunnel?

I hope scrolling opened you mind about some new game concepts.

Download the full examples, give me feedback and remember to send me your games, if made starting from one of my tutorials.

Then, continue wit part 5.2

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