NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

var ballSpeedX:int = -3;
var ballSpeedY:int = -2;
var cpuPaddleSpeed:int = 3;
var playerScore:int = 0;
var cpuScore:int = 0;

init();

function init():void
{
stage.addEventListener(Event.ENTER_FRAME, loop);
}

function calculateBallAngle(paddleY:Number, ballY:Number):Number
{
var ySpeed:Number = 5 * ( (ballY-paddleY) / 25 );
// (ballY-paddleY) / 25 will be between -1 and 1 depending on where the ball hits

return ySpeed;
}

function updateTextFields():void
{
playerScoreText.text = ("Player Score: " + playerScore);
cpuScoreText.text = ("CPU Score: " + cpuScore);
}

function loop(e:Event):void
{

if( playerPaddle.hitTestObject(ball) == true ){
if(ballSpeedX < 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(playerPaddle.y, ball.y); //add this
}
} else if(cpuPaddle.hitTestObject(ball) == true ){ //add this
if(ballSpeedX > 0){
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(cpuPaddle.y, ball.y); //add this
}

}

if(cpuPaddle.y < ball.y - 10){
cpuPaddle.y += cpuPaddleSpeed;
} else if(cpuPaddle.y > ball.y + 10){
cpuPaddle.y -= cpuPaddleSpeed;
}

playerPaddle.y = mouseY;

//check if top of paddle is above top of screen
if(playerPaddle.y - playerPaddle.height/2 < 0){
playerPaddle.y = playerPaddle.height/2;

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

ball.x += ballSpeedX;
ball.y += ballSpeedY;

//because the ball's position is measured by where its CENTER is...
//...we need add or subtract half of its width or height to see if that SIDE is hitting a wall

//first check the left and right boundaries
if(ball.x <= ball.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0
ball.x = ball.width/2; //then set the ball's x position to that point, in case it already moved off the screen
ballSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left
cpuScore++; //increase cpuScore by 1
updateTextFields();

} else if(ball.x >= stage.stageWidth-ball.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550
ball.x = stage.stageWidth-ball.width/2; //reposition it, just in case
ballSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)
playerScore++; //increase playerScore by 1
updateTextFields();

}

//now we do the same with the top and bottom of the screen
if(ball.y <= ball.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen
ball.y = ball.height/2; //like we did before, set it to that y position...
ballSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up

} else if(ball.y >= stage.stageHeight-ball.height/2){ //if the bottom of the ball is lower than the bottom of the screen
ball.y = stage.stageHeight-ball.height/2; //reposition it
ballSpeedY *= -1; //and reverse its y speed so that it is moving up now

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.