Hi,

Im a newbie to flash and have an issue regarding the collision within my pong game (pong but using football graphics). The men are placed on the top & bottom of the screen.

The collision detection has problems as the ball symbol rotates and hits either the CPU symbol or Player symbol. Sometimes the collision detection gets stuck in a loop, the ball Y co-ordinates appears not to clear the hit test area and loops around the code.

The issues dont occur all the time and are more apparent if the ball hits the sides of the football player.

The collision code using hittest is below (assigned to ball symbol):

if (this.hitTest(_root.P1footballman)) {
 
ang = (this._x-_root.P1footballman._x)/5;
this.xspeed += ang; //and use it as the rebound angle
this.yspeed = this.yspeed*-1;
 
trace("X= " + this._x + " Y= " + this._y);
 
//play football kick & header sound
_root.footballkickorheader.start();
 
}

xspeed & yspeed are set to 3 within the main frame.

Also within the ball script is the following code used to move the ball

if (randNumber1>0) {
  this._y += this.yspeed;
  //move it on the y axis at the set speed
 } else {
  this._y -= this.yspeed;
  //move it on the y axis at the set speed
 }
 if (randNumber2>0) {
  this._x += this.xspeed;
  //move it on the x axis at the set speed
 } else {
  this._x -= this.xspeed;
  //move it on the x axis at the set speed
 }

Random number 1 & 2 are variables used to generate values of 1 or -1 of x,y to allow the ball to move in One of four diagnal directions during 'Kick off' at beginning of game or when someone scores.

The following code is used to rotate the ball (again attached to ball symbol)

i = getProperty(this, _rotation);
 setProperty(this, _rotation, i+5);
 _this.rotation = i;

I dont believe the roation is the issue rather than when the collision detection occurs the ball is moved the value of yspeed either 3 or -3 and it doesnt clear the collision area and is automatically detected again.

Hope this makes sense, also recorded output from the trace command showing when the ball and man repeatidly collide and the ball is stuck to the man in a loop.

X= 617.1 Y= 88.4
X= 616.6 Y= 85.4
X= 616.25 Y= 88.4
X= 616.1 Y= 85.4
X= 616.15 Y= 88.4
X= 616.4 Y= 85.4
X= 616.8 Y= 88.4
X= 617.3 Y= 85.4
X= 617.8 Y= 88.4

I could always post a link to my code if anyone thinks they can help.

Thanks
Rob

I now understand why this is happening as during the collision the code

this.yspeed = this.yspeed*-1;

Results in the following:-

this.yspeed = this.y (y location of ball symbol)
this.yspeed = 3 or -3 depending which direction the ball is travelling up or down.

The hittest simply revers the direction using yspeed*-1 however when it loops around again the ball symbol has not cleared the collision area as it needs to be moved more than 3 pixels away.

Therefore loopig moving the Y lcoation of the 3 pixels up/down, up/down etc.., as it never clears the collision area and executes the hittest code.

Can anyone please help?
Im looking int getbounds a I believe this may be one solution.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.