Arutha Studios
11-26-2007, 03:17 PM
Hey people, im making a pong game in classes, I was just wondering how I will go about making the ball bounce off the paddles, im asking this because the paddles are in another class and not in the same .as file as the ball.
Heres the .as files
One of the paddles
package classes.bats{
import flash.display.Sprite;
public class Rightbat extends Sprite{
public function Rightbat(){
graphics.lineStyle(1);
graphics.beginFill(0x2a00ff);
graphics.drawRect(774,250,25,100);
}
}
}
and here the ball .as file
package classes.ball{
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Ball extends Sprite
{
private var myBall:BallClip;
public function Ball( )
{
myBall = new BallClip( );
addChild( myBall );
var moveTimer:Timer = new Timer( 25 );
moveTimer.addEventListener( TimerEvent.TIMER , myBall.step );
moveTimer.start( );
}
}
}
import flash.display.Sprite;
import flash.events.TimerEvent;
class BallClip extends Sprite
{
private var xspeed:Number;
private var yspeed:Number;
public function BallClip ( )
{
graphics.lineStyle(1);
graphics.beginFill( 0xffffff , 1 );
graphics.drawCircle( 20 , 20 , 15 );
graphics.endFill( );
x = 400;
y = 300;
xspeed = Math.random( )*5;
yspeed = Math.random( )*3;
}
public function step ( timeEvent:TimerEvent ):void
{
if ( x + xspeed > 850 ) xspeed *= -1;
if ( x + xspeed < -50 ) xspeed *= -1;
if ( y + yspeed > 570 ) yspeed *= -1;
if ( y + yspeed < 0 ) yspeed *= -1;
x += xspeed;
y += yspeed;
}
}
Heres the .as files
One of the paddles
package classes.bats{
import flash.display.Sprite;
public class Rightbat extends Sprite{
public function Rightbat(){
graphics.lineStyle(1);
graphics.beginFill(0x2a00ff);
graphics.drawRect(774,250,25,100);
}
}
}
and here the ball .as file
package classes.ball{
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Ball extends Sprite
{
private var myBall:BallClip;
public function Ball( )
{
myBall = new BallClip( );
addChild( myBall );
var moveTimer:Timer = new Timer( 25 );
moveTimer.addEventListener( TimerEvent.TIMER , myBall.step );
moveTimer.start( );
}
}
}
import flash.display.Sprite;
import flash.events.TimerEvent;
class BallClip extends Sprite
{
private var xspeed:Number;
private var yspeed:Number;
public function BallClip ( )
{
graphics.lineStyle(1);
graphics.beginFill( 0xffffff , 1 );
graphics.drawCircle( 20 , 20 , 15 );
graphics.endFill( );
x = 400;
y = 300;
xspeed = Math.random( )*5;
yspeed = Math.random( )*3;
}
public function step ( timeEvent:TimerEvent ):void
{
if ( x + xspeed > 850 ) xspeed *= -1;
if ( x + xspeed < -50 ) xspeed *= -1;
if ( y + yspeed > 570 ) yspeed *= -1;
if ( y + yspeed < 0 ) yspeed *= -1;
x += xspeed;
y += yspeed;
}
}