im creating a brick builder game
ive got the paddle and ball moving now i just want the ball to bounce off the paddle hit the brick and then the brick disappears, i am using document classes, im just stuck on this bit, any help would be appreciated, please need your help lol
Expand|Select|Wrap|Line Numbers
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.ui.Mouse;
- public class Breakout extends Sprite {
- var ball_mc:Ball;
- var xVel:int=10;
- var yVel:int=10;
- var paddle_mc:Paddle;
- function Breakout() {
- ball_mc=new Ball();
- ball_mc.x=100;
- ball_mc.y=100;
- this.addChild(ball_mc);
- paddle_mc=new Paddle();
- paddle_mc.x=250;
- paddle_mc.y=380;//this positions the paddle on the screen, currently at the bottom of the screen//
- this.addChild(paddle_mc);
- paddle_mc.init();
- start_btn.addEventListener(MouseEvent.CLICK,startGame);
- this.addEventListener(Event.ENTER_FRAME, updateGame);
- }
- function startGame(evt:MouseEvent) {
- this.addEventListener(Event.ENTER_FRAME,moveBall);
- start_btn.visible=false;
- }
- private function updateGame(e:Event) {
- paddle_mc.moveIt();
- }
- function moveBall(e:Event) {
- ball_mc.y=ball_mc.y-yVel;
- ball_mc.x=ball_mc.x-xVel;
- if (ball_mc.x>this.stage.stageWidth || ball_mc.x<0) {
- xVel=xVel*-1;
- }
- if (ball_mc.y>this.stage.stageHeight || ball_mc.y<0) {
- yVel=yVel*-1;
- }
- }
- //Hitting the paddle
- if (Ball.hitTestObject(Paddle)) {
- calcBallAngle();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- package {
- import flash.display.MovieClip;
- import flash.events.*;
- import flash.events.KeyboardEvent;
- public class Paddle extends MovieClip {
- var left:Boolean;
- var right:Boolean;
- function Paddle() {
- this.stop();
- }
- function init() {
- this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- this.stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
- }
- private function keyDownHandler(e:KeyboardEvent):void {
- if (e.keyCode==37) {
- left=true;
- }
- if (e.keyCode==39) {
- right=true;
- }
- }
- private function keyUpHandler(e:KeyboardEvent):void {
- if (e.keyCode==37) {
- left=false;
- }
- if (e.keyCode==39) {
- right=false;
- }
- }
- function moveIt() {
- if (left==true) {
- this.x-=10;
- }
- if (right==true) {
- this.x+=10;
- }
- if (this.x <= 48) {
- this.x+=10;
- }
- if (this.x >= 500) {
- this.x-=10;
- }
- }
- }
- }