homepage Welcome to WebmasterWorld Guest from 50.17.176.149
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Site Graphics and Multimedia Design
Forum Library, Charter, Moderator: open

Site Graphics and Multimedia Design Forum

    
Collision Detection
for a landscape in Flash...
joshie76




msg:858136
 1:42 pm on Mar 13, 2002 (gmt 0)

I want to create some complex collision detection for a landscape environment where the good old bounding boxes of Movie Clips and hitTest() probably isn't going to be suitable.

Imagine a Lander type game - what would be the best way to do such collision detection?

Josh

 

joshie76




msg:858137
 9:00 pm on Mar 14, 2002 (gmt 0)

Think I've cracked it...

By having the 'landscape' as a Movie Clip with transparent background you can use landscape.hitTest(object._x, object._y, true). This takes an explicit x-y coord and returns true if it falls on a part of the landscape that isn't transparent. Ideal for Lander;)

J

mivox




msg:858138
 9:09 pm on Mar 14, 2002 (gmt 0)

Interesting! You'd utterly lost me on your original post, but please sticky me with a URL when you get it working... I'd love to get a look at the effect.

joshie76




msg:858139
 9:45 am on Mar 15, 2002 (gmt 0)

There probably won't be a URL as I'm just learning Flash and going through the motions - every time I find something I might want to do, I want to learn the best way of doing it - fussy like that;)

If you want to try the effect it's pretty easy to set it up.

- Create a movie clip and draw some abstract shape leaving the background 'empty' (transparent). Place this on the root timeline. Name the instance of this movie clip something eg: 'landscape'.

- If you were making a game you'd probably have some sprite that you wanted to test for collisions with the background, but for brevity we'll just use the mouse. Principle's the same.

- To add some output (so you can see when there is a collision) add a dynamic textbox called 'outputField' to the root timeline.

- In the ActionScript Panel for your landscape movie clip instance insert the following script:

onClipEvent(enterFrame)
{
if (this.hitTest(_root._xmouse, _root._ymouse, true))
{
_root.outputField = 'hit';
}
else
{
_root.outputField = '';
}
}

- Test your movie - you should see that when the mouse pointer is over a populated part of your movie clip your dynamic text box shows 'hit' and when it's not it should be blank.

A useful effect for anyone wanting to create 2D games etc


NOTE
- To use this type of collision detection you have to specify an exact x-y coord in the hitTest function. However this throws up a question... What if my 'sprite' is bigger than a single pixel (which it's likely to be). Easy - in your if (hitTest statement here) test you need to add more hitTests as part of the if statement (OR) where each x-y coord represents a different corner of your 'sprite'. You'd use the getBounds(_root) method to let flash work this out for you - check the ActionScript dictionary for a full explanation of that method.

- also, if there's an error in the above let me know as I'm writing from memory - my copy of Flash is at home!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Site Graphics and Multimedia Design
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved