I'm having a problem constructing collision checks for a replica of the classic Pong game.
I'll post all of the code before I explain the problem:
PHP Code:
//#CLIENTSIDE
const BALL_SPEED = 6;
function onWeaponFired(){
this.game_active = !this.game_active;
if(this.game_active){
onInitiatePong();
}
else {
onClosePong();
}
}
function onInitiatePong(){
// Scaling of game screen and objects
temp.court_width = 500;
temp.court_height = 350;
temp.paddle_width = 20;
temp.paddle_height = 100;
temp.ball_width = 16;
temp.ball_height = 16;
// Black background
showPoly(1000, {(screenwidth/2)-(temp.court_width/2), (screenheight/2)-(temp.court_height/2),
(screenwidth/2)+(temp.court_width/2), (screenheight/2)-(temp.court_height/2),
(screenwidth/2)+(temp.court_width/2), (screenheight/2)+(temp.court_height/2),
(screenwidth/2)-(temp.court_width/2), (screenheight/2)+(temp.court_height/2)});
changeImgColors(1000, 0, 0, 0, 1);
changeImgVis(1000, 5);
// Left white paddle
showPoly(1001, {(screenwidth/2)-(temp.court_width/2)+10, (screenheight/2)-(temp.paddle_height/2),
(screenwidth/2)-(temp.court_width/2)+10+temp.paddle_width, (screenheight/2)-(temp.paddle_height/2),
(screenwidth/2)-(temp.court_width/2)+10+temp.paddle_width, (screenheight/2)+(temp.paddle_height/2),
(screenwidth/2)-(temp.court_width/2)+10, (screenheight/2)+(temp.paddle_height/2)});
changeImgColors(1001, 1, 1, 1, 1);
changeImgVis(1001, 6);
// Right white paddle
showPoly(1002, {(screenwidth/2)+(temp.court_width/2)-10, (screenheight/2)-(temp.paddle_height/2),
(screenwidth/2)+(temp.court_width/2)-10-temp.paddle_width, (screenheight/2)-(temp.paddle_height/2),
(screenwidth/2)+(temp.court_width/2)-10-temp.paddle_width, (screenheight/2)+(temp.paddle_height/2),
(screenwidth/2)+(temp.court_width/2)-10, (screenheight/2)+(temp.paddle_height/2)});
changeImgColors(1002, 1, 1, 1, 1);
changeImgVis(1002, 6);
// Central white divider
showPoly(1003, {(screenwidth/2), (screenheight/2)-(temp.court_height/2),
(screenwidth/2), (screenheight/2)+(temp.court_height/2)-1});
changeImgColors(1003, 1, 1, 1, 1);
changeImgVis(1003, 6);
// Centre-starting ball
showPoly(1004, {(screenwidth/2)-(temp.ball_width/2), (screenheight/2)-(temp.ball_height/2),
(screenwidth/2)+(temp.ball_width/2), (screenheight/2)-(temp.ball_height/2),
(screenwidth/2)+(temp.ball_width/2), (screenheight/2)+(temp.ball_height/2),
(screenwidth/2)-(temp.ball_width/2), (screenheight/2)+(temp.ball_height/2)});
changeImgColors(1004, 1, 1, 1, 1);
changeImgVis(1004, 7);
scheduleEvent(0.5, "StartPong", NULL);
}
function onStartPong(){
temp.ball_angle = random(0, 360);
onMoveBall(temp.ball_angle);
}
function onMoveBall(temp.angle){
cancelEvents("MoveBall");
if(int(temp.angle % 90) == 0){
temp.angle += random(-10, 10);
}
// Ball collision detection and angle adjustment
for(temp.iy = 1; temp.iy <= 7; temp.iy += 2){
if(findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED < findImg(1000).polygon[1] ||
findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED > findImg(1000).polygon[7]){
temp.angle = 180 - temp.angle;
}
// First collision check
if(findImg(1004).polygon[temp.iy-1] + sin(degtorad(temp.angle)) * BALL_SPEED in |findImg(1001).polygon[0], findImg(1001).polygon[2]|){
if(findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED in |findImg(1001).polygon[7]-10, findImg(1001).polygon[7]| ||
findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED in |findImg(1001).polygon[1], findImg(1001).polygon[1]+10|){
temp.angle = 180 - temp.angle;
}
// Second collision check
// MY PROBLEM IS THE LINE BELOW
else if(findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED in |findImg(1002).polygon[7]-10, findImg(1002).polygon[7]| ||
findImg(1004).polygon[temp.iy] - cos(degtorad(temp.angle)) * BALL_SPEED in |findImg(1002).polygon[1], findImg(1002).polygon[1]+10|){
temp.angle = 180 - temp.angle;
}
}
}
// Moving the ball along angle
for(temp.bx = 0; temp.bx <= 6; temp.bx += 2){
findImg(1004).polygon[temp.bx] += sin(degtorad(temp.angle)) * BALL_SPEED;
}
for(temp.by = 1; temp.by <= 7; temp.by += 2){
findImg(1004).polygon[temp.by] -= cos(degtorad(temp.angle)) * BALL_SPEED;
}
showText(1005, findImg(1004).polygon[0], findImg(1004).polygon[1] - 20, "Arial", "c", temp.angle);
changeImgVis(1005, 7);
if(this.game_active){
scheduleEvent(0.05, "MoveBall", temp.angle);
}
}
function onClosePong(){
hideImgs(1000, 1004);
}
If you scroll about halfway down, you'll get to the collision checks under the onMoveBall function, you'll see where I've highlighted my problem.
When the script is as it is in this post, the first collision check works fine but the second check does not, and RC gives no error messages about the script.
However, if you add a line before the second collision check such as this:
PHP Code:
player.chat = "blah";
RC then gives an error message about the line below, causing the whole collision detection to stop working.
Thanks.