|
-
Gross Pecululatarian
The elusive hitTest explained
Back by popular demand!
HitTest is a very useful, and can be used for just about anything (like ductape really). There are two main forms that it takes:
Code:
if(this.hitTest(another_clip)){
// Something
}
This just tests the bounding boxes of this, and another_clip. If they hit, then the contents of the if will run.
Code:
if(this.hitTest(x, y, true)){
// Something
}
This tests the point x,y against the contents of this, the actual shape and not the bounding box. The true is what makes it test against the shape and not the bounding box.
Anyone else want to continue?
(if it's an irrelevant post, it'll be edited out as this is a guide sticky, not just a general bantering one )
Last edited by Ed Mack; 11-02-2002 at 09:27 AM.
-
Strange Life
I am going to show you how to have a hitTest on your badguy so that if your hero touches it, it will decrease your life.
1.Goto your hero's instance box and name it "player"
2.Add a dynamic text box on your scene, and set it's variable to "life".
3.Add this code to Frame 1
Edit the life if you wish
4.Bring up the object actions panel for your enemy and add these actions:
Code:
onClipEvent (enterFrame) {
if (this.hitTest(_root.player)) {
_root.life -= 5;
if (_root.life == 0) {
_root.gotoAndStop(2);
}
}
}
These actions cause the variable "life" on _root (the one you set the dynamic text field to display) to go down by 5 each time the player and the enemy collide. It also makes the main timeline goto frame two, if the player runs out of life. You should put a gameover screen on frame two of _root.
There you go, test it out
[ed: Just tweaking a few readability things, thanks very much for this ]
Last edited by Ed Mack; 11-02-2002 at 09:27 AM.
-
ism
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
-
Gross Pecululatarian
Thanks! Is there much more that we can do?
Here's code I'm quite fond of using for moving the player. It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):
Code:
onClipEvent(load){
s = 5;
b = this.getBounds(this);
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
_x += x;
_y += y;
}
}
}
}
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
-
light11.ca
 Originally Posted by Ed Mack
Thanks! Is there much more that we can do?
Here's code I'm quite fond of using for moving the player. It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):
Code:
onClipEvent(load){
s = 5;
b = this.getBounds(this);
function move(x,y){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymin+1, true)){
if(!_root.hit.hitTest(_x+x+b.xmin+1, _y+y+b.ymax-1, true)){
if(!_root.hit.hitTest(_x+x+b.xmax-1, _y+y+b.ymax-1, true)){
_x += x;
_y += y;
}
}
}
}
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
move(0,-s);
}
if(Key.isDown(Key.DOWN)){
move(0,s);
}
if(Key.isDown(Key.LEFT)){
move(-s,0);
}
if(Key.isDown(Key.RIGHT)){
move(s,0);
}
}
Does anyone know how i can use this code to work in flash 8 or 2004? I tried it at school with MX and it worked fine. When i got home and tried with 8 it did not work. Can someone help/ pm me/ or email [email protected] thanks
-
Not PWD
Code:
if(this.hitTest(x, y, true)){
// Something
}
This tests the point x,y against the contents of this, the actual shape and not the bounding box. The true is what makes it test against the shape and not the bounding box.
This would be useful for, say, when leaving a level. Like your hero. once he hits a door or something, (this is for art-based), you would just set the right x and y coords and poof! Perfect for art based...
PAlexC: That's just Chuck Norris's way of saying sometimes corn needs to lay the heck down.
Gerbick: America. Stabbing suckers since Vespucci left.
-
SaphuA
-
Will this work with...
this works really nicely for art-based RPG's where you go from room to room, but is there any way to modify the code to make a large outdoor map that will start to scroll up/down/left/right as soon as the "hero" gets to the midpoint of the screen? If you still don't know what I'm talking about here's an example someone did that I'm trying to implement into my RPG engine, but i'm stumped on how to do it using the current script setup: http://www.geocities.com/mclelun/rpg.html
Any help would be appreciated. Thanx.
-
 Originally Posted by NeotiK
this works really nicely for art-based RPG's where you go from room to room, but is there any way to modify the code to make a large outdoor map that will start to scroll up/down/left/right as soon as the "hero" gets to the midpoint of the screen? If you still don't know what I'm talking about here's an example someone did that I'm trying to implement into my RPG engine, but i'm stumped on how to do it using the current script setup: http://www.geocities.com/mclelun/rpg.html
Any help would be appreciated. Thanx.
Its called art based scrolling.....
Or you can use tiles
check the knowledgebase
-
hello everyone.
i am makin a top down art based rpg game, and i have this code:
code:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
game.map._y += speed
}
}
if(Key.isDown(Key.DOWN)){
if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
game.map._y -= speed
}
}
if(Key.isDown(Key.LEFT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x -= speed
var po_x = game.player._x
}
}
}
i have some white circle movie clips (instance name limit) in a movie clip called map.
when it hits the limit movie clip i want it to stop, the problem with taht is it scrolls into the circle a bit before stoping (the player and the object partly overlap) and its easy to get stuck.
I want it to stop as soon as one edge ofd the player hits one edge of a wall.
tahts the first thing
the 2nd thing is that i have 7 or something of them circle movie clips in the map movie clip (all instanced named limit), and it only works for one. I need it so i can make many diffrent walls and boundaries of diffrent shapes and locations.
help is apreciated! ive been looking for days.
-
Pumpkin Carving 2008
 Originally Posted by ThrashBoy
hello everyone.
i am makin a top down art based rpg game, and i have this code:
code:
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
game.map._y += speed
}
}
if(Key.isDown(Key.DOWN)){
if(! game.map.limit.hitTest(game.player._x, (game.player._y + speed), true)) {
game.map._y -= speed
}
}
if(Key.isDown(Key.LEFT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(! game.map.limit.hitTest((game.player._x + speed), game.player._y, true)) {
game.map._x -= speed
var po_x = game.player._x
}
}
}
i have some white circle movie clips (instance name limit) in a movie clip called map.
when it hits the limit movie clip i want it to stop, the problem with taht is it scrolls into the circle a bit before stoping (the player and the object partly overlap) and its easy to get stuck.
I want it to stop as soon as one edge ofd the player hits one edge of a wall.
tahts the first thing
the 2nd thing is that i have 7 or something of them circle movie clips in the map movie clip (all instanced named limit), and it only works for one. I need it so i can make many diffrent walls and boundaries of diffrent shapes and locations.
help is apreciated! ive been looking for days.
You don't need to post this twice. I just answered your question here .
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
sorry
code: for (i=0; i<8; i++) {
onEnterFrame=function(){
if(Key.isDown(Key.UP)){
if(! game.map["limit"+i].hitTest(game.player._x, (game.player._y + speed - playerhs), true)) {
game.map._y += speed
}
}
if(Key.isDown(Key.DOWN)){
if(! game.map["limit"+i].hitTest(game.player._x, (game.player._y + speed + playerhs), true)) {
game.map._y -= speed
}
}
if(Key.isDown(Key.LEFT)){
if(! game.map["limit"+i].hitTest((game.player._x + speed - playerhs), game.player._y, true)) {
game.map._x += speed
}
}
if(Key.isDown(Key.RIGHT)){
if(! game.map["limit"+i].hitTest((game.player._x + speed + playerhs), game.player._y, true)) {
game.map._x -= speed
}
}
}
}
i now have another issue (i know that codes not very good above, because i just tryed to make it work) and i was wondreing if that was somewhat close to what i should be doing to make mutiple walls.
-
HI,
I just want to detect collision between two circles not with there bounding boxes but there actual shapes using shapeFlag property of flash.
I came acros this thread actual wherein the discussion is alrady on.
Anyway
we all know this is right
onCliEvent(enterFrame){
if(hitTest(_root._xmouse,_root._ymouse,true)){
trace(Hit is on);
}
}
This works fine with the _xmouse and _ymouse property.
But what If I want to do this witha movie clip but checking the actual shape of the movieclip not the bounding box.
something like :
onCliEvent(enterFrame){
if(hitTest(_root.mov._x,_root.mov._y,true)){
trace(Hit is on);
}
}
This also works but it works only when the center of the "mov" clip which is basically a circle is hit by the host movie clip i:e _x and _y of mov.
I want it to happen when the mov actaully collides iwth the host movielcip
So I treid
onCliEvent(enterFrame){
if(hitTest(_root.mov._x-_root.mov._width/2,_root.mov._x-
_root.mov._width/2,_root.mov._y-_root.mov._height/2,_root.mov._y+
_root.mov._height/2){
trace(Hit is on);
}
}
This is to detect the actual collison of the mov movie clip.
But this isnt happening.
Also I have heard that this type of hitTest method in flash consumes quiet an amount of memory resulting in lower fps.
Pls guide me if I am wrong somewhere.
thx
Where is this Flash Rocket Taking us ^|^
-
Gross Pecululatarian
The easiest way to check if two circles hit, is to work out the distance between the two circle's centers, and check if it less than both circle's radias (?) added together.
-
that worked, that really worked.
thx edmack that was gr8 help.
this is what I tried and got it. The important thing is that I have totally eluded the hitTest function of flash.
onClipEvent (enterFrame) {
x = _x-_root.two._x;
y = _y-_root.two._y;
h = Math.sqrt((x*x)+(y*y));
rad = _width/2;
radTwo = _root.two._width/2;
sumRad = rad+radTwo;
if (sumRad>=h) {
trace("yes");
}
}
onClipEvent(load){
startDrag("", true);
}
two is the other movie clip on the stage.
Infact I am working on a pool game with this help I can really move ahead. I will start posting threads based on this.
thx again.
Where is this Flash Rocket Taking us ^|^
-
I'm dope.
Originally posted by Ed Mack
It requires a clip on _root, called map containing the areas you're not allowed to walk on (It basically tests the bounding box of the object against an actual shape):
Could you explain that code a lil better, im not sure exactly how it works...?
-
NeotiK, your engine is looking sweet.
Ed Mack, I think someone got an 'A' in geometry.
••••••••••••••••••••••••••••••••••••••••••••••••••
Try out the all new Ball Drop 2.0!
http://www.mtsu.edu/~jwt2h/balldrop/
-
general question...
is there a proper way to do a hitTest that checks if some one is clicking on the object? if not are you supposed to use buttons to do this? i have seen many games where a click alters a MC... (shoot-em-up types obviously)
-joe
-
try this
PHP Code:
onClipEvent(mouseUp){
if(hitTest(_root._xmouse,_root._ymouse)){
//Your statements here;
}
}
Copy Paste the above code in movieclip.
Where is this Flash Rocket Taking us ^|^
-
don't need a "this." before the hitTest?
that's something good to know. just implied when inside a movieClip script?
••••••••••••••••••••••••••••••••••••••••••••••••••
Try out the all new Ball Drop 2.0!
http://www.mtsu.edu/~jwt2h/balldrop/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|