0
votes

I am trying to introduce a new ball into my pong game, sort of like a power up. I am writing all of my code in the Actions panel in the first frame. The new ball should appear on the stage and start moving around randomly like the original ball. Although I am using a code snippet and not .as file. So all of my code is in the Actions panel(Accessed by pressing f9).

I would also like my dynamic text box to merge with the stage colour so that you can't see the white background.

I can't show you what the fla looks like because I have less than 10 reputation, but the dynamic text box will not merge into the background and instead has a white surrounding. This hides the ball when the ball goes up.

import flash.events.Event;
import flash.ui.Mouse;

//hide mouse
Mouse.hide();






init(); //initialises everything

var bSpeedX:int = -3.5;
var bSpeedY:int = -2.5;


// assign a maximum speed to the AI 
var compPaddleSpeed:int = 3.5; 

var pScore:int = 0;
var cScore:int = 0;




// Updates the score
function scoreUpdate():void {
    playerScore.text = ("Player Score: " + pScore);
    computerScore.text = ("AI Score: " + cScore);


}




function init():void //tells flash not to return values
{ 

    stage.addEventListener(Event.ENTER_FRAME, loop);

}   
/*we want the ySpeed to be larger if there 
is a greater difference between the y 
positions of the ball and paddle, so I started with 
(gameBallY-padY). To convert this difference 
into a number between -1 and 1, I divided 
this number by 25, which 
is half the height of the paddle. Finally, I wanted 
the ySpeed to be more powerful than 
just -1 to 1, and after a bit of trial and error 
I decided to times by 5 at the end 
to change the total magnitude of the new ySpeed.*/



//defying the laws of Physics
function calculategameBallAngle(padY:Number, gameBallY:Number):Number
{
    var ySpeed:Number = 5 * ((gameBallY-padY) / 25 );
    return ySpeed;
}





//main loop 
function loop(e:Event):void

{   
    //makes the paddle track the mouse
    playerPaddle.y = mouseY;

    //paddle AI
    if(compPaddle.y < gameBall.y - 10){ 
        compPaddle.y += compPaddleSpeed;//make it go up
    } else if(compPaddle.y > gameBall.y + 10){ 
        compPaddle.y -= compPaddleSpeed;//make it go down
    }

    //Collisions
    if( playerPaddle.hitTestObject(gameBall) == true ){
    if(bSpeedX < 0){
        bSpeedX *= -1;
        bSpeedY = calculategameBallAngle(playerPaddle.y, gameBall.y);
    }

} else if(compPaddle.hitTestObject(gameBall) == true ){ 
    if(bSpeedX > 0){
        bSpeedX *= -1;
        bSpeedY = calculategameBallAngle(compPaddle.y, gameBall.y);
    }

}


    //makes the gameBall move 
    gameBall.x += bSpeedX; //each frame, we add the bSpeedX to the ball's x position.
    gameBall.y += bSpeedY; //same for the bSpeedY to the ball's y postion.



    // checks to see if the ball misses the paddle 
    if(gameBall.x <= gameBall.width/2){
       gameBall.x = gameBall.width/2;
       bSpeedX *= -1;
       cScore ++;
       scoreUpdate();


    //keeps the ball within the stage
    } else if(gameBall.x >= stage.stageWidth-gameBall.width/2){
        gameBall.x = stage.stageWidth-gameBall.width/2;
        bSpeedX *= -1;
        pScore ++;
        scoreUpdate();


    }

    if(gameBall.y <= gameBall.height/2){
        gameBall.y = gameBall.height/2;
        bSpeedY *= -1;
    }

    else if(gameBall.y >= stage.stageHeight-gameBall.height/2){
        gameBall.y = stage.stageHeight-gameBall.height/2;
        bSpeedY *= -1;
    }
 //-------------------------------------------------------



    //keeps the player paddle within the stage
    //check if paddle is above top of the screen 
    if(playerPaddle.y - playerPaddle.height/2 < 0){
        playerPaddle.y = playerPaddle.height/2;
    } else if(playerPaddle.y + playerPaddle.hieght/2 > stage.stageHeight){
        playerPaddle.y = stage.stageHeight - playerPaddle.height/2;


    //check if paddle is below bottom of the screen
    } else if(playerPaddle.y + playerPaddle.height/2 > stage.stageHeight){
     playerPaddle.y = stage.stageHeight - playerPaddle.height/2;
  }

    }
1
I have no idea what you are talking about. You say something abot ball spawning and than you talk about some text box. Is your ball a text file or what. Try to be more precise so we can clearly see your problem.Paweł Audionysos
Which text box are you talking about here? What is the instance name and what are its properties from the panel?Gurtej Singh
The text boxes are for player score and ai score. The instance names for them are playerScore and computerScore.Tech2day-AND-4eva
@PawełAudionysos I have two dynamic text fields. They should allow me to keep track of the score by adding 1 to pScore and cScore. The problem I am having with the text boxes is that the stage colour is blue and instead of the text merging in with the stage to show only the text it also has a box with a white background. I was asking if it were possible to remove this white background and make the YELLOW text only appear on top of the BLUE stage colour without a box.Tech2day-AND-4eva
Select text field and in its properties find button called Show border around text? And what this has to do with question title?Paweł Audionysos

1 Answers

0
votes

If you only want your ball to be replaced with new one which has diffrent grphics and/or speed you can for example export your ball class to action script:

Library>RMB on your symbol>Properties>ActionScript Linkage>Export for ActionScript

Type your class name under Class: field like "MyBallClass" and hit OK.

Now you can construct this ball in your code and replace old one like this:

var newBall:MyBallClass = new MyBallClass();
addChild(newBall);
newBall.x = gameBall.x; newBall.y = gameBall.y;
gameBall = newBall;

Additionally you can define new variable like var speedModifier:Number = 1; to use with:

gameBall.x += bSpeedX * speedModifier;
gameBall.y += bSpeedY * speedModifier;

And change that also when you change the ball.

If You want to have multiple balls at same time You really should consider build this in OOP. For simplest example in addition to previous one You can create MyBallClass.as file and write in it something like:

package  
{
    import flash.display.Sprite;
    import flash.geom.Point;

    public class MyBallClass extends Sprite
    {
        public var speedFactor:Number;
        public var speed:Point = new Point(-3.5, -2.5);
        public function MyBallClass(x:Number, y:Number, speedFactor:Number = 1) 
        {
            this.x = x; this.y = y;
            this.speed = speed;
        }
    }
}

Now you can create container for all the balls in yor game.

var balls:Vector<MyBallClass> = Vector<MyBallClass>([]);

and run your physics for all of them in a loop.

Generally main code would look something like this:

var balls:Vector.<MyBallClass> = Vector.<MyBallClass>([]);

addBall(...)//place first ball.

function loop(e:Event):void {
    processBalls();
    if(wantToAddNewSuperSpeedBall) addBall(x,y,3);
    ...
}

function processBalls() {
    for (var i:int = 0; i < balls.length; i++) {
        detecCollision(balls[i]);
        moveBall(balls[i]);
        //any code that process a single ball...
    }
}

function addBall(x:Number, y:Number, speedFactor:Number = 1) {
    var newBall:MyBallClass = new MyBallClass(x,y, speedFactor);
    addChild(newBall);
    balls.push(newBall);
}

function moveBall(ball:MyBallClass) {
    ball.x += ball.speed.x * ball.speedFactor;
    ball.y += ball.speed.y * ball.speedFactor;
}

So you should modify all functions which affect ball behavior to work with ball passed as argument, not only one specific instance and then use them for all balls.

There are more to cover in this topic and this isn't maybe the best approach but I've tried to make it easy to understend. There a lot of guides for OOP so you can get better idea about what is going on if you read them. I hope that helped you somehow.