View Single Post
  #1  
Old 01-22-2012, 06:39 PM
iBeatz iBeatz is offline
Kavan
iBeatz's Avatar
Join Date: Dec 2010
Location: Northern Ireland, UK
Posts: 154
iBeatz will become famous soon enough
Send a message via Yahoo to iBeatz
Collision check script error

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(10000001);
  
changeImgVis(10005);
  
  
// 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(10011111);
  
changeImgVis(10016);
  
  
// 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(10021111);
  
changeImgVis(10026);
  
  
// Central white divider
  
showPoly(1003, {(screenwidth/2), (screenheight/2)-(temp.court_height/2),
                  (
screenwidth/2), (screenheight/2)+(temp.court_height/2)-1});
  
changeImgColors(10031111);
  
changeImgVis(10036);
  
  
// 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(10041111);
  
changeImgVis(10047);
  
  
scheduleEvent(0.5"StartPong"NULL);
}

function 
onStartPong(){

  
temp.ball_angle random(0360);
  
  
onMoveBall(temp.ball_angle);
}

function 
onMoveBall(temp.angle){

  
cancelEvents("MoveBall");
  
  if(
int(temp.angle 90) == 0){
    
temp.angle += random(-1010);
  }
  
  
// Ball collision detection and angle adjustment
  
for(temp.iy 1temp.iy <= 7temp.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]-10findImg(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]-10findImg(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 0temp.bx <= 6temp.bx += 2){
    
findImg(1004).polygon[temp.bx] += sin(degtorad(temp.angle)) * BALL_SPEED;
  }
  for(
temp.by 1temp.by <= 7temp.by += 2){
    
findImg(1004).polygon[temp.by] -= cos(degtorad(temp.angle)) * BALL_SPEED;
  }
  
  
showText(1005findImg(1004).polygon[0], findImg(1004).polygon[1] - 20"Arial""c"temp.angle);
  
changeImgVis(10057);
  
  if(
this.game_active){
    
scheduleEvent(0.05"MoveBall"temp.angle);
  }
}

function 
onClosePong(){

  
hideImgs(10001004);

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.
__________________

Intelligence without ambition is like a bird without wings.

Reply With Quote