Sticky walls...?
- Marsume
-
Marsume
- Member since: Oct. 22, 2006
- Offline.
-
- Forum Stats
- Member
- Level 09
- Blank Slate
So im messing around with some AS(2), and i decided to make a ball move with AS
when the ball touches the parimeter of the stage, it bounces back and gets a new value for its speed.
if it hits the left or right walls, the var xS gets a new value
if it hits the top or bottom walls, the ar yS gets a new value
the ball is 50x50 (so with a r=25)
Now when the ball hits the top or left wall...it sticks to it. if it hits the top wall, it doesnt bounce back, but sticks, and continues moving left along the top of the wall until the center of the ball is at (25,25) i
and the same thing happens when it hits the left wall fist, it sticks to it, but keeps moving up until it gets to (25,25)
so i cant figure it out, probably a easy solution that i overlooked, but any help is apprecieated.
anyways...heres my code
onClipEvent (load) {
var xS:Number = 10;
var yS:Number = 10;
}
onClipEvent (enterFrame) {
//movement
this._x = _x+xS;
this._y = _y+yS;
//bouncing X
if (this._x>(720-25)) {
xS = random(8);
switch (xS) {
case 1 :
xS = 1;
case 2 :
xS = 35;
case 3 :
xS = 5;
case 4 :
xS = 10;
case 5 :
xS = 15;
case 6 :
xS = 20;
case 7 :
xS = 25;
case 8 :
xS = 30;
}
this._x = 695;
xS = xS*(-1);
}
if (this._x<25) {
xS = random(8);
switch (xS) {
case 1 :
xS = 1;
case 2 :
xS = 35;
case 3 :
xS = 5;
case 4 :
xS = 10;
case 5 :
xS = 15;
case 6 :
xS = 20;
case 7 :
xS = 25;
case 8 :
xS = 30;
}
this._x = 25;
xS = xS*(-1);
}
//bouncing Y
if (this._y<0) {
yS = random(8);
switch (yS) {
case 1 :
yS = 1;
case 2 :
yS = 35;
case 3 :
yS = 5;
case 4 :
yS = 10;
case 5 :
yS = 15;
case 6 :
yS = 20;
case 7 :
yS = 25;
case 8 :
yS = 30;
}
this._y = 25;
yS = yS*(-1);
}
if (this._y>455) {
yS = random(8);
switch (yS) {
case 1 :
yS = 1;
case 2 :
yS = 35;
case 3 :
yS = 5;
case 4 :
yS = 10;
case 5 :
yS = 15;
case 6 :
yS = 20;
case 7 :
yS = 25;
case 8 :
yS = 30;
}
this._y = 455;
yS = yS*(-1);
}
}
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 3/18/09 01:46 AM, Marsume wrote: Now when the ball hits the top or left wall...it sticks to it. if it hits the top wall, it doesnt bounce back, but sticks, and continues moving left along the top of the wall until the center of the ball is at (25,25) i
and the same thing happens when it hits the left wall
When the ball hits the left wall, you don't need the xS = xS*(-1); since you're assigning positive values for the xS... then you're turning them to negative values, so the xS is negative - which keeps it moving leftwards, not rightwards. Same applies to the yS = yS*(-1); if _y<0 (which should be 25 btw)
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Also as an afterthought you may want to consider using an array for the speed values rather than all those case/switch statements
onClipEvent (load) {
var xS:Number = 10;
var yS:Number = 10;
spdArr=[1,5,10,15,20,25,30,35];
}
onClipEvent (enterFrame) {
//movement
_x = _x+xS;
_y = _y+yS;
//bouncing X
if (_x>695) {
xS=spdArr[random(spdArr.length)];
_x = 695;
xS = xS*(-1);
}
if (_x<25) {
xS=spdArr[random(spdArr.length)];
_x = 25;
}
//bouncing Y
if (_y<25) {
yS=spdArr[random(spdArr.length)];
_y = 25;
}
if (_y>455) {
yS = random(8);
yS=spdArr[random(spdArr.length)];
_y = 455;
yS = yS*(-1);
}
} 

