Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-03-2010, 02:46 AM   #1
flips30240
Registered User
 
Join Date: Dec 2009
Posts: 44
Default [AS3] pong funtime

hello all ( again ) im making a pong game where one of the paddles are ai controlled(correct term usage? probably not) the code i have for that is

function ai(event:Event) :void {
paddlepart1.y = ball.y + 19;
paddlepart2.y = ball.y - 19
}

(i made the paddle of 2 dif pieces to control if the ball will bounce of it at an upwards or downwards angle, im sure theres an easier way to do that but i dont know it lol)

the paddle halves are 38 pixels each so hence the - + 19.

ok SO now to my dilema, the paddle will get to the ball every time, maiing the game impossible to win, i want to slow the paddle down but i dont now how to add a speed variable into it. any suggestions?
flips30240 is offline   Reply With Quote
Old 12-03-2010, 03:19 AM   #2
malarr
Registered User
 
Join Date: Dec 2010
Posts: 6
Default

don't use ball += 19, instead try using a variable like xSpeed and ySpeed then create a random number that will change the speed.
malarr is offline   Reply With Quote
Old 12-03-2010, 03:25 AM   #3
Kritjon
Senior Member
 
Kritjon's Avatar
 
Join Date: Oct 2009
Location: Beijing, China
Posts: 244
Default

Just give the paddle a maximum speed.

ActionScript Code:
const PADDLE_SPEED = 10; function ai(event:Event):void {     if ((Math.abs(paddle.y - ball.y)) < PADDLE_SPEED) {         paddle.y = ball.y;     } else if (paddle.y < ball.y) {         paddle.y +=  PADDLE_SPEED;     } else {         paddle.y -=  PADDLE_SPEED;     } }
Kritjon is offline   Reply With Quote
Old 12-03-2010, 03:51 AM   #4
flips30240
Registered User
 
Join Date: Dec 2009
Posts: 44
Default

love the quick responses here =) ill give that a shot =3
flips30240 is offline   Reply With Quote
Old 12-03-2010, 04:11 AM   #5
flips30240
Registered User
 
Join Date: Dec 2009
Posts: 44
Default

Quote:
Originally Posted by Kritjon View Post
Just give the paddle a maximum speed.

ActionScript Code:
const PADDLE_SPEED = 10; function ai(event:Event):void {     if ((Math.abs(paddle.y - ball.y)) < PADDLE_SPEED) {         paddle.y = ball.y;     } else if (paddle.y < ball.y) {         paddle.y +=  PADDLE_SPEED;     } else {         paddle.y -=  PADDLE_SPEED;     } }
alright you lead me to the right track track with the if(paddle.y < ball.y) and if(paddle.y > ball.y) which resulted in the finished and working code of

ActionScript Code:
function ai(event:Event) :void {     if(paddlepart1.y < ball.y){             paddlepart1.y += 4.5;             paddlepart2.y += 4.5;         }else if(paddlepart2.y > ball.y){             paddlepart1.y -= 4.5;             paddlepart2.y-= 4.5;         } }

huge thanks, its funny how simple something seems once the solution is in front of you =3
flips30240 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:19 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.