You are not logged in.
Announcement
#1 05/03/07 10:54am
- blufires
- Member
- Registered: 04/03/07
- Posts: 2
VISTA Inkball for psp
i have made a game where the ball starts moving, then it bounces off of lines u draw, i just need to know how to make the walls deflect the ball too... please help.
here is the code on the ball:
onClipEvent (load) {
yspeed = 0;
xspeed = 2;
gravity = 0.0;
radius = 14;
friction = 0.90;
precision = 180;
bounces = 0;
}
//lines drawn
onClipEvent (enterFrame) {
if (_root.go == true) {
collisions = 0;
sum_x = 0;
sum_y = 0;
yspeed = yspeed+gravity;
for (x=1; x<precision; x++) {
spot_x = _x+radius*Math.sin(x*360/precision);
spot_y = _y-radius*Math.cos(x*360/precision);
if (_root.terrain.hitTest(spot_x, spot_y, true)) {
collisions++;
sum_x += spot_x;
sum_y += spot_y;
}
}
if (collisions>0) {
bounces++;
_root.collisions.text = "Bounces: "+bounces;
ball_dir = Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
if ((xspeed*-1)<0) {
ball_dir += 180;
}
if ((xspeed*-1)>=0 && yspeed<0) {
ball_dir += 360;
}
spot_x = sum_x/collisions;
spot_y = sum_y/collisions;
x_cat = spot_x-_x;
y_cat = spot_y-_y;
ball_coll = Math.atan(y_cat/x_cat)/(Math.PI/180);
if (x_cat<0) {
ball_coll += 180;
}
if (x_cat>=0 && y_cat<0) {
ball_coll += 360;
}
ground_rotation = ball_coll-90;
if (ground_rotation<0) {
ground_rotation += 180;
}
bounce_angle = 180-ball_dir-2*(ground_rotation);
if (bounce_angle<0) {
bounce_angle += 360;
}
speed = Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
xspeed = speed*Math.cos(bounce_angle*Math.PI/180)*friction;
yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
}
_y = _y+yspeed;
_x = _x+xspeed;
}
}
and here is the script on the frame to make the lines draw:
Code:
createEmptyMovieClip("terrain", 1); terrain.lineStyle(10, 0xACEE11, 100); imdrawing = false; onMouseDown = function () { if (imdrawing == false) { terrain.moveTo(_xmouse, _ymouse); imdrawing = true; } if (imdrawing == true) { onEnterFrame = function () { terrain.lineTo(_xmouse, _ymouse); }; } }; onMouseUp = function () { onEnterFrame = function () { imdrawing = false; }; };
please help if you can, i have had troubles making it work, and i got one wall to deflect the ball by changing its instance name to "wall", then copying the part of the script for the ball which defines the bouncing. (yes i made it from few different tutorials)
Offline
#2 05/03/07 12:49pm
Re: VISTA Inkball for psp
At a guess I would think that you need to make each new line into a movie clip, and then perform hit tests to see if the ball movie clip is touching the line movie clip - if it is, then you can tell the ball to bounce.
I don't really know enough actionscript to give a proper answer on this though - Jake's the guy you want to ask... Jake?
PSP Flash Gaming creator
Offline
#3 05/03/07 2:28pm
- Jake
- Administrator
- Registered: 29/04/06
- Posts: 307
Re: VISTA Inkball for psp
By the "wall" do you mean the edges of the screen?. Your other code seems fine and when I tried it the ball was bouncing of the lines. If this is the case then the following should work:
Code:
if(_x > 480){ _x = 480; xspeed = -xspeed; yspeed = -yspeed; }if(_x < 0){ _x = 0; xspeed = -xspeed; yspeed = -yspeed; }if(_y > 272){ _y = 272; xspeed = -xspeed; yspeed = -yspeed; }if(_y < 0){ _y = 0; xspeed = -xspeed; yspeed = -yspeed; }
The above code probably could be better but I have little time at the moment. Just add it in to the code on the ball so it runs every frame like the other stuff.
If this is not the problem then I suggest posting a link to the swf so I can see.
Offline
#6 05/03/07 3:24pm
- Jake
- Administrator
- Registered: 29/04/06
- Posts: 307
Re: VISTA Inkball for psp
The code you put in Phil will not work, for the reason that the position has to be reset otherwise the ball will get stuck at the edge "vibrating" otherwise I would have done it in one if statement.
And also if you wish to use an or statement the code for or is || eg.
Code:
if ((x==1)||(x==2)){ //code here }
Offline