theARTboy
where art meets design
Writing a Duck Hunt style game in Actionscript
Categories: flash

You can download the files buildingDuckHunt here.

File 0 contains the artwork.
File 1 starts adds the duck to the stage with ActionScript.
[as3]
//imports
import flash.display.MovieClip;

//variables
var duck:MovieClip=new Duck();

addChild(duck);
duck.x=stage.stageWidth*0.5;
duck.y=stage.stageHeight*0.5;

//functions
[/as3]

File 2 adds the remainder of the artwork to the stage.
[as3]
//imports
import flash.display.MovieClip;

//variables
var duck:MovieClip;
var scorebox:MovieClip;
var crosshairs:MovieClip;

//functions
function setup():void
{
duck=new Duck();
duck.x=Math.random()*(stage.stageWidth-100)+50;
duck.y=Math.random()*(stage.stageHeight-100)+50;
addChild(duck);

scorebox=new Scorebox();
scorebox.x=20;
scorebox.y=10;
addChild(scorebox);

crosshairs=new Crosshairs();
crosshairs.x=stage.stageWidth*0.5;
crosshairs.y=stage.stageHeight*0.5;
addChild(crosshairs);

}

setup();
[/as3]

File 3 adds a click action to the duck and scoring.
[as3]
//imports
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

//variables
var duck:MovieClip;
var scorebox:MovieClip;
var crosshairs:MovieClip;

var score:int;

//functions
function setup():void
{
duck=new Duck();
duck.x=Math.random()*(stage.stageWidth-100)+50;
duck.y=Math.random()*(stage.stageHeight-100)+50;
addChild(duck);
duck.addEventListener(MouseEvent.CLICK, duckClick);

scorebox=new Scorebox();
scorebox.x=20;
scorebox.y=10;
addChild(scorebox);

crosshairs=new Crosshairs();
crosshairs.x=stage.stageWidth*0.5;
crosshairs.y=stage.stageHeight*0.5;
addChild(crosshairs);
crosshairs.mouseEnabled=false;

Mouse.hide();
score=0;

addEventListener(Event.ENTER_FRAME,loop);
}

function loop(e:Event):void
{
crosshairs.x=mouseX;
crosshairs.y=mouseY;

scorebox.scorefield.text="Score: "+score;

}
function duckClick(e:MouseEvent):void
{
score += 1;
}

setup();
[/as3]

File 4 removes the duck instantiation from the setup and add a random spawning to the ducks.
[as3]
//imports
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

//variables
var scorebox:MovieClip;
var crosshairs:MovieClip;

var score:int;

//functions
function setup():void
{
stage.addEventListener(MouseEvent.CLICK, duckClick);

scorebox=new Scorebox();
scorebox.x=20;
scorebox.y=10;
addChild(scorebox);

crosshairs=new Crosshairs();
crosshairs.x=stage.stageWidth*0.5;
crosshairs.y=stage.stageHeight*0.5;
addChild(crosshairs);
crosshairs.mouseEnabled=false;

Mouse.hide();
score=0;

addEventListener(Event.ENTER_FRAME,loop);
}

function loop(e:Event):void
{
crosshairs.x=mouseX;
crosshairs.y=mouseY;

scorebox.scorefield.text="Score: "+score;

enemySpawn();
addChild(crosshairs);

}
function duckClick(e:MouseEvent):void
{
if (e.target is Duck){
score += 1;
e.target.parent.removeChild(e.target);
}

}
function enemySpawn():void
{
if (Math.floor(Math.random()*30) == 0)
{
var duck:MovieClip=new Duck();
duck.x=Math.random()*(stage.stageWidth-100)+50;
duck.y=Math.random()*(stage.stageHeight-100)+50;
addChild(duck);
}
}

setup();
[/as3]

The video instructions of how the code all fits together can be watched below.
httpvh://www.youtube.com/watch?v=POXI2CP4T78

Leave a Reply

You must be logged in to post a comment.