Collision detection problems in Flash! Please Help!

Started by
4 comments, last by Captain P 16 years, 4 months ago
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
Advertisement
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. :)
Create-ivity - a game development blog Mouseover for more information.
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
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?
Create-ivity - a game development blog Mouseover for more information.
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

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-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement