I am a beginner with AS3 and I am trying to make a basic Player vs CPU pong game(using a tutorial as reference). I apologize if this sounds stupid or obvious. I have a document class and individual classes for Ball, Player and CPU. My problem is that I don't know how to make CPU class use the co-ordinates of the movie clip ball on the stage so that it can move with respect to the ball as required to form the AI. The tutorial I have been referring has all the code for ball, player and cpu in the document class only but I have written code for everything in their respective classes.
The tutorial link http://as3gametuts.com/2011/03/19/pong-1/
My version of code. Currently the ball is bouncing off the walls but HitTest has not been applied to anything. The player paddle is moving with the arrow keys. No warnings or errors are being displayed.
Main
public class Main extends MovieClip
{
public function Main()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
trace("Initialized");
}
}
Ball
public class Ball extends MovieClip
{
public var ballSpeedX:int = 5;
public var ballSpeedY:int = 6;
public function Ball()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
}
public function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
this.x = stage.stageWidth / 2;
this.y = stage.stageHeight / 2;
this.addEventListener(Event.ENTER_FRAME, loop)
}
public function loop(e:Event):void
{
this.y += ballSpeedY;
this.x += ballSpeedX;
if ((this.y <= 0 + this.height/2) || (this.y >= stage.stageHeight - this.height/2))
{
ballSpeedY *= -1;
}
else if ((this.x <= 0 + this.width/2) || (this.x >= stage.stageWidth - this.width/2))
{
ballSpeedX *= -1;
}
}
}
TheCpu
public class TheCpu extends MovieClip
{
public var cpu:TheCpu;
public var ball:Ball;
private var vx:Number = 5;
public function TheCpu()
{
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.x = 240;
this.y = 10;
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
/*if (this.x < ball.x - 10)
{
this.x += vx;
}
else if (this.x > ball.x + 10)
{
this.x -= vx;
}*/
}
}
I added this to my main class
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
trace("It's Alive!");
addBall();
addCPU();
}
public function addBall():void
{
var ball:MovieClip = stage.getChildByName("ball") as MovieClip
stage.addChild(ball);
}
public function addCPU():void
{
var cpu:MovieClip = stage.getChildByName("cpu") as MovieClip
stage.addChild(cpu);
}
}
but now it is giving error TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild() at flash.display::Stage/addChild() at src::Main/addBall() at src::Main/onAddedToStage()
and if I use
var ball:Ball = new Ball();
var cpu:TheCpu = new TheCpu();
I get TypeError: Error #1009: Cannot access a property or method of a null object reference. at src::TheCpu() at src::Main/addCPU() at src::Main/onAddedToStage()
I think I am being really dumb now.