Creation of the engine behind “Nodes” game with Flash

Read all posts about "" game

I always surf the web looking for great Flash games. One site where you can find good ones is Newgrounds.

The #1 Daily Feature in 06/19/2007 was a simple game called Nodes. I suggest you to play it for a couple of minutes. The idea is very simple and the engine behind is very simple too.

Nodes

You have to drag some “nodes” (actually some circles”) and there is a “laser” (actually a line) that connects all nodes.

Let’s see how can we made this engine in 20 lines of code, brackets included.

The only object we need to draw is the node itsef.

As you can see from the picture, I linkaged it as “laser” and aligned it to its center

Nodes

Then, on the first frame of the movie, I entered this actionscript:

laser_nodes = 4;
for (x=1; x<=laser_nodes; x++) {
	node = _root.attachMovie("laser", "laser_"+x, x, {_x:Math.random()*460+20, _y:Math.random()*310+20});
	node.onPress = function() {
		startDrag(this);
	};
	node.onRelease = function() {
		stopDrag();
	};
}
_root.createEmptyMovieClip("ray", _root.getNextHighestDepth());
ray.onEnterFrame = function() {
	this.clear();
	this.lineStyle(3, 0xff0000);
	this.moveTo(_root.laser_1._x, _root.laser_1._y);
	for (x=2; x<=laser_nodes; x++) {
		this.lineTo(_root["laser_"+x]._x, _root["laser_"+x]._y);
	}
	this.lineTo(_root.laser_1._x, _root.laser_1._y);
};

Line 1: Initializing the laser_nodes variable, that will contain the number of laser nodes on the stage

Line 2: Main loop that will place the nodes on stage

Line 3: Attaching the node, naming it laser_1 if it's the first node, laser_2 if it's the second, and so on. The _x and _y starting positions are randomly generated

Lines 4-6: If the node is pressed (if the player clicks and holds the mouse button) then drag the node. Notice that everything is done by startDrag function.

From Macromedia Flash Actionscript Dictionary:

startDrag(target,[lock,left,top,right,bottom])

Parameters:
target: The target path of the movie clip to drag.

lock: A Boolean value specifying whether the draggable movie clip is locked to the center of the mouse position ( true ), or locked to the point where the user first clicked on the movie clip ( false ). This parameter is optional.

left, top, right, bottom: Values relative to the coordinates of the movie clip's parent that specify a constraint rectangle for the movie clip. These parameters are optional.

Description: makes the target movie clip draggable while the movie is playing. Only one movie clip can be dragged at a time. Once a startDrag operation is executed, the movie clip remains draggable until explicitly stopped by a stopDrag action, or until a startDrag action for another movie clip is called.

Lines 7-9: If the node is released (if the player releases the mouse button) then stop dragging the node.

Line 11: Creation of the movie clip where we will draw the ray. The movieclip is called, with a lot of creativity... ray!

Line 12: Beginning of a set of actions to be executed on ray movieclip at every frame

Line 13: Cleaning the movieclip

Line 14: Defining the line style of the movieclip with a 3 pixel height and a bright red color

Line 15: Moving the line pointer to the first laser centre. At this point I recommed you to read Create a flash draw game like Line Rider or others - part 1 tutorial for more information about line drawing in Flash.

Lines 16-18: Loop that draws lines from the first node center to the second node center, from the second node center to the third node center and so on

Line 19: Connecting the last node center with the first one.

And that's it! 20 lines for this interesting entine, that will be used in some other projects. I am currently working to one or two... download it and enjoy

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