Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //init(); //call this function once, at the very beginning, to initialize everything
- //
- // function init():void
- // {
- // stage.addEventListener(Event.ENTER_FRAME, loop); //every time our game enters a new frame (60 times per second), this event listener will fire and our game will loop
- // }
- //
- // function loop(e:Event):void
- // {
- //
- // }
- // var ballSpeedX:int = -3;
- // var ballSpeedY:int = -2;
- //
- // init();
- //
- // function init():void
- // {
- // stage.addEventListener(Event.ENTER_FRAME, loop);
- // }
- //
- // function loop(e:Event):void
- // {
- // 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
- //
- // } 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)
- // \
- // }
- //
- // //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
- //
- // }
- // }
- var ballSpeedX:int = -3;
- var ballSpeedY:int = -2;
- init();
- function init():void
- {
- stage.addEventListener(Event.ENTER_FRAME, loop);
- }
- function loop(e:Event):void
- {
- 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
- } 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)
- }
- //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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement