[AS2] shapeFlag hitTest still using the bounding box?

Subscribe to [AS2] shapeFlag hitTest still using the bounding box? 4 posts

avatar for kirt7788 kirt7788 120 posts
Flag Post

On one frame of my game, I have two movie clips. One is the boundary, which is a big block of paint with an empty path erased into it. The other is a player, who is meant to walk through the path. I’m trying to use a shapeflag hitTest so that I dont need to create tons of separate hitTests in order to make the player stay inside the path. My problem is that this current code is still using the boundary’s box to determine the hitTest. Does anybody know why this is?

onClipEvent(enterFrame) {
	if(this.hitTest(_root.dude.body._x,_root.dude.body._y,true)) {	
			_root.left=false;
			_root.right=false;
			_root.up=false;
			_root.down=false;
	   } else {
		   	_root.left=true;
			_root.right=true;
			_root.up=true;
			_root.down=true;
	   }
}

(I’m also using a separate movieclip inside the player called “body” for the hitTest since the players head is big and in the code for the player’s movement, left, right, up or down need to be true for the player to move)

 
avatar for MoonlaughMaster MoonlaughMaster 6660 posts
Flag Post
Originally posted by Mazoonist:

First, hitTest with shapeFlag works just fine, but there is one “gotcha” with its use. The x and y values that you give it MUST be expressed using global coordinates (the coordinate space of _root). With two clips on stage in the _root timeline, this is no problem. But since your two movie clips are nested within the “World” MovieClip, you’re in a different coordinate space from the “global” space. Even though your clips are both in the same coordinate space as each other, hitTest with shapeFlag won’t work unless you give it global coordinates for the x and y.

So all you have to do is convert the x and y that you have to global coordinates, then feed those converted values to the hitTest method. Here’s how: You have to create a generic object and give it x and y properties (no underscore in front of these properties). Set those x and y values equal to the local coordinates that you want to convert. Then call the localToGlobal method and give it the generic object you just made as an argument. You call the localToGlobal method of the object that “owns” the coordinate space containing the points you want to convert.

I hope this isn’t sounding too confusing, but hear me out: Since you are writing this hitTest inside an onEnterFrame, and that’s attached to “main” (which is your character), any reference to “this” inside the function is going to mean the “main” movie clip. So you want to convert Ftrx and Ftry, which are basically main’s _x and _y properties (which you’ve altered somewhat to get future values). Anyway, the “owner” of that coordinate space would be main’s _parent (which is the “World” movie clip, but the _parent reference will do just fine).

So you create a generic object, which I’ve called “charPoint” below, and give it x and y properties and set them equal to Ftrx and Ftry. Then you call the localToGlobal method of main’s _parent, and feed it that charPoint object as an argument. This method converts the x and y values in the charPoint object to stage coordinates. Then, you just use charPoint.x and charPoint.y in your hitTest (so here’s the magic code that works!):

If you don’t get this, click the cite link for the thread.

 
avatar for kirt7788 kirt7788 120 posts
Flag Post

I don’t think I do need to use the localtoGlobal command here. Both of these movie clips are on the same frame of the main timeline. This code is just written inside the boundary.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by kirt7788:

I don’t think I do need to use the localtoGlobal command here. Both of these movie clips are on the same frame of the main timeline. This code is just written inside the boundary.

Ahem:

_root.dude.body._x

That _x is NOT a global coordinate. It is a relative coordinate relative to the dude.