A Flash Developer Resource Site

# Thread: collisions and obtacles

1. ## collisions and obtacles

I have attached the fla for a problem that I do not understand. I would like to learn by creating a pinball type machine. So I have a ball that bounces around but if you watch the movie, the falling ball sometimes goes right thru the obstacle. It should bounce off of it. The ball never does this when travelling upwards, just sometimes in the downward velocity. I am not sure of what to ask for. Any suggestions? I also posted this in games but never got a reply so that is why I am asking it here.

2. Its a big problem Ive had myself ( and have ). The problem seems to be that when the ball is hitTested and is for instance 6 pixels into the other object it gets instructed to reverse its motion and go 4 pixels up, and then it is still in hittest area so it reverses its motion again and so on.

You will notice that if you make the ball go a longer distance per frame when it hits the stop the bad behaviour ends ( having _root["ball"].speedxstart *= -3; on the stop for instance ). But then the ball goes a lot faster of course.

Some posts suggest that you make a variable with the location of the ball just before the collision occurs and refer to that in the hittest statement.

I did a Pinball game once and I had to place the stops in certain areas or it would not work. ( example www.pellepiano.com/games/flipout.html ).

3. Originally posted by pellepiano

Some posts suggest that you make a variable with the location of the ball just before the collision occurs and refer to that in the hittest statement.

this I do not understand.

Also is there a way to define areas that would work as boundries. if I have a movie that is 100,100 and I want the center to be an obtacle, how would I define the center obstacle being a square of 10*10. The corners would be (x,y)=(45,45)(55,45)(45,55)(55,55).
I used

floor=400;

if (this._y > floor) {
this._y = floor ;
speedy *= -bounce ;
time = getTimer () ;
time /= 100 ;
speedystart = speedy ;
}

to determine the floor. Somehow put the (x,y) into the this._y > floor statement. Then on the actuall movie I would just put a shape or vector in the center position? Maybe I am overthinking this? I will go thru your suggestions. Thanks for your help. I visit your great site sometimes when I need inpiration.

4. In theory you could use like....

if (this.hitTest(_root.ball)) {
_root["ball"].speedxstart *= -2;
_root["ball"].speedystart *= -2;
_root["ball"]._y = prev_posy;
} else {
}
prev_posy = _root["ball"]._y;

That will always have the balls previous position handy if there is a hitTest and return the ball outside of a colission.

As for now your ball checks hitTest with its bounding box ( square ) but hitTest can also be used to check the borders of just the graphics inside a movieclip with the ShapeFlag ( but it can only be used on one object though. Two objects can not sense both their true borders ).

I do not know how to put all borders into one statement though.

5. I see that you are keeping x and y seperate. That was my thinking when i started. I do not know why the y axis is acting different. I need to go find an old physics book and get the basics down, even though I can fake it. I also believe this is why the ball keeps bouncing faster and faster. Just sloppyness on my part. I have redone the basic fla. I understand the bounding box but not the shape flag.
Your suggestion did work for me. Thank you very much.

I have seen some good games done entirely with actionscript and no shapes. The one that macromedia did with asteroids is an example. But I am a long way from that. I am sure that you have seen it.

6. No I haven't. I'll check it out.

Im not good with scripting physics ( I had to hire a programmer for making my ball bounce right from round objects ), although I have done some colissin detection , mostly the olf way fro Flash 4 where you have to define four areas of a obstacle to know what part the ball hits.

I would have done much more games with balls if I had not have this problew wjth shapes getting stuck in eachother, as I really like the "randomness" of these games.

7. I attached the swf and asteroids flsh if you are interested. I did get it from macromedia and it is supposed to be as closely to the original as possible (from what I read). The whole lesson is really 10 fla.

Once again thanks for your help. I will use your last method, _root["ball"]._y = prev_posy; untill I learn a better way. Maybe this is the best way. I have been doing this with brute force to make myself understand this actionscript language. I am getting better but have a long way to go. This copying scrips from other peoples fla helps but it is not the real way to go.

8. Thanks I'll look into it.
Have you seen the pinball game on the Starsky&Hutch site? Its the best ever seen in Flash.

http://starskyandhutchmovie.warnerbros.com/

All kinds of cool things on that site.

9. Wow, that game works like the real thing.

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width