A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [CS3] Changing a Moving MC's Direction

  1. #1
    Member
    Join Date
    Aug 2006
    Posts
    56

    [CS3] Changing a Moving MC's Direction

    Hey, everybody. I am trying to make a game of pong, but I can't properly reverse the direction of the ball when it touches the walls and paddles. If anybody knows how to solve this, please help me. This is my current code on the ball itself:

    Code:
    onClipEvent (enterFrame) {
    	// Speed and Direction
    	BallSpeed = 1;
    	_root.Direction = random(2);
    	if (_root.Direction=0) {
    		this._x = (_root.Ball._x -= BallSpeed);
    		this._y = (_root.Ball._y -= BallSpeed);
    	}
    	if (_root.Direction=1) {
    		this._x = (_root.Ball._x += BallSpeed);
    		this._y = (_root.Ball._y += BallSpeed);
    	}
    	// HitTests      
    	if (this.hitTest(_root.MaxHeight)) {
    		this._y = (_root.Ball._y += BallSpeed);
    	}
    	if (this.hitTest(_root.MinHeight)) {
    		this._y = (_root.Ball._y -= BallSpeed);
    	}
    	if (this.hitTest(_root.RightPaddle)) {
    		this._x = (_root.Ball._x -= BallSpeed);
    	}
    	if (this.hitTest(_root.LeftPaddle)) {
    		this._x = (_root.Ball._x += BallSpeed);
    	}
    }
    Thanks in advance!

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Separate your speed variable into speedx and speedy. Then you can reverse speedx on left/right walls and speedy on top/bottom walls.

  3. #3
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    to reverse your direction variable better use values of '1' and '-1' that way you can easier switch it by using:
    PHP Code:
    direction*=-1
    which is basicly the same as writing:
    PHP Code:
    direction=direction *-1
    and that would make either 1 resulting to -1 or -1 resulting into 1

  4. #4
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    tonypa is right:
    Quote Originally Posted by tonypa
    Separate your speed variable into speedx and speedy.
    This is called vector, you will probably learn that in school at some point, so just wait

    @render, nothing stops you from doing direction = 1 - direction to go from 0 to 1 and back
    Last edited by realMakc; 09-06-2008 at 03:06 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  5. #5
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    well I noticed that I forgot to mention that in context of
    PHP Code:
    this._x+= direction*BallSpeed
    so that the direction vriable really works as a direction variable and not just as a boolean switch

  6. #6
    Member
    Join Date
    Aug 2006
    Posts
    56
    Thanks for your help, but nothing seems to work. I realized that even the line I wrote that determines the direction and the if statements don't work:
    Code:
    _root.Direction = random(2);
    	if (_root.Direction=0) {
    		this._x = (_root.Ball._x -= BallSpeed);
    		this._y = (_root.Ball._y -= BallSpeed);
    	}
    	if (_root.Direction=1) {
    		this._x = (_root.Ball._x += BallSpeed);
    		this._y = (_root.Ball._y += BallSpeed);
    	}
    The ball always moves right. Can anybody figure out the problem here?

  7. #7
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    Can anybody figure out the problem here
    if by that you mean fix your code, then I can't figure it out, sorry.
    who is this? a word of friendly advice: FFS stop using AS2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center