## Recommended Posts

##### Share on other sites
You could indeed use MovieClips for those impassable area's and simply use the MovieClip.hitTest function, but if memory and speed are a concern, it's better to store these area's as simple rectangles, where each rectangle has a position (x, y) and a width and height. You could write a class for it, to make these rectangles easier to work with. It would be handy to write a collision check function that takes a movieclip and checks if it collides with a rectangle, too.

Oh, a tip for posting here: you can put code between code tags, as explained in the FAQ page. It makes posts easier to read. :)

##### Share on other sites
Thank you so much for your info. About what you explain there, is there a code you can show me so I can take it as an example?

Thank you once again!

yayo

##### Share on other sites
It's quite simple really, as it's just a collision text between two axis-aligned bounding boxes. You just use the movieclip position, width and height and check it against your own rectangles position, width and height.

So what specifically do you need help with? Writing a rectangle class? Writing the collision test? How to react to the collision? How to use these rectangles? Or something else?

##### Share on other sites
Quote:
 Original post by Captain PIt's quite simple really, as it's just a collision text between two axis-aligned bounding boxes. You just use the movieclip position, width and height and check it against your own rectangles position, width and height.So what specifically do you need help with? Writing a rectangle class? Writing the collision test? How to react to the collision? How to use these rectangles? Or something else?

Thank you for your info. Yes, I need help on writting a code so the hitTest with the box works perfectly, not only there, but in all the other parts of the world. Besides, it has a "bug", if you keep touching it, if you want to get out, you can't, how can we fix that?

Thank you so much for your time!

yayo

##### Share on other sites
Right then, here we go. Personally, I'm using HaXe, not Actionscript, so the syntax may not be completely correct, but that shouldn't be too hard to change:

class Rectangle{	public var x : Number;	public var y : Number;	public var width : Number;	public var height : Number;		// Constructor	function Rectangle(x : Number, y : Number, width : Number, height : Number)	{		this.x = x;		this.y = y;		this.width = width;		this.height = height;	}		// This function returns true when the given movieclip collides with this rectangle.	// The code is somewhat obfuscated, but what it really does is checking if the movieclip	// is not totally to the left, not totally to the right, not totally under and not totally above this rectangle,	// in that order.	function CollidesWith(mc : MovieClip)	{		return (mc._x + mc._width > this.x && mc._x < this.x + this.width && mc._y + mc._height > this.y && mc._y < this.y + this.height);	}}

As for the player getting stuck, I don't know how you currently react to collisions, but it sounds like you stop the players movement if he collides. That's a bad thing to do, because it's easy to end up with a situation where, no matter how the player moves, he'll still be stuck, which means he'll never get out of that object. Instead, it's better to push the player out of the object he's colliding with. But I'll leave the implementation of that up to you. ;)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978753

• 21
• 14
• 12
• 39