-1

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.

Rakesh
  • 1
  • 1
  • Try reading over http://www.kirupa.com/forum/showthread.php?223798-ActionScript-3-Tip-of-the-Day/page28&p=2167721#post2167721 . In summary, you will have to use either events or references. If you are just beginning you probably haven't learned yet to how to create and dispatch your own events (or use references, for that matter). So instead, I recommend that you put all your code in your document class for now, and use classes once you learn more about OOP and how to communicate between classes. – Garry Wong Jun 06 '13 at 20:08
  • I disagree with Garry, unless you're in a time crunch take the time now to learn how to use events and listeners. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html The main thing to understand is events come from some dispatcher and you listen for events on that dispatcher. When the dispatcher dispatches an event any functions registered for that event will be triggered. Also if you're using stuff on the stage events bubble from child to parent. Point out the tutorial or show your code for more help. – shaunhusain Jun 06 '13 at 20:15

2 Answers2

0

As a simpler solution.

Basically when you create the ball you could pass the instance of the ball to the CPU so that the CPU can just check where the ball is on each update/enter frame. Something like this:

var ball:Ball = new Ball();
var cpu:CPU = new CPU(ball);

I think ideally you would have an update loop that just tells each part to update itself in sequence and passes along any important information between the objects so they can update themselves appropriately (and the main update loop can track things like ball out of region/point scored etc.)

EDIT

If I understand you correctly you've placed symbols on the stage in Flash and have assigned these code blocks to each of them. So your issue is you don't know how to reference each of the instances. You could use this method to retrieve the instances you put on the stage by naming them and referring to them by name: How do I access a movieClip on the stage using as3 class?

Alternatively you could create the instances in the Main class in the added to Stage handler by creating instances as shown in the code block above then calling: stage.addChild(cpu); stage.addChild(ball);

Just be sure to use the MovieClip symbol name from the library in place of CPU and Ball.

Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51
0

In your commented-out code in the TheCpu class, you put "ball.y" when I think you meant "ball.x".

bandaro
  • 165
  • 7