View Full Version: Collision detect is it possible?

lassie >>Discussions >>Collision detect is it possible?


<< Prev | Next >>

NigeC- 12-17-2007
Collision detect is it possible?
would collision detect be possible between the a character and a sprite in Lassie? as in a character could pickup a coin automatically as he passes over it or recoils/losses health hitting an enemy If it is possible what area do i need to focus on? the Lassie Dizzy game has gone down well. i plan to do some thing more accurate

bigmac- 12-17-2007

There is not true shape--*test*-('")ing in Flash. That's to say that you can't draw two circles and use Flash to -*test*-('") when their positive image areas are touching. The flash-native hit--*test*-('") will only -*test*-('") a single point (x, y) against the actual image area of a shape. However, there are ways to set up shape--*test*-('")s. The simplest is doing a bounds -*test*-('"). You can get the bound of each object and -*test*-('") the rectangle coordinates of each to see if they overlap. For this method, you'd want to research MovieClip.getRect() or MovieClip.getBounds() (they are very similar). Bounds hit -*test*-('")ing is a little weak though. Often times you end up getting hit-results when transparent image corners overlap (which is a little strange). So, hit--*test*-('")ing to a circle is often times better, though requires some simple Trigonometry. So, you'll want to work with the center point of each sprite's display, which you can derive either from centering their registration point or assessing height and width relative to screen position. Once you have the center point of each clip, find the distance between the two points using the Pythagorean Theorem: A-squared + B-squared = C-squared. In context, you want to solve for C (the hypotenuse) so it would be... var c:Number = Math.sqrt((a*a) + (b*b)); Once you've got the distance between center points, you can basically say that you're hit--*test*-('")ing with circles since this distance would be a radian of an imaginary circle. So, just decide how big that circle should be and decide if your radian is longer than the radius. Don't panic, this isn't too bad. It sounds like a lot, but I implemented a working version of the second method on the first big Flash programming project I ever did. Noobs can work through this!!

NigeC- 12-17-2007

I'm really thick :oops: .. so can i try and put it into context on how i would use collisions in Lassie just out of arguments sake, i wanted _char to bounce off a floor sprite.. a simple AS example would be: xmov=0; gravity=1; _root.onEnterFrame=function () { ymov+=gravity; ball._y+=ymov; if (ball.hitTest(floor)) { xmov*=-1; } } to be able to get this little snippet to work within Lassie would open a load of doors, i just don't know how to get code accessible within the Lassie environment would ball be _char and the sprite id floor.. or would that have to be a movie clip? sorry if its all a bit vague.. ask me how computerized fuel injection works and i could get all techie then lol

bigmac- 12-17-2007

Hey Nige, I'm not entirely following your example, but then I'm also skimming too. I'd just drop your onEnterFrame script as a timeline script in the character. So, on frame 1 of the character SWF you'd put: this.onEnterFrame = function() { //do dtuff! } Then for targeting other objects in the room you'll have to use a double parent reference. For example, to target your floor object (within the room scope) you'd need to reference it as: this._parent._parent.floor_mc; This will ascend you through the ranks of the character image and container, on up to the room clip. If I got that targeting reference wrong, you can always go top-down and do: _root.room.floor_mc hope that help. Ok, got a meeting.

NigeC- 12-17-2007

:oops: still can't get it to work! ok simplify it even more lol.. for my benefit _cha walks into a npc character called dora if _char hits dora then play sequence dora

bigmac- 12-17-2007

Sounds pretty logical. Now you just have to put it into code... That just takes tinkering. Try just getting the onEnterFrame rolling for starters with a traced result pointing to Dora. Traced result being the standard flash output: trace(this._parent._parent.dora); Since you've got the Lassie player as an intermediary, you'll have to collect trace output from a browser window. Use Firefox and get the Flash Tracer plug-in installed. It will let you view a panel that dumps out Flash output realtime. Sweet stuff. Or, easier yet... put a dynamic text field within the character (remember to embed fonts!). Then just use that as the output. First step is to get your object reference working, so that when you output the asset you'll get a path to the MovieClip rather than the word "undefined". Undefined results means you've got an invalid object reference.

NigeC- 12-17-2007

thanks Greg, i downloaded the Trace plugin, i just need to have a play and see what i can come up with thanks for your time

Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.