~when
you first load the page, first you need to click inside the .swf window to play
the game ( hope you are not killed before you do this :-) )
~then use your left/right arrow key to control the box, trying to avoid being
hit by the flowers
~refresh
if you want to play again
code on scene timeline, layer "code", frame 1:
//task09
//
//use the arrow key (left key / right key) on the keyboard to control the box's (the "gun") position,
//once the box(gun) is hit by the flower, it'll reduce its size,
//it turns transparent to warn you if it's near dying,
//"game is over" when it was 40% of the original size
//make an array of all the flowers
var flowerArray = new Array (flower00, flower01, flower02, flower03, flower04, flower05, flower06, flower07, flower08, flower09, flower10, flower11, flower12, flower13, flower14, flower15, flower16, flower17);
var whichF=0; //index of flowerArray
//set initial position of all flowers
function initFlowers() {
for (whichF=0; whichF<flowerArray.length; whichF++){
flowerArray[whichF].initPosition(whichF);
}
}
//update position of all flowers
function updateFlowers() {
for (whichF=0; whichF<flowerArray.length; whichF++){
flowerArray[whichF].updatePosition(whichF);
}
}
//scale down the gun when it was bombed by the flower
function bombed(){
for (whichF=0; whichF<flowerArray.length; whichF++){
if (gun.distanceTo(flowerArray[whichF])gun.sumRadiusWith(flowerArray[whichF])){
//if their distance is less than the sum of their radius
//meaning they are colliding
gun._yscale-=2;
gun._xscale-=2;
}
}
}
//do whatever is needed to update frame
function frameUpdate() {
if(gun._xscale<60){gun._alpha=50;} //if the gun is 60% of its original size,
//turn it to transparent to warn the player
if(gun._xscale<40){ _root.gotoAndStop(2);} //if it's 40%, go to frame 2, which is the "game over" frame
updateFlowers(); //update flowers position
bombed(); //detect if bomb is happening
}
//do something when each frame is done
this.onEnterFrame = frameUpdate;
//init the movie when loaded
this.onLoad = initFlowers;
stop();
code on movieClip "flowerMov", layer "code", frame 1:
//"flowerMov" - object's properties
//this.amount=18; //was trying to set the total number of flowers
this._xscale=30;
this._yscale=30; //set init scale of flowers
this.column=6; //how many columns of flowers are in the scene
this.yVelocity=4; //how fast are they falling (pixel/frame)
this.swingAmp=8; //how hard the flowers swing back and forth
this.randomX=0; //each flower is randomly put in one column decided by randomX
this.norm = random(this.column)/ this.column; //a random number controls the phase shift of flowers
//
this.flowerMov = _root.flowerMov;
//"flowerMov" - object's methods
var columnWidth=Stage.width/this.column; //how many pixels are there in one column
//initiate the position of each flower
this.initPosition = function(whichFlower){
this.randomX=columnWidth*( random(this.column)+.5); //for each flower, pick a random x position for it
this._x=this.randomX;
this._y=-whichFlower*20; //evenly distribute the flowers in y direction
//and they are out of scene at the beginning
}
//update the position of each flower
this.updatePosition = function(whichFlower){
var milliseconds = (new Date()).getTime(); //get a time variable
var period = 2*1000; //time needed for each cycle of swing
var phaseShift = this.norm * (2 * Math.PI); //calculate the phase shift for each flower
var radians = (2 * Math.PI) * ((milliseconds % period) / period); //the input for the swing sin curve
this._x = this.randomX+this.swingAmp*Math.sin(radians+phaseShift); //make the x position follow the sin curve
if (this._y<200) {this._y+=this.yVelocity;} //if the flower has not reach the bottom, make it go down
else {this._y=-this._height/2-150;} //if it has reached the bottom, put it to the top for the next fall
};
//stop
stop();
code on movieClip "gunMov", layer "code", frame 1:
//"gunMov" - object's properties
this._x=Stage.width/2;
this._y=Stage.height-this._height/2-10; //give the initial position of the gun
this.gun = _root.gun;
//"gunMov" - object's methods
//calculate the distance with a falling flower (center to center)
function distanceTo(aflower){
return Math.sqrt(Math.pow((this._x-aflower._x),2)+Math.pow((this._y-aflower._y),2));
};
//calculate the sum of the radius of the gun and the flower
function sumRadiusWith(aflower){
return this._width/2+aflower._width/2;
};
//stop
stop();
code on instance "gun" of "gunMov":
onClipEvent (enterFrame) {
if(Key.isDown(Key.RIGHT)) { //if right arrow key is pressed, move gun 4 pixel rightwards
if (this._x<190){ //if it hasn't go beyond the right edge
this._x+=4;
}
}
else if (Key.isDown(Key.LEFT)) { //if left arrow key is pressed, move gun 4 pixel leftwards
if (this._x>10){ //if it hasn't go beyond the left edge
this._x-=4;
}
}
}