(single) REDIGERET 16 June 2009 • by admin

Bat_Ball classes

bat class

package {
	import flash.display.MovieClip;
	import flash.events.*;
	public class bat extends MovieClip {
		public function bat() {
			//INIT();
		}
		function INIT() {
			addEventListener(Event.ENTER_FRAME, update);
		}
		function update(e:Event) {
			y= stage.stageHeight-(height/2);
			x= stage.mouseX;
		}
		function EXIT() {
			removeEventListener(Event.ENTER_FRAME, update);
		}
	}
}

ball class

package {
	import flash.display.MovieClip;
	import flash.events.*;
	public class ball extends MovieClip {
		var xSpeed=10;
		var ySpeed=10;
		var margin=width/2;
		public function ball() {
			//INIT();
		}
		function INIT() {
			y=0+margin;
			addEventListener(Event.ENTER_FRAME, update);
		}
		function update(e:Event) {
			if (x<0+margin) {
				x=0+margin;
				xBounce();
			}
			if (x>stage.stageWidth-margin) {
				x=stage.stageWidth-margin;
				xBounce();
			}
			if (y<0+margin) {
				y=0+margin;
				yBounce();
			}
			if (y>=stage.stageHeight) {
				y=0+margin;
			}
			x+=xSpeed;
			y+=ySpeed;
		}
		function xBounce() {
			xSpeed=xSpeed*-1;
			gotoAndPlay("pong");
		}
		function yBounce() {
			ySpeed=ySpeed*-1;
			gotoAndPlay("pong");
		}
		function EXIT() {
			removeEventListener(Event.ENTER_FRAME, update);
		}
	}
}

doc class

package {
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.text.*;
	public class doc extends MovieClip {
		var ourBat = new bat();
		var ourBall = new ball();
		var score:int;
		var points:int=1;
		var scoreMIN:int=-2;
		var scoreMAX:int=2;
		public function doc() {
			stop();
			//INIT();
			back_but.addEventListener(MouseEvent.CLICK, goGame);
		}
		function INIT() {
			score=0;
			score_txt.text=score+" points yet";
			addChild(ourBat);
			ourBat.INIT();
			addChild(ourBall);
			ourBall.INIT();
			addEventListener(Event.ENTER_FRAME, update);
		}
		function update(e:Event) {
			if (ourBall.hitTestObject(ourBat)) {
				ourBall.yBounce();
				score+=points;
			}
			if (ourBall.y>stage.stageHeight) {
				score-=points;
			}
			result();
		}
		function result() {
			//trace("Du har " + score + " points");
			score_txt.text="points: "+score+" out of "+scoreMAX ;
			if (score==scoreMAX) {
				EXIT();
				gotoAndStop("win");
			}
			if (score==scoreMIN) {
				EXIT();
				gotoAndStop("lose");
			}
		}
		function EXIT() {
			removeEventListener(Event.ENTER_FRAME, update);
			removeChild(ourBat);
			ourBat.EXIT();
			removeChild(ourBall);
			ourBall.EXIT();
		}
		function goGame(e:MouseEvent) {
			gotoAndStop("game");
			INIT();
		}/**/
	}
}