Hey guys - I'm trying to make a top down RPG game - similar to what you'd find in the Zelda or Pokemon games on portable consoles.
I was wondering if anybody could enlighten me with a simple collision detection - perhaps something similar to pixel collision? I'm probably making this confusing so feel free to post a question if you don't get what I'm after and I'll try and explain it better.
/////////////////////////////////////////////////////////////////////////////// // Position Character ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
char.x = 100; char.y = 100;
/////////////////////////////////////////////////////////////////////////////// // Character Movement ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
var speed:Number = 5; var leftButton:Boolean = false; var rightButton:Boolean = false; var upButton:Boolean = false; var downButton:Boolean = false;
I'm not sure how to add 100% pixel by pixel collision detection but I use a sort-of pixel collision detection instead. But I'll start with hitTestObject.
PHP Code:
if (circle.hitTestObject (triangle)) {
//They collided
}
hitTestObject checks if the bounding boxes of both objects intersect, a bounding box is the blue box that surrounds your movieclip when you select it. This is a truly awful way to detect collisions unless all your movieclips are squares or rectangles.
PHP Code:
if (veryelaborateshape.hitTestPoint (mouseX, mouseY, true)) {
//They collided
}
hitTestPoint takes 3 parameters, the x point, the y point, and whether to check using the bounding box or check every pixel. This is perfect for testing a shape with a point, such as if your mouse cursor is over a very elaborately drawn movieclip.
Now I don't know pixel by pixel testing but this is what I do to try to achieve that. Let's say I want to test collision between a treble clef (complex shape) and a triangle (less complex shape). In addition to the 2 movieclips, I would create a new one called mc_hitpoint and I would go into the triangle movieclip and place an instance of mc_hitpoint on each corner of the triangle. Then this is the code I would use
PHP Code:
for (var i = 0; i < triangle.numChildren; i++) {
if (triangle.getChildAt (i) is mc_hitpoint) {//The current child is a hit point
var hitpoint:MovieClip = MovieClip (triangle.getChildAt (i));
var globalpoint:Point = hitpoint.localToGlobal (new Point (0, 0));//Get the hit point's coordinates in the stage's coordinate space
if (treble.hitTestPoint (globalpoint.x, globalpoint.y, true)) {
//Triangle and treble collided
}
}
}
This method is better than using hitTestObject or hitTestPoint alone because you're not checking with a box or a single point on a triangle. And the great thing about this method is if you want to be more precise, all you do is add more instances of mc_hitpoint in the triangle movieclip.
For your purposes just choose whichever method you think will be best suited for you. If you need anymore detail I have no problem explaining them further.
So... using the code I provided - could you possibly explain a way in which I could implement it? Considering there is going to be a number of Movie Clips that would be used as blocks.
/////////////////////////////////////////////////////////////////////////////// // Position Character ///////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
char.x = 100; char.y = 100;
/////////////////////////////////////////////////////////////////////////////// // Character Movement //////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
var speed:Number = 5; var leftButton:Boolean = false; var rightButton:Boolean = false; var upButton:Boolean = false; var downButton:Boolean = false;
This is what I have so far - colliding with a single object - but if I write it this way this means im going to have to write MILLIONS of "if" statements.
I'm also having the problem where this doesn't work until I hit the center of the object and then I can't move away - the character freezes - please help?
The reason your attempt didn't work is because the the hit test is taking place in the wrong function. It should take place in the enter frame function. In your example, let's say char has already collided with the tree. So when you press the left button you set leftButton to true. But immediately you check if char is hitting the tree and it is, so you set all the button states to false. So you're not giving the enter frame function a chance to update char's position before checking for a collision.
This is an example of using my third method of collision. I chose the tree to be the complex shape and the character the simple shape for obvious reasons. In the character movieclip is a layer that contains hit points on 4 corners. The trees are added to their own container called trees because this is more organized and easier to access.
So in the enter frame function it enters a for loop that goes through every tree in trees, and on every iteration it enters another for loop that goes through every hit point in the character. So it checks each hit point to see if it collides with the current tree.
As for what happens once you collide, that's up to you because different game mechanics call for different responses.
if (tree.hitTestPoint (global.x, global.y, true)) {
//One of the hit points collided with the tree
trace ("A");
break;
}
Sorry to be a pain in the ass - but what would I have to enter in here to successfully stop all character movement. Previous tests I've done stop the character but if I try to move the character after collision - it doesn't move.
Any ideas?
Again - sorry for this - i'm new to as3 coding and im in real need of this