×
Namespaces

Variants
Actions
Revision as of 03:01, 14 May 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Creating a simple One-key game in Flash Lite

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

This article describes the creation and code logic of a simple Flash Lite 2.0 game called Heli. Heli is one of the classic video games played with lot of craze by young and old alike. Heli ss.JPG

Article Metadata
Code Example
Source file: Media:heli.zip
Article
Created: manikantan (21 May 2009)
Last edited: hamishwillee (14 May 2013)

Contents

Gameplay

The game is quite simple to play. You have a helicopter to control and take a long trip. The trip’s length is undefined. You have obstacles in between and you need to avoid the helicopter’s collision with these obstacles. More obstacles you cross, higher is your score. Simple Right ?

The code perspective

We need to control the helicopter( I use heli as a substitute, here on ) with a single key. No, sideways movement is permitted for the heli. I personally like to control the heli using keys like * or #. This is an addictive game, its better to assign non-frequently used keys for it and also convenient to have a corner key to control. Ok, now back to code. When we press the * ( as per my code) , the heli lifts up or raises and on normal circumstances ( when nothing is pressed), it dips.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


	if (Key.isDown(56)) {
		heli._y -= Heli_Spd;
	} else {
		heli._y += Heli_Spd;
	}

In addition to this, we also have the obstacles or walls moving constantly to the left. The speed, with which these move, need to be configured as per the game play. Because, the movement happens constantly we code the movement inside an onEnterFrame. We should also make sure that once they reach the left side of the screen, we need to regenerate them at the right side. This will give an illusion as if new obstacles are coming, but actually we reuse the same. Initial placement of these obstacles can be arbitrary, preferably randomly spaced and beyond the right side of the screen. To regenerate the obstacles randomly, use a RandomNumber function and assign it to the _x. and _y attributes of the obstacle. In the following code, w1, w2, w3 are the obstacles.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


onEnterFrame = function () {
	w1._x -= Speed;
	w2._x -= Speed;
	w3._x -= Speed;
	if (w1._x+w1._width<0) {
		Score++;
		w1._x = randRange();
		w1._y = randRangeY();
	}
	if (w2._x+w2._width<0) {
		Score++;
		w2._x = randRange();
		w2._y = randRangeY();
	}
	if (w3._x+w3._width<0) {
		Score++;
		w3._x = randRange();
		w3._y = randRangeY();
	}
	// 56 is keyCode for *   
	if (Key.isDown(56)) {
		heli._y -= Heli_Spd;
	} else {
		heli._y += Heli_Spd;
	}
};

Checking collision

You need to check for collision of the heli with the obstacles regularly or at every short interval of say, 100ms. So, run the collision checking in a setInterval function. To check for collisions, use the hitTest() and to check collision with the borders ( top and bottom ), use simpler _y comparision. Remember, once you get positive results for collision, make the heli invisible and in that place include activate a simple Explosion animated GIF.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


id = setInterval(checkColl, 100);
function checkColl() {
	coll = false;
	if (heli._y<=44) {
		coll = true;
	}
	if ((heli._y+heli._height)>=166) {
		coll = true;
	}
	if (heli.hitTest(w1) || heli.hitTest(w2) || heli.hitTest(w3)) {
		coll = true;
	}
	if (coll) {
		trace("Coll");
		heli._visible = false;
		clearInterval(id);
		xplod._x = heli._x  ;
		xplod._y = heli._y -15;
		xplod.play();
	}
}


Updating Scores

To keep track of scores, simple just count the number of obstacles that reached the left side of the screen ( _x = 0 ). That will be a direct approximation of the score. Display the score to a dynamic text field placed on the top-left region of the screen. Also, remember that High scores can be saved in a local file.


Other Notes, Guidelines

-- Use PNG images for Heli as the background should not be seen.
-- Store the high scores, this will improve the interest among users.
-- Always have a Game Menu screen at the beginning.
-- Allow the user to exit at any point of the game.
-- This article describes only the core logic/code of the game. The design, looks, sounds are out of the scope of this article.


Download

You can get the source FLA, Media:heli.zip.

Author

--manikantan 11:50, 21 May 2009 (EEST)

This page was last modified on 14 May 2013, at 03:01.
88 page views in the last 30 days.