-
[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!
-
Senior Member
Separate your speed variable into speedx and speedy. Then you can reverse speedx on left/right walls and speedy on top/bottom walls.
-
to reverse your direction variable better use values of '1' and '-1' that way you can easier switch it by using:
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
-
Senior Member
tonypa is right:
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.
-
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
-
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?
-
Senior Member
Can anybody figure out the problem here
if by that you mean fix your code, then I can't figure it out, sorry.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|