0
votes

Im trying to make a simple game in AS3 where a player eats as many balls as possible. i dont know how to code very well and im having trouble trying to add a new ball to the stage every time one is eaten. this is the code i have in main.as at the moment.

    private var startX:Number = 512;
    private var startY:Number = 384;
    private var speed:Number = 8;
    var player1;
    var player2;
    var player3;
    var theBall; 
    player1 = new player(50,384, 1);
    player2 = new player(944,384,2);
    player3 = new player(488,84,3);
    stage.addChild(player1);
    stage.addChild(player2);
    stage.addChild(player3);

    if(theBall.hitTestObject(player1) || theBall.hitTestObject(player2) || theBall.hitTestObject(player3))
        {
            //removes the ball from the stage
            trace("a player has eaten a ball");
            stage.removeChild(theBall);
            //adds new ball
            //stage.addChild(theBall);
            //reset x and y
            startX = Math.random()*speed-speed/2;
            startY = Math.random()*speed-speed/2;
        }

In the ball.as ive specified how the ball should move randomly, start in the center of the stage and bounce off walls.

no errors appear, the code just doesnt work. how do you make a new ball re spawn in the center of the stage when one is eaten? do i declare this where i tried to in the main, or in the ball.as?

2

2 Answers

0
votes

If you have a "ball" Object, in the way you have a "player" Object, then...

        //removes the ball from the stage
        trace("a player has eaten a ball");
        stage.removeChild(theBall);
        //reset x and y
        startX = Math.random()*speed-speed/2;
        startY = Math.random()*speed-speed/2;
        //adds new ball
        theBall = new ball();
        theBall.x = stage.stageWidth/2;
        theBall.y = stage.stageHeight/2;
        stage.addChild( theBall );

You reuse the "theBall" variable, since you've removed the eaten instance from the stage. The old "theBall" instance is thrown out with stage.removeChild(theBall), and then you make a new one and add that new one to the stage.

0
votes

Before your code, if the BALL isn't called and prepared for your stage. It might not work.

In your Library, check the movie_clip that contain the ball... And under the row "AS LINKAGE" next to the name of the Movie Clipe... You should call it for example "BALL_TEST"

In your AS3 code :

Add this before your code

var ball_bounce:BALL_TEST = new BALL_TEST();

Then your code

If i did correctly understand your question...

Kind regards