Hi, can anyone help Im making a simple breakout game in AS3.0 Im trying desperately to learn figure out how to get the collisons working with the blocks. it works on some blocks some times. Ive tried everything I can think of!
testBlocksCollisions() is the function that wont work.
I have a clip called ball on the stage a clip called bat and block1-9 on the stage. Please help but if you could also explain where I was going wrong that would be immense.
this is my code
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.display.Stage;
import flash.events.Event;
import flash.events.*;
import flash.display.*;
public class Main extends MovieClip {
public var gravity:Number = 1;
public var restitution:Number = 0.6;
public var friction:Number = 0.9;
public var velX = -5;
public var velY = 5;
public var numberOfBlocks = 10;
function Main() {
init();
}
function init() {
stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(Event.ENTER_FRAME,gameLoop);
}
function keyDownHandler(kb:KeyboardEvent) {
if ((bat.x>0)&&(bat.x<300)) {
if (kb.keyCode==37) {
bat.x-=10;
kb.updateAfterEvent();
}
if (kb.keyCode==39) {
bat.x+=10;
kb.updateAfterEvent();
}
}
}
function testBlocksCollisions() {
for (var i:int = 0; i<10; i++) {
if (getChildByName('block'+i)!=null) {
var myClip:MovieClip = getChildByName('block'+i) as MovieClip;
var myBottom = myClip.y//-(myClip.height);
var myTop = myClip.y;
var myRight= myClip.x+myClip.width;
var myLeft = myClip.x;
var myClipX = myClip.x;
var myClipY = myClip.y;
var ballY = ball.y;
var ballX = ball.x;
var radius = ball.width/2;
var distanceY = myClipY-(ballY+radius);
var timeHitY = distanceY / velY;
var distanceX = myClipX-(ballX+radius);
var timeHitX = distanceX / velX;
if (timeHitX>=0&&timeHitX<1) {
trace('((ballY>myTop)&&(ballY<myBottom)): ' +((ballY>myTop)&&(ballY<myBottom-20)))
if ((ballY>myTop)&&(ballY<myBottom)) {
trace('ballX: ' + ballX);
trace('myLeft: ' + myLeft);
if ((ballX<myLeft)) {
trace('.............................Left.........................');
ballX += (velX*timeHitX);
removeChild(myClip);
velX*= -1;
return;
}
if ((ballX+(myRight.height)>=myRight)) {
trace('.............................Right.............................');
ballX += (velX*timeHitX);
velX*= -1;
removeChild(myClip);
return;
}
}
}
if ((timeHitY>=0)&&(timeHitY<=1)) {
trace('HITY-----------------------')
if (ballX>myLeft&&ballX<myRight) {
if ((ballY+(ball.height/2)>=myBottom)) {
trace('**********************************Bottom**********************************');
ballY += (velY*timeHitX);
velY*= -1;
removeChild(myClip);
return;
}
if ((ballY<=myTop)) {
trace('ballY: ' + ballY);
trace('myBottom: ' + myBottom);
trace('**********************************Top**********************************');
ballY += (velY*timeHitX);
velY*= -1;
removeChild(myClip);
return;
}
}
}
}
}
}
function gameLoop(e:Event) {
//findNumberOfBlocks();
moveBall();
checkCollisionBallLeftWall();
checkCollisionBallRightWall();
collisionWithBat();
checkCollisionWithTop();
testBlocksCollisions();
}
function moveBall() {
ball.x+=velX;
ball.y+=velY;
}
function checkCollisionWithTop() {
if (ball.y<4) {
velY*=-1;
}
}
function collisionWithBat() {
var batY = bat.y;
var ballY = ball.y;
var radius = ball.width/2;
var distance = batY-(ballY+radius);
var timeHit = distance / velX;
if ((timeHit>=0)&&(timeHit<1)) {
ballY += (velY*timeHit);
if ((ball.x>=bat.x)&&(ball.x<=bat.x+bat.width)) {
velY*= -1;
}
}
}
function checkCollisionBallRightWall() {
var wallX = rightWall.x;
var ballX = ball.x;
var radius = ball.width/2;
var distance = wallX-(ballX+radius);
var timeHit = distance / velX;
if ((timeHit>=0)&&(timeHit<1)) {
ballX += (velX*timeHit);
velX*= -1;
}
}
function checkCollisionBallLeftWall() {
var wallX = leftWall.x;
var ballX = ball.x;
var radius = ball.width/2;
var distance = wallX-(ballX+radius);
var timeHit = distance / velX;
if ((timeHit>=0)&&(timeHit<1)) {
ballX += (velX*timeHit);
velX*= -1;
}
}
}
}