-
I'm novice. Please help me with it...
Hello!
I have Adobe Flash CS6 and I'm starting to use AS3.0.
I'm doing a simple Breakout style game (Only one level). This is the source code for AS3.0:
import flash.events.KeyboardEvent;
import flash.events.Event;
//Paddle motion flags
var moveLeft:Boolean=false;
var moveRight:Boolean=false;
var ballXSpeed:Number=8; //Horizontal ball speed.
var ballYSpeed:Number=8; //Ball vertical speed.
var vector:Vector.<brick> = new Vector.<brick>();
//Calls createBricks function (To create the bricks).
createBricks();
//Event listeners for keys states
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, movePaddle);
//Adds an event listener to the ball to call the motion function.
ball.addEventListener(Event.ENTER_FRAME, moveBall);
//Function keyPressed:
//Checks if arrow keys LEFT and Right are pressed and turn the motion paddle
//flags to true. It returns a void value.
function keyPressed(key:KeyboardEvent):void
{
if(key.keyCode==Keyboard.LEFT)
{
moveLeft=true;
}
if(key.keyCode==Keyboard.RIGHT)
{
moveRight=true;
}
}
//Function keyReleased:
//Opposite actions... It returns a void value too.
function keyReleased(key:KeyboardEvent):void
{
if(key.keyCode==Keyboard.LEFT)
{
moveLeft=false;
}
if(key.keyCode==Keyboard.RIGHT)
{
moveRight=false;
}
}
//Function movePaddle:
//Checks if the motion flags of the paddle are "true" and moves the paddle
//in the correct directions. It returns a void value. It's called by the
//stage event listener ENTER_FRAME.
function movePaddle(e:Event):void
{
if(moveLeft)
{
paddle.x-=15;
}
if(moveRight)
{
paddle.x+=15;
}
//Limits stage edges...
if(paddle.x<=0)
{
paddle.x=0;
}
if(paddle.x>=stage.stageWidth-paddle.width)
{
paddle.x=stage.stageWidth-paddle.width;
}
}
//Function moveBall:
//Sets and controls the ball motion. It returns a void value.
function moveBall(e:Event):void
{
ball.x+=ballXSpeed; //Horizontal initial motion
ball.y+=ballYSpeed; //Vertical initial motion
//Checks stage edges and makes the ball to bounce off edges
if(ball.x<=0)
{
ballXSpeed*=-1;
}
if(ball.x>=stage.stageWidth-ball.width)
{
ballXSpeed*=-1;
}
if(ball.y<=0)
{
ballYSpeed*=-1;
}
//Makes the ball to bounce off the paddle (Calls a custom function!)
if(ball.hitTestObject(paddle))
{
bounceAgainstPaddle();
}
for(var i:int=0; i<vector.length; i++)
{
if(ball.hitTestObject(vector[i]))
{
stage.removeChild(vector[i]);
vector.splice(i,1);
ballYSpeed*=-1;
}
}
}
//Function bounceAgainstPaddle:
//Makes the ball to bounce off the paddle correctly.
//It returns a void value.
function bounceAgainstPaddle():void
{
var ballPosition:Number=ball.x-paddle.x;
var hitPercent:Number=(ballPosition /(paddle.width-ball.width))-.5;
ballXSpeed=hitPercent*10;
ballYSpeed*=-1;
}
//Function createBricks:
//Creates the bricks on screen. It returns a void value.
function createBricks():void
{
for(var i:int=0; i<6; i++)
{
for(var j:int=0; j<11; j++)
{
var newBrick:brick = new brick();
newBrick.x=i*57+30;
newBrick.y=j*25+30;
vector.unshift(newBrick)
stage.addChild(vector[0]);
}
}
}
I want to know:
a)- How to check if the player destroyed all the bricks in order to win the game?. I want to do it: If the player breaks all bricks on screen, a congratulation screen will appear.
b)- How to restart the game when player lets the ball fall?. I have to ckeck if(ball.y>=stage.stageHeight), but I don't know what actions have I to put in order to restart all the game.
Please, help me. Thanks! - Nicolás
-
Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
var ballXSpeed:Number = 8;
var ballYSpeed:Number = 8;
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var vector:Vector.<brick> = new Vector.<brick>();
var sWidth:int = stage.stageWidth;
var baseX:int = ball.x;
var baseY:int = ball.y;
initGame();
function initGame():void {
trace("new game");
ball.x = baseX;
ball.y = baseY;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, movePaddle);
ball.addEventListener(Event.ENTER_FRAME, moveBall);
createBricks();
}
function keyPressed(key:KeyboardEvent):void {
if (key.keyCode == Keyboard.LEFT) {
moveLeft = true;
}
if (key.keyCode == Keyboard.RIGHT) {
moveRight = true;
}
}
function keyReleased(key:KeyboardEvent):void {
if (key.keyCode == Keyboard.LEFT) {
moveLeft = false;
}
if (key.keyCode == Keyboard.RIGHT) {
moveRight = false;
}
}
function movePaddle(e:Event):void {
if (moveLeft) {
paddle.x -= 15;
}
if (moveRight) {
paddle.x += 15;
}
if (paddle.x <= 0) {
paddle.x = 0;
}
if (paddle.x >= sWidth - paddle.width) {
paddle.x = sWidth - paddle.width;
}
}
function moveBall(e:Event):void {
ball.x += ballXSpeed;
ball.y += ballYSpeed;
if (ball.x <= 0) {
ballXSpeed *= -1;
}
if (ball.x >= sWidth - ball.width) {
ballXSpeed *= -1;
}
if (ball.y <= 0) {
ballYSpeed *= -1;
}
if (ball.hitTestObject(paddle)) {
bounceAgainstPaddle();
}
for (var i:int=0; i<vector.length; i++) {
if (ball.hitTestObject(vector[i])) {
stage.removeChild(vector[i]);
vector.splice(i,1);
ballYSpeed *= -1;
}
}
if (vector.length == 0) {
removeListeners()
trace("you won");
}
if (ball.y > paddle.y + paddle.height + ball.height) {
trace("lost ball");
removeListeners()
while (vector.length > 0) {
stage.removeChild(vector[vector.length-1]);
vector.splice(vector.length-1,1);
}
initGame();
}
}
function removeListeners():void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.removeEventListener(Event.ENTER_FRAME, movePaddle);
ball.removeEventListener(Event.ENTER_FRAME, moveBall);
}
function bounceAgainstPaddle():void {
var ballPosition:Number = ball.x - paddle.x;
var hitPercent:Number=(ballPosition /(paddle.width-ball.width))-.5;
ballXSpeed = hitPercent * 10;
ballYSpeed *= -1;
}
function createBricks():void {
for (var i:int=0; i<6; i++) {
for (var j:int=0; j<11; j++) {
var newBrick:brick = new brick();
newBrick.x = i * 57 + 30;
newBrick.y = j * 25 + 30;
vector.unshift(newBrick);
stage.addChild(vector[0]);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|