Wednesday, April 6, 2011

BLOCK BREAKER ACTIONSCRIPT CODE

function buildboard ()
{
_root.createEmptyMovieClip("board", 1);
board._x = 0;
board._y = 0;

grid = new Array();

bcnt = 0;
for (var j = 4; j < 9; j++)
{
grid[j] = new Array();
for (var i = 0; i < 10; i++)
{
var nm = "block_" + j + "_" + i;
board.attachMovie("block", nm, bcnt);
board[nm]._x = i * 32;
board[nm]._y = j * 16;

var btype = (j) % 4 + 1
board[nm].gotoAndStop(btype);
grid[j][i] = btype;

bcnt++;
}
}
}

paddle.onEnterFrame = function()
{
var xpos = _xmouse;
if (xpos > (320 - (this._width / 2)))
xpos = 320 - (this._width / 2);

if (xpos < (this._width / 2))
xpos = this._width / 2;

this._x = xpos;
}

fadercnt = 0;
function makeFader(x, y)
{
var nm = "fader" + fadercnt;
board.attachMovie("fader", nm, fadercnt + 9999);
board[nm]._x = x;
board[nm]._y = y;
score+=100;
score_txt.text = score;
fadercnt++;

if (fadercnt == bcnt)
{
Mouse.show();
lives_txt.text = "YOU WIN";
delete ball.onEnterFrame;
}
}

function ballAction()
{
this._x += this.dx;
this._y += this.dy;

if (this._x < 0)
{
this._x = 0;
this.dx *= -1;
}
if (this._x > 320)
{
this._x = 320;
this.dx *= -1;
}
if (this._y < 0)
{
this._y = 0;
this.dy *= -1;
}
if (this._y > 400)
{
// Lose a life
lives--;
lives_txt.text = lives;

if (lives == 0)
{
Mouse.show();
lives_txt.text = "GAME OVER";
delete ball.onEnterFrame;
}
else
{
this._x = 160;
this._y = 200;
this.dx = 0;
this.dy = 6;
}
}

if (this._y < paddle._y && (this._y + this.dy) > paddle._y && (this._x + this.dx) > paddle._x - 41 && (this._x + this.dx) < paddle._x + 41)
{
this.dy = -6;
this.dx = (this._x - paddle._x) / 5;
}

var grdx = Math.floor(this._x / 32);
var grdy = Math.floor(this._y / 16);
var ngrdx = Math.floor((this._x + this.dx) / 32);
var ngrdy = Math.floor((this._y + this.dy) / 16);

if (grid[grdy][ngrdx] > 0)
{
grid[grdy][ngrdx] = 0;
var bnm = "block_" + grdy + "_" + ngrdx;
board[bnm].removeMovieClip();
makeFader(ngrdx * 32, grdy * 16);
this.dx *= -1;
}

if (grid[ngrdy][grdx] > 0)
{
grid[ngrdy][grdx] = 0;
var bnm = "block_" + ngrdy + "_" + grdx;
board[bnm].removeMovieClip();
makeFader(grdx * 32, ngrdy * 16);
this.dy *= -1;
}
if (grid[ngrdy][ngrdx] > 0)
{
grid[ngrdy][ngrdx] = 0;
var bnm = "block_" + ngrdy + "_" + ngrdx;
board[bnm].removeMovieClip();
makeFader(ngrdx * 32, ngrdy * 16);
this.dy *= -1;
this.dx *= -1;
}
}

function startClick()
{
buildboard();
lives = 5;
score = 0;
score_txt.text = score;
lives_txt.text = lives;

ball.onEnterFrame = ballAction;
ball._x = 160;
ball._y = 200;
ball.dx = 0;
ball.dy = 6;

Mouse.hide();

}

startButton.addEventListener("click", startClick);

No comments:

Post a Comment