Collision detection problem in Actionscript 2.0 - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Collision detection problem in Actionscript 2.0

Hi,

I'm coding an Arkanoid clone in Flash CS3 using AS 2.0 and I can't get the collision detection to work properly. It only works for far right side of the brick wall?!?!

Here is the code:

[code]
class Ball extends MovieClip
{
var xVelocity;
var yVelocity;
var ballLeft;
var ballRight;
var ballUp;
var ballDown;
var wall;

function onLoad()
{
xVelocity = 3;
yVelocity = 3;
ballLeft = 0;
ballRight = 1;
ballUp = 1;
ballDown = 0;
}

function onEnterFrame()
{
var i, j
for(j = 0; j <= 7; j++)
{
for(i = 0; i <= 11; i++)
{
if(this.hitTest(_root.bat.wall[i][j]))
{
_root.bat.wall[i][j].removeMovieClip();
}
}
}

if((_x > 543) and (ballUp == 1) and (ballRight == 1))
{
_x = _x - xVelocity;
ballLeft = 1;
ballRight = 0;
}
else if((ballUp == 1) and (ballRight == 1))
{
_x = _x + xVelocity;
ballRight = 1;
ballLeft = 0;
}
else if((_x > 543) and (ballDown == 1) and (ballRight == 1))
{
_x = _x - xVelocity;
ballLeft = 1;
ballRight = 0;
}
else if((this.hitTest(_root.bat)) and (ballDown == 1) and (ballRight == 1))
{
_x = _x + xVelocity;
ballRight = 1;
ballLeft = 0;
}
else if((this.hitTest(_root.bat)) and (ballDown == 1) and (ballLeft == 1))
{
_x = _x - xVelocity;
ballLeft = 1;
ballRight = 0;
}
else if((_x < 7) and ((ballUp == 1) or (ballDown == 1)) and (ballLeft == 1))
{
_x = _x + xVelocity;
ballLeft = 0;
ballRight = 1;
}
else if((ballDown == 1) and (ballRight == 1))
{
_x = _x + xVelocity;
ballLeft = 0;
ballRight = 1;
}
else if((ballDown == 1) and (ballLeft == 1))
{
_x = _x - xVelocity;
ballLeft = 1;
ballRight = 0;
}
else if((ballUp == 1) and (ballLeft == 1))
{
_x = _x - xVelocity;
ballLeft = 1;
ballRight = 0;
}
else if((ballUp == 1) and (ballRight == 1))
{
_x = _x + xVelocity;
ballLeft = 0;
ballRight = 1;
}
else
{
// do nothing
}


if((_y < 7) and (ballUp == 1) and (ballRight == 1))
{
_y = _y + yVelocity;
ballUp = 0;
ballDown = 1;
}
else if((ballUp == 1) and (ballRight == 1))
{
_y = _y - yVelocity;
ballUp = 1;
ballDown = 0;
}
else if((_x > 543) and (ballDown == 1) and (ballRight == 1))
{
_y = _y + yVelocity;
ballDown = 1;
ballUp = 0;
}
else if((this.hitTest(_root.bat)) and (ballDown == 1) and ((ballRight == 1) or (ballLeft == 1)))
{
_y = _y - yVelocity;
ballUp = 1;
ballDown = 0;
}
else if((ballDown == 1) and (ballRight == 1))
{
_y = _y + yVelocity;
ballDown = 1;
ballUp = 0;
}
else if((ballDown == 1) and (ballLeft == 1))
{
_y = _y + yVelocity;
ballDown = 1;
ballUp = 0;
}
else if((_y < 7) and ((ballUp == 1) or (ballDown == 1)) and (ballLeft == 1))
{
_y = _y + yVelocity;
ballDown = 1;
ballUp = 0;
}
else if((ballUp == 1) and (ballLeft == 1))
{
_y = _y - yVelocity;
ballUp = 1;
ballDown = 0;
}
else if((ballUp == 1) and (ballRight == 1))
{
_y = _y - yVelocity;
ballUp = 1;
ballDown = 0;
}
else
{
// do nothing
}
}

}
[/code]

[code]
class Bat extends MovieClip
{
var xPos = 25;
var yPos = 50;
var wall;

function onLoad()
{
var sB = new Array(0, 0, 0, 0, 0, 0, 0, 0);
wall = new Array(sB, sB, sB, sB, sB, sB, sB, sB, sB, sB, sB, sB)

for(var x = 0; x <= 11; x++)
{
for(var y = 0; y <= 7; y++)
{
wall[x][y] = _root.attachMovie("Brick", "Brick" + _root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x: xPos, _y: yPos});
yPos = yPos + 25;
}
xPos = xPos + 45;
yPos = 50;
}
}

/*
function buildWallX(xPos)
{
for(var i = 1; i < 4; i++)
{
var buildWall = _root.attachMovie("Brick", "Brick" + _root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x: xPos, _y: yPos});
wall.push(buildWall);
xPos = xPos + 45;
}
}
*/

function onEnterFrame()
{
if(Key.isDown(Key.LEFT))
{
if(_x < 49.5)
{
// do nothing
}
else
{
_x = _x - 10;
}
}

if(Key.isDown(Key.RIGHT))
{
if(_x > 500.5)
{
// do nothing
}
else
{
_x = _x + 10;
}
}
}
}
[/code]

Thanks.
Sign In or Register to comment.