Flash Advisor logo
:: Desktop Shortcut
:: Flash Help
Advice from Experts

Closed Thread
Results 1 to 2 of 2
  1. #1

    Default pong collisions and movement

    I may have asked this a while ago, but i'll ask anyway because if i did i rephrased it now.
    I'm making pong for my first game and I need help making collisions between the ball, the paddle, and the walls (objects the same color as the background)
    I have it so the paddle can't move off the screen but i can't figure out how i would change the ball's direction when it collides with something. what's a good way to go about this?

  2. # ADS
    Join Date
    Always
    Posts
    Many
     
  3. #2
    Join Date
    May 2004
    Location
    U.S.A.
    Posts
    2,890

    Default

    Hi,

    This can be done by assigning a variable such as speed to an objects _x or _y position in an enterframe event. Then, when you want to reverse direction, simply apply negative to the speed variable. To see this in practice, create a new movie with a circle movieclip named "ball". Paste this onto the first frame of the movie...

    Code:
    speed = 10;
    this.onEnterFrame = function() {
    	if (ball._x>=Stage.width) {
    		speed = -speed;
    	&#125; else if &#40;ball._x<=0&#41; &#123;
    		speed = -speed;
    	&#125;
    	ball._x += speed;
    &#125;
    The above only accounts for motion on the _x axis. The same thing would need to apply for the _y axis. To further the above example to include motion on both axis and create a boundary to show the ball bouncing off different boundaries,......... remove the above posted code from frame 1 and replace it with the following function....
    Code:
    function ballBounce&#40;myTarget&#41; &#123;
    	speedY = 10;
    	speedX = 8;
    	walls = 300;
    	top = 100;
    	bottom = 300;
    	r = myTarget._width/2;
    	_root.createEmptyMovieClip&#40;"outline", 1&#41;;
    	with &#40;_root.outline&#41; &#123;
    		lineStyle&#40;1, 0xFF00FF, 100&#41;;
    		moveTo&#40;top, bottom&#41;;
    		lineTo&#40;top, bottom&#41;;
    		lineTo&#40;walls, bottom&#41;;
    		lineTo&#40;walls, top&#41;;
    		lineTo&#40;top, top&#41;;
    		lineTo&#40;top, bottom&#41;;
    	&#125;
    	this.onEnterFrame = function&#40;&#41; &#123;
    		posY = myTarget._y+speedY;
    		posX = myTarget._x+speedX;
    		if &#40;posY>&#40;bottom-r&#41;&#41; &#123;
    			myTarget._y = bottom-r;
    			speedY = -speedY;
    		&#125; else if &#40;posY<top+r&#41; &#123;
    			myTarget._y = top+r;
    			speedY = -speedY;
    		&#125; else &#123;
    			myTarget._y = myTarget._y+speedY;
    		&#125;
    		if &#40;posX>&#40;walls-r&#41;&#41; &#123;
    			myTarget._x = walls-r;
    			speedX = -speedX;
    		&#125; else if &#40;posX<top+r&#41; &#123;
    			myTarget._x = top+r;
    			speedX = -speedX;
    		&#125; else &#123;
    			myTarget._x += speedX;
    		&#125;
    	&#125;
    &#125;
    ballBounce&#40;ball&#41;;
    This should help you understand the motion involved. You will need to research the hitTest method for detecting when movieclips collide to create a paddle that will change the speed variables upon contact.

    Regards
    NTD
    A mind once stretched by a new idea never regains its original dimensions.
    - Oliver Wendell Holmes

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. movement question and www.thekooks.co.uk
    By auejim in forum Advanced Flash
    Replies: 19
    Last Post: 05-13-2006, 09:59 PM
  2. Need help with Character movement
    By lewmiszkin in forum Games
    Replies: 0
    Last Post: 12-14-2005, 05:37 AM
  3. Off screen movement
    By Um in forum Flash Scripting
    Replies: 11
    Last Post: 11-22-2005, 12:55 PM
  4. need help with mouse over movement
    By domenique in forum Advanced Flash
    Replies: 3
    Last Post: 07-13-2005, 06:00 PM
  5. detecting 7 collisions?
    By Pappman in forum Flash Scripting
    Replies: 0
    Last Post: 09-14-2003, 12:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Sponsors
Create Speaking Characters for your website and Flash movies. 15 Day Free Trial