To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-20-2004, 07:16 PM   #1
RDTJ
Video Game Addict
 
Join Date: Apr 2004
Location: Oregon
Posts: 128
hit test barrier

I know how to get an object when hit to move to a random spot on the stage but how would i make it so that when something its it it stops?

Like this:
I have a movie clip of a wall, and when the user controlled player hits the wall it wont let them past it, like a boundry i guess, How would i do that?
RDTJ is offline   Reply With Quote
Old 10-21-2004, 06:12 PM   #2
RDTJ
Video Game Addict
 
Join Date: Apr 2004
Location: Oregon
Posts: 128
anyone please, cant be that hard...
RDTJ is offline   Reply With Quote
Old 10-21-2004, 06:24 PM   #3
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
What you're doing is called collision testing or hit testing. There are simple ways to do it, and more complicated ways.

A simple way to do it in flash is to use the hitTest() function, which comes in a few different forms. There is one form, where you can test two movieclips for collision:

code:

if (movie1.hitTest(movie2))
{
trace('kaboom!');
}



However this form tests if the bounding-boxes of each movieclip intersect, and sometimes, that isn't what you want, especially if your movieclips are not square in shape. It's particularly bad for long skinny movieclips which are rotated, because these have a big giant bounding-box.

There is another version which tests if a particular set of global x,y coordinates collides with any of the visible parts of a movieclip:

code:

if (movie1.hitTest(x,y,true))
{
trace('kaboom!');
}

OR

if (movie1.hitTest(movie2._x,movie2._y,true))
{
trace('kaboom!');
}





...and this tends to get used more often for games, as it is more precise. But since it only tests a single point, you have to account for that.

One way to account for that is to design your game so that objects move about a grid in discrete jumps. For example, in a frogger style game, if the frog always moves += 50 pixels, and the cars are centered on each 50x50 cell, then you only need to test the center of the frog with the cars to see if there is a hit.

Another way to deal with it is to test a few different points which represent different parts of the object (e.g. the back and front of a bumper car).

Finally, if the wall stretches all the way across the stage, or you are trying to restrain an object to a rectangular area of movement, it's easier to just do the math and check if _x is greater or less than some minimum or maximum value.

code:

if (this._x > 20 or this._x > 500)
{
trace('kaboom!');
}




In more advanced arcade games, collision testing can get pretty hairy - if an object is moving really fast, it may jump over an object it is supposed to collide with, which makes hitTest() style collision testing fail. In this case, you have to do a line/object collision test and see if the line of movement intersects with any objects. Ugh.
__________________

Last edited by jbum; 10-21-2004 at 06:28 PM.
jbum is offline   Reply With Quote
Old 10-30-2004, 06:31 PM   #4
RDTJ
Video Game Addict
 
Join Date: Apr 2004
Location: Oregon
Posts: 128
Thanks

thank you for the help
RDTJ is offline   Reply With Quote
Old 12-26-2007, 09:46 PM   #5
flashkay
Banned
 
Join Date: Dec 2007
Location: In planet earth...florida
Posts: 11
Hello! I am flashkay!
I would just like to say...I love hiTest!!!

Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

NOTE: the code goes inside movieclip 1

CODE:

onClipEvent (load) {
speed = 2;
}

onClipEvent (enterFrame) {
if (this.hitTest(_parent.MOIECLIP2)
this.speed = 0;
}
}

And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

NOTE: this code goes inside movieclip 1

CODE:

if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
this._x -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
this._x += 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
this._y -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
this._y += 2;
}
}



This might help!


Bye-bye!

~kayla
flashkay is offline   Reply With Quote
Old 12-26-2007, 09:48 PM   #6
flashkay
Banned
 
Join Date: Dec 2007
Location: In planet earth...florida
Posts: 11
Hello! I am flashkay!
I would just like to say...I love hiTest!!!

Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

NOTE: the code goes inside movieclip 1

CODE:

onClipEvent (load) {
speed = 2;
}

onClipEvent (enterFrame) {
if (this.hitTest(_parent.MOIECLIP2)
this.speed = 0;
}
}

And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

NOTE: this code goes inside movieclip 1

CODE:

if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
this._x -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
this._x += 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
this._y -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
this._y += 2;
}
}



This might help!


Bye-bye!

~kayla
flashkay is offline   Reply With Quote
Old 12-26-2007, 09:50 PM   #7
flashkay
Banned
 
Join Date: Dec 2007
Location: In planet earth...florida
Posts: 11
Hello! I am flashkay!
I would just like to say...I love hiTest!!!

Ok, here is one kind of HitTest that see's if a blue box that goes around the movie clip is touching another movie clip's blue box:

NOTE: the code goes inside movieclip 1

CODE:

onClipEvent (load) {
speed = 2;
}

onClipEvent (enterFrame) {
if (this.hitTest(_parent.MOIECLIP2)
this.speed = 0;
}
}

And this is to see if it is touching the actual movieclip 1, but, you can't make it slow down or anything...you can only make it stop:

NOTE: this code goes inside movieclip 1

CODE:

if (_root.MOVIECLIP1.hitTest(_x+(_width/2), _y, true)) {
this._x -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x-(_width/2), _y, true)) {
this._x += 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y+(_height/2), true)) {
this._y -= 2;
}
if (_root.MOVIECLIP1.hitTest(_x, _y-(_height/2), true)) {
this._y += 2;
}
}



This might help!


Bye-bye!

~kayla
flashkay is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:45 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.