Hello all,
I’m working on an air hockey type game with Flash and ActionScript 3.0. I’m having a few problems and was hoping you guys could help me out. Mainly having to do with the hit detection and bouncing off the paddle at the correct angle. So far I’ve been able to get it mostly working, my problem is that it will only work in one direction, if you’re moving the paddle to the right and hit the puck, it will bounce off at the same angle you were moving at. However, if you are moving the paddle to the left, it goes to the right. Another problem I was having involves multiple hits, or when the puck gets stuck on the paddle. It tries to calculate the angle and if you’re not moving the paddle it kind of blows up. I was trying to get it to only detect the first hit, I tried a couple things, removing the listener and then adding it back with a timer, so you can hit again. That didn’t really seem to have any effect.
Any help would be appreciated.
Here is the code for the main doc:
public class AirHockeyDoc extends MovieClip
{
private var playerPaddle:Paddle;
private var computerPaddle:Paddle;
private var thePuck:Puck;
private var angle:Number;
private var velocity:Number;
private var initialX:Number;
private var initialY:Number;
private var destX:Number;
private var destY:Number;
public function AirHockeyDoc()
{
//Add paddles to stage
playerPaddle = new Paddle("red", 50, 200);
computerPaddle = new Paddle("blue", 450, 200);
this.addChild(playerPaddle);
this.addChild(computerPaddle);
//Move the paddle
this.addEventListener(Event.ENTER_FRAME, paddleMove);
//Add puck to stage
thePuck = new Puck(280, 225, 5);
this.addChild(thePuck);
//Check to see if paddle hit the puck
playerPaddle.addEventListener(Event.ENTER_FRAME, puckHit);
//Timer for angle calc
var myTimer:Timer = new Timer(100);
myTimer.addEventListener(TimerEvent.TIMER, angleCalc);
myTimer.start();
}
public function angleCalc(e:Event)
{
initialX = stage.mouseX;
initialY = stage.mouseY;
}
//Let user move the paddle with the mouse
public function paddleMove(e:Event)
{
playerPaddle.userMove(stage.mouseX, stage.mouseY);
}
//When a player hits the puck with the paddle
public function puckHit(e:Event)
{
if(playerPaddle.hitTestObject(thePuck))
{
destX = stage.mouseX;
destY = stage.mouseY;
velocity = Math.sqrt((Math.pow((destY-initialY), 2) + Math.pow((destX-initialX), 2)))
angle = Math.atan((destY-initialY)/(destX-initialX));
trace(angle);
thePuck.moveMe(angle, velocity);
}
}
This is the code for the puck movement:
package code
{
import flash.display.*;
import flash.events.*;
public class Puck extends MovieClip
{
private var speed:Number;
private var startPuck:Number = 0;
private var xPos:Number;
private var yPos:Number;
private var xVel:Number;
private var yVel:Number;
private var radius:Number;
private var acceleration:Number = 1;
public function Puck(myX:Number, myY:Number, _speed:Number)
{
this.x = myX;
this.y = myY;
speed = _speed;
radius = this.width/2;
xPos = this.x;
yPos = this.y;
xVel = (Math.random()*10)-10;
yVel = (Math.random()*10)-10;
this.addEventListener(Event.ENTER_FRAME, contMove);
}
public function moveMe(angle:Number, velocity:Number)
{
acceleration = 1;
xVel*=-1;
yVel*=-1;
xVel+=(Math.cos(angle)*speed);
yVel+=(Math.sin(angle)*speed);
xPos+=xVel;
yPos+=yVel;
this.x=xPos;
this.y=yPos;
}
public function contMove(e:Event):void
{
acceleration -=.01;
yVel*=acceleration;
xVel*=acceleration;
xPos+=xVel;
yPos+=yVel;
if (yPos > (400 - radius)) {
yPos = (400 - radius);
yVel *= -1;
}
else if(yPos < (53 + radius))
{
yPos = (53+radius);
yVel *= -1;
}
if (xPos > (550 - radius)) {
xPos = (550 - radius);
xVel *= -1;
}
else if(xPos < (0 + radius))
{
xPos = (0+radius);
xVel *= -1;
}
this.x = xPos;
this.y = yPos;
}
}
}
Thank you.