Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-14-2008, 10:26 PM   #1
wutu
Human
 
Join Date: Oct 2008
Location: Behind You?
Posts: 60
Question Trembling Opponent?

Hey world. Im trying to make a very old game much better than it currently is. It is a possibly better version of Pong and so far its been going great except for one problem that i just can't fix. The computer opponent is shaking for some reason. Can someone help me with this problem?

I have the swf attached (it is not the real one, I just changed the colors of the paddles, ball, etc.)

here is the AS2 code for it so far:

ActionScript Code:
init(); function init(){ Mouse.hide(); _root.onEnterFrame = function(){     quit();     player._y = _root._ymouse; } //ball.dy=5; ball.dx=10; opp.speed=6; //if opp speed is same as ball dx, opp has perfect AI } ball.onEnterFrame = function(){     ball.move();     ball.walls();     ball.bounce(); } ball.move = function(){     ball._x += ball.dx;     ball._y += ball.dy; } ball.walls = function(){   if (ball._y < 0){     ball.dy = -ball.dy;   }   if (ball._y > Stage.height){     ball.dy = -ball.dy;   }   if (ball._x < 0){     ball._x = Stage.width;   }   if (ball._x > Stage.width){     ball._x = 0;   } } ball.bounce = function(){   if (ball.hitTest(player)){     ball.dy = getDy(player);     ball.dx = -ball.dx;   }   if (ball.hitTest(opp)){     ball.dy = getDy(opp);     ball.dx = -ball.dx;   } } function getDy(paddle){   relY = ball._y - paddle._y;   relPerc = relY / paddle._height;   newDy = relPerc * 20;   return newDy; } opp.onEnterFrame = function(){   //opp._y = ball._y;   //perfect AI   if (ball._y < opp._y){       //move opp up     opp.dy = -opp.speed;   } else if (ball._y > opp._y){       //move opp down     opp.dy = opp.speed;   }   opp._y += opp.dy; } function quit(){     var key = new String("Q").charCodeAt(0);     if (Key.isDown(key)){         _root.gotoAndStop("start");   } }

Last edited by wutu; 10-23-2008 at 07:00 PM.
wutu is offline   Reply With Quote
Old 10-21-2008, 12:22 PM   #2
wutu
Human
 
Join Date: Oct 2008
Location: Behind You?
Posts: 60
Question [AS2] Pong: Trembling Opponent?

Hey world. Im trying to make a very old game much better than it currently is. It is a possibly better version of Pong and so far its been going great except for one problem that i just can't fix. The computer opponent is shaking for some reason. Can someone help me with this problem?

I have the swf attached (it is not the real one, I just changed the colors of the paddles, ball, etc.)

here is the AS2 code for it so far:

ActionScript Code:
init(); function init(){ Mouse.hide(); _root.onEnterFrame = function(){     quit();     player._y = _root._ymouse; } //ball.dy=5; ball.dx=10; opp.speed=6; //if opp speed is same as ball dx, opp has perfect AI } ball.onEnterFrame = function(){     ball.move();     ball.walls();     ball.bounce(); } ball.move = function(){     ball._x += ball.dx;     ball._y += ball.dy; } ball.walls = function(){   if (ball._y < 0){     ball.dy = -ball.dy;   }   if (ball._y > Stage.height){     ball.dy = -ball.dy;   }   if (ball._x < 0){     ball._x = Stage.width;   }   if (ball._x > Stage.width){     ball._x = 0;   } } ball.bounce = function(){   if (ball.hitTest(player)){     ball.dy = getDy(player);     ball.dx = -ball.dx;   }   if (ball.hitTest(opp)){     ball.dy = getDy(opp);     ball.dx = -ball.dx;   } } function getDy(paddle){   relY = ball._y - paddle._y;   relPerc = relY / paddle._height;   newDy = relPerc * 20;   return newDy; } opp.onEnterFrame = function(){   //opp._y = ball._y;   //perfect AI   if (ball._y < opp._y){       //move opp up     opp.dy = -opp.speed;   } else if (ball._y > opp._y){       //move opp down     opp.dy = opp.speed;   }   opp._y += opp.dy; } function quit(){     var key = new String("Q").charCodeAt(0);     if (Key.isDown(key)){         _root.gotoAndStop("start");   } }

see what you can do to fix the problem plz!

Last edited by wutu; 10-23-2008 at 07:00 PM.
wutu is offline   Reply With Quote
Old 10-21-2008, 12:40 PM   #3
bassmonster
Registered User
 
Join Date: Apr 2007
Posts: 53
Default

Hi Wutu

at first glance I would say what's happening is your if statement in your onEnterFrame is causing it, it's asking is the ball higher than the paddle? if so raise the position of the paddle, now the paddle is higher so lower the position of the paddle back and forth etc.

What you might want to do is check the actual difference between the paddle and the ball:

ActionScript Code:
opp.onEnterFrame = function(){   //opp._y = ball._y;   //perfect AI   diff = opp._y - ball._y;   if (Math.abs(diff) > 1) {      opp.speed = -diff * 0.5;      opp._y += opp.speed;   } }

The higher the decimal number (0.5) the better the opponent will be

Last edited by bassmonster; 10-21-2008 at 12:53 PM.
bassmonster is offline   Reply With Quote
Old 10-21-2008, 12:54 PM   #4
wutu
Human
 
Join Date: Oct 2008
Location: Behind You?
Posts: 60
Default

thx , this helped a lot!
wutu 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
[AS2] pong game - move slider ahungryghost Gaming and Game Development 1 06-21-2006 10:41 AM


All times are GMT. The time now is 09:17 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.