Collision detection kit with FlashPunk


(Zouhair Elamrani Abou Elassad) #1

I started creating my flash game using Flashpunk, for collisions i didn’t want to use hitboxes because i had images (PNG) ans sprite sheets with transparent parts in them so i decided to use Collision Detection Kit, i have a problem when creating the collision list, it takes a display object as a parameter and doesn’t accept flash punk spritemaps, i tried to cast the spritemap to a flash display object but it’s not working, is there a way to use the CDK with flashpunk ?

override public function begin():void 
{

    _player = new Player(100, 100);// Entity

    initCollision(_player.sprPlayer);// The Entity Spritemap
}


private function initCollision(player:Spritemap):void {

    collisionChecker = new CollisionList(player); // Problem here

}

Need Help with FlashPunk Collision Detection
(Jacob Albano) #2

The CDK won’t work with Flashpunk at all. It relies on each object being a DisplayObject (and, last I checked, added to a common parent), and Flashpunk only uses one DisplayObject; the Engine class.

In my opinion pixel-perfect collision is unneccessary a good 90% of the time, but if you really do need it I suggest you look into Flashpunk’s Pixelmask class.


(Zouhair Elamrani Abou Elassad) #3

… I tried to implement it the way you said, i have a several level images, i created mask for every one of them, i created the mask for my player as well and then checked for collisions but nothing happens, this is my code :

 private function initLevelImage(type:int) {
		switch(type) {
			case 1:
				_levelImage = new Image(GameConstants.LEVEL_ART_01);
				mask = new Pixelmask(GameConstants.LEVEL_ART_01, x, y);
				break;
			case 2:
				_levelImage = new Image(GameConstants.LEVEL_ART_02);
				mask = new Pixelmask(GameConstants.LEVEL_ART_02, x, y);
				break;
			case 3:
				_levelImage = new Image(GameConstants.LEVEL_ART_03);
				mask = new Pixelmask(GameConstants.LEVEL_ART_03, x, y);
				break;
			case 4:
				_levelImage = new Image(GameConstants.LEVEL_ART_04);
				mask = new Pixelmask(GameConstants.LEVEL_ART_04, x, y);
				break;
			default :
				_levelImage = new Image(GameConstants.LEVEL_ART_05);
				mask = new Pixelmask(GameConstants.LEVEL_ART_05, x, y);
				break;
  }}

for my player (spritemap) :

       [Embed(source="../../assets/images/beat.png")]
	protected static const PLAYER_ART:Class;
	
	public var sprPlayer:Spritemap = new Spritemap(PLAYER_ART, 400, 300); 
        mask = new Pixelmask(PLAYER_ART, x, y);

and then check for collisions :

     var _levelList:Vector.<Level> = new Vector.<Level>();
 
		
		getType("level", _levelList);
		
		for each (var level:Level in _levelList)
			{
				if (level.collideWith(_player, level.x, level.y))
				{

					trace("lost");
				}
			}

(Jacob Albano) #4

Here’s a sneaky way to get rid of a lot of that code:

private function initLevelImage(type:int) {
    if (type < 1 || type > 4) type = 5;  // handle the default case
    var source:Object = GameConstants["LEVEL_ART_0" + type];
    _levelImage = new Image(source);
    mask = new Pixelmask(source, x, y);
}

You can also cut out the manual iteration through levels by just doing this:

var level:Entity = _player.collide("level", _player.x, _player.y);
if (level != null) {
    trace("yay");
}

Now, on to figuring out what’s not working. I suspect you’re placing the masks in the wrong place, by passing x, y to the constructor. You could try passing 0, 0 instead to make it line up with the entity. Masks and images always follow the entity they’re attached to, Try enabling the debug console and toggling it on to see where the masks are being placed.

FP.console.enable();

Then press the tilde key; ~.


(Zouhair Elamrani Abou Elassad) #5

… You are right, the problem was in the x,y parameters passed to the mask, the 0,0 did the trick, thank you so much, it took me a while to figure out the problem cuz i had a null reference in a variable but flash develop kept crashing without giving me any errors, i’m launching the debug in a new pop up window because i have a problem showing traces in console, i hope that has nothing to do with flashdevelop hanging, another thing is that tha sprite sheet used for the player is a large image so when i add the spritemap to the stage i scale it but the mask is set based on the large image and not the scaled one and collisions were already being launched, i’m testing now with a simple image and it’s working great but what if i want the mask to be set based on the scaled spritemap for the player, once again thanks a lot for the feedback and for the sneaky way to get rid of all that code that was smart :slight_smile: