Sign in to follow this  

Collision detection problems in Flash! Please Help!

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys from the net. Our club is developing some games, one of them is a RPG. But I encounter some problems with the CD (it has some bugs BTW), I will paste the code: <code> contador = 0; estado = null; avaen_mc.onEnterFrame = function() { if (avaen_mc.hitTest(limite1_mc)) { if(Key.isDown(Key.UP)) { avaen_mc.gotoAndStop(2); estado = "arriba"; carcel_mc._y += 0; limite1_mc._y += 0; } else if(Key.isDown(Key.DOWN)) { avaen_mc.gotoAndStop(3); estado = "abajo"; carcel_mc._y -= 0; limite1_mc._y -= 0; } else if(Key.isDown(Key.LEFT)) { avaen_mc.gotoAndStop(4); estado = "izquierda"; carcel_mc._x += 0; limite1_mc._x += 0; } else if(Key.isDown(Key.RIGHT)) { avaen_mc.gotoAndStop(5); estado = "derecha"; carcel_mc._x -= 0; limite1_mc._x -= 0; } else { avaen_mc.gotoAndStop(1); avaen_mc.estado_mc.gotoAndStop(estado); } } else { if(Key.isDown(Key.UP)) { avaen_mc.gotoAndStop(2); estado = "arriba"; carcel_mc._y += 5; limite1_mc._y += 5; } else if(Key.isDown(Key.DOWN)) { avaen_mc.gotoAndStop(3); estado = "abajo"; carcel_mc._y -= 5; limite1_mc._y -= 5; } else if(Key.isDown(Key.LEFT)) { avaen_mc.gotoAndStop(4); estado = "izquierda"; carcel_mc._x += 5; limite1_mc._x += 5; } else if(Key.isDown(Key.RIGHT)) { avaen_mc.gotoAndStop(5); estado = "derecha"; carcel_mc._x -= 5; limite1_mc._x -= 5; } else { avaen_mc.gotoAndStop(1); avaen_mc.estado_mc.gotoAndStop(estado); } texto_txt.text = "no"; } } // DETECCIION DE COLOSIONES //avaen_mc.onEnterFrame = function() { //} </code> how will be the easiest way to do this without spending memory? The game on the cellphone will become very slow if we put too many MC on the scene. The blue marks are the places where the char will not pass. We are transfering the game to cellphone too! Here is the game: http://cdv.preisc.com/games/thegameBest.swf Have a look! Thank you yayo

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Captain P
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?


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 this post


Link to post
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. ;)

Share this post


Link to post
Share on other sites

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this