1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| package{
import flash.display.*;
import flash.events.*;
import Fruit;
import flash.text.TextField;
import flash.text.TextFormat;
public class Catching extends MovieClip {
//object vars
private var myBasket:Basket = new Basket();
private var fruit01:Fruit = new Fruit();
private var fruit02:Fruit2 = new Fruit2();
private var fruit03:Fruit3 = new Fruit3();
private var fruit04:Fruit4 = new Fruit4();
private var lifeBox:TextField = new TextField();
private var scoreBox:TextField = new TextField();
//life start
private var life:Number = new Number(3);
//score start
private var score:Number = new Number(0);
//stage values
private var stageLeft:Number = 0;
private var stageRight:Number = 1163;
private var stageBottom:Number = 503;
//for basket movement
private var movement:int = 20;
//for basket start placement
private var basketY = stageBottom - myBasket.height;
private var basketX = 450;
//for fruit and skull gravity
private var gravity:int = 5;
//Fonction principale
public function Catching() {
init();
}
//the init function to start game
private function init():void{
switch(currentFrame){
case 1:
startBtn.addEventListener(MouseEvent.CLICK, startGame);
stop();
break
}
}
private function startGame(Event:MouseEvent):void{
gotoAndStop(2);
trace(currentFrame);
removeEventListener(MouseEvent.CLICK, startGame);
addBasket();
addFruit();
stage.focus = stage;
gameListeners();
}
private function gameListeners():void{
addEventListener(Event.ENTER_FRAME, recycleFruit);
addEventListener(Event.ENTER_FRAME, addScore);
addEventListener(Event.ENTER_FRAME, addLife);
}
private function addScore(event:Event):void{
addChild(scoreBox);
scoreBox.defaultTextFormat = new TextFormat("Arial", 20);
scoreBox.textColor = 0x354a5f;
scoreBox.text = "SCORE: " + String(score);
scoreBox.x = stageLeft + 100;
scoreBox.y = stageLeft - 10;
scoreBox.width = 200;
scoreBox.mouseEnabled = false;
switch(currentFrame){
case 3:
scoreBox.defaultTextFormat = new TextFormat("Arial", 40);
scoreBox.text = "Score Final: " + String(score);
scoreBox.height = 80;
scoreBox.width = stage.width;
}
}
private function addLife(event:Event):void{
addChild(lifeBox);
lifeBox.defaultTextFormat = new TextFormat("Arial", 20);
lifeBox.textColor = 0x354a5f;
lifeBox.text = "Vies: " + String(life);
lifeBox.x = stageLeft + 900;
lifeBox.y = stageLeft - 10;
lifeBox.mouseEnabled = false;
switch(currentFrame){
case 3:
removeChild(lifeBox);
}
}
private function addBasket():void{
addChild(myBasket);
myBasket.y = basketY;
myBasket.x = basketX;
stage.addEventListener(KeyboardEvent.KEY_DOWN, movingBasket);
stage.addEventListener(Event.ENTER_FRAME, movingBasketCollision);
}
private function movingBasket(event:KeyboardEvent):void{
if(event.keyCode == 37) //37 is keycode for left arrow
{
myBasket.x -= movement;//subtracts movement integer from the ball's x point
}
if(event.keyCode == 39) //39 is keycode for right arrow
{
myBasket.x += movement;//adds movement integer from the ball's x point
}
}
private function movingBasketCollision(event:Event) :void{
if (myBasket.x <= stageLeft){
myBasket.x = stageLeft;
}
else if (myBasket.x >= (stageRight - myBasket.width)){
myBasket.x = stageRight - myBasket.width;
}
}
private function addFruit():void{
addChild(fruit01);
addChild(fruit02);
addChild(fruit03);
addChild(fruit04);
}
private function recycleFruit(event:Event):void{
if(fruit01.hitTestObject(myBasket.basketHit)){
life --;
removeChild(fruit01);
}
if(fruit02.hitTestObject(myBasket.basketHit)){
score += 1;
removeChild(fruit02);
}
if(fruit03.hitTestObject(myBasket.basketHit)){
score += 1;
removeChild(fruit03);
}
endGame();
}
private function endGame():void{
if(life <= 0){
removeChild(myBasket);
removeChild(fruit01);
removeChild(fruit02);
removeChild(fruit03);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, movingBasket);
stage.removeEventListener(Event.ENTER_FRAME, movingBasketCollision);
removeEventListener(Event.ENTER_FRAME, recycleFruit);
removeEventListener(Event.ENTER_FRAME, addScore);
removeEventListener(Event.ENTER_FRAME, addLife);
gotoAndStop(3);
startOverBtn.addEventListener(MouseEvent.CLICK, startOver);
}
if(score >= 10){
removeChild(myBasket);
removeChild(fruit01);
removeChild(fruit02);
removeChild(fruit03);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, movingBasket);
stage.removeEventListener(Event.ENTER_FRAME, movingBasketCollision);
removeEventListener(Event.ENTER_FRAME, recycleFruit);
removeEventListener(Event.ENTER_FRAME, addScore);
removeEventListener(Event.ENTER_FRAME, addLife);
gotoAndStop(4);
startOverBtn.addEventListener(MouseEvent.CLICK, startOver);
}
}
private function startOver(event:MouseEvent):void{
startOverBtn.removeEventListener(MouseEvent.CLICK, startOver);
removeChild(scoreBox);
gotoAndStop(1);
life = 3;
score = 0;
init();
}
}
} |
Partager