Monthly Archives: February 2010

IDAT102 – Flash Game – 0026-0030

I first added sounds here but encountered a problem. Whenever a sound was played it would cause the player movement to freeze for a fraction of a second. This negatively impacted gameplay significantly. After attempting many different solutions I eventually found the problem. For some reason when a sound is first played the player must load something inorder to play sounds. Immediatly after finishing playing that sound it stops it. By running a continuous sound loop in the background this never stops, removing the freezing problem.

IDAT102 – Flash Game – 0015-0025

After creating the code for random ball generator, removeral and hit detection it was relatively simple to replicate the code for each of the three types of ball I have decided to use. The variables used had to be changed to prevent conflicts between the various parts of the code.

I added code to freeze the game once the timer reaches zero:

if ((count)-myTimer.currentCount == 0){
finalscoreTxt.text = "You scored " + String(score);
gameState = "finished";
ballP1.alpha = 0.5;
}

The frequency of which the code generates different types of balls was balanced:

setInterval(ballGenGOOD,100);
setInterval(ballGenBAD,500);
setInterval(ballGenBONUS,10000);

The interval time for the bonus ball was later randomised and effected by an external rss feed.

A play again button was added which is moved from outside of the stage once the timer reaches zero:

function playagain(event:MouseEvent):void {
countTxt.text = time;
againTxt.y = 700;
finalscoreTxt.text = "";
ballP1.alpha = 1;
gameState = "started"
score = 0;

var count:Number = time;
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void{
countTxt.text = String((count)-myTimer.currentCount);
}

IDAT102 – Flash Game – Hit Detection 0010-0015

I first inter grated simple collision detection into my masterLoop function.

for (var loop2=lag; loop2
if (ballP1.hitTestObject(getChildByName("myBall" + loop2))) {
trace('HIT');
}
}

An error occurred whenever it was testing against a ball that doesn’t exist. I fixed this by adding “if (getChildByName((“myBall” + loop2)) != null)” so that it would only hit test against objects if it could find them.

for (var loop2=lag; loop2
if (getChildByName(("myBall" + loop2)) != null) {
if (ballP1.hitTestObject(getChildByName("myBall" + loop2))) {
trace('HIT');
}
}
}

Later the trace was replaced with:

var currentBall = getChildByName("myBall" + loop2).name;
removeChild(getChildByName(currentBall));

As well as this scoring was added.

IDAT102 – Flash Game – Ball generation S2-0001-0009

I decided to use a separate .fla for this part and to only add it to the main version when it was working properly. For this part to be complete I wanted to be able to address, identify and remove each individual ball separately and for them to automatically expire after a set time period.

I started with simply producing movieclips with the following code:

function mainFunc():void {
var clips;
var newMC:mc = new mc();
addChild(newMC);
newMC.x= Math.random() * 550;
newMC.y= Math.random() * 400;
clips = newMC;
var size:Number = (Math.random() * 20) + 20;
var transp:Number = 1;
clips.height = size;
clips.width = size;
clips.alpha = transp;
}

I then experimented with some code that I had found:

function SelfDelete()
{
rt = new Timer(100, 50);
rt.addEventListener(TimerEvent.TIMER, fadeMe);
rt.addEventListener(TimerEvent.TIMER_COMPLETE, deleteMe);
rt.start();
}
function deleteMe(event:TimerEvent)
{
parent.removeChild(this);
}
function fadeMe(event:TimerEvent):void
{
this.alpha -= 0.01;
}

This function applied to every ball at exactly the same time so I needed to give each ball a unique name so I replaced everthing with:

function mainFunc():void {
var clips:Number;
var myBall:mc = new mc();
addChild(myBall).name = "myBall" + i;
myBall.x= Math.random() * 550;
myBall.y= Math.random() * 400;
var size:Number = (Math.random() * 20) + 20;
myBall.height = size;
myBall.width = size;
myBall.alpha = Math.random() + 0.3;
j = i - 5;
a = "myBall" + int(j);


if (i > 4) {
removeChildAt(j);
trace(i);
}
i = i + 1;
}

This seemed to work at first but then began to continuously output errors.

“RangeError: Error #2006: The supplied index is out of bounds.”

I eventually solved this problem by using getChildByName(“name”) when deleting the ball movieclip.

To fade the balls before deleting them I used:

if (i >= 5){getChildByName("myBall" + (i - 5)).alpha = 0.9;}
if (i >= 6){getChildByName("myBall" + (i - 6)).alpha = 0.8;}
if (i >= 7){getChildByName("myBall" + (i - 7)).alpha = 0.7;}
if (i >= 8){getChildByName("myBall" + (i - 8)).alpha = 0.6;}
if (i >= 9){getChildByName("myBall" + (i - 9)).alpha = 0.5;}
i = ++i;

I later converted this into a loop:

for (var z:Number = 5; z <>= z){getChildByName("myBall" + (i - z)).alpha = ((dotsNum - z)/dotsNum) ;}
}
i = ++i;

After this I was ready to merge the two .fla files.

IDAT102 – Flash Game – Movement 2 – 0007

While testing the player movement system I discovered that the player, when moving diagonally, exeeded the set speed. This was due to Pythagoras’ theorem. For example, if the horizontal and vertical speed was 10 then the diagonal speed would be 14.14, the square root of 10 squared plus 10 squared.

To resolve this I created 2 new variables, startSpeed to contain the desired speed and diagSpeed to contain the calculated speed that will result in the startSpeed when moving diagonally.

var startSpeed = 15;
var speed = startSpeed;
var diagSpeed = (Math.sqrt((startSpeed * startSpeed)*2))/2;

Into the masterLoop I added:

if ((leftIsPressed && upIsPressed)
(leftIsPressed && downIsPressed)
(rightIsPressed && upIsPressed)
(rightIsPressed && downIsPressed))
{speed = diagSpeed;}else{speed = startSpeed}

It detects if the player is moving diagonally and changes the speed to diagSpeed, if the player isn’t then it stays at or reverts to the startSpeed.

IDAT102 – Flash Game – Timer and play button 0004-0006

Next I added a timer using the code:

var time = 120;
var count:Number = time;
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
countTxt.text = String((count)-myTimer.currentCount);
}

This counts down from 120 displaying the current time remaining in countTxt.#

I then created a separate frame for a splash screen at the start of the game. To prevent the game simply playing through the frames as it would an animation, I started the actionscript with stop();. After this I added a button which becomes lighter on mouseover and onclick runs nextFrame();.

I experienced an error when transitioning between frames which I eventually determined to be because of the event listeners on the first frame persisting. To fix this I used:

btnPlay.removeEventListener(MouseEvent.MOUSE_OVER, hoverON);
btnPlay.removeEventListener(MouseEvent.MOUSE_OUT, hoverOFF);