Automatically adjust collision box for scaled sprite

Got a useful code snippet? Share it here for others to use!

Automatically adjust collision box for scaled sprite

Postby Rybar » Fri May 18, 2012 10:50 pm

In my current game project, my player can change size, its a primary play mechanic. This presented a problem, as Axel (and flixel for that matter) don't take scale into account when creating the default collision box. Here's a snippet that handles that, in the update() of your AxSprite:

collisionBox holds the offset and bounding box of your sprite in its unscaled state.
Code: Select all
collisionBox = new AxRect(11,11,170,170); //x offset, y offset, width, height

//in your update:
//the following will correctly adjust our bounding box to match the scaled sprite
         offset.x= collisionBox.x + Math.floor(collisionBox.width  * -(scale.x - 1)/2);
         offset.y= collisionBox.y + Math.floor(collisionBox.height * -(scale.y - 1)/2);
         width = collisionBox.width * (scale.x);
         height = collisionBox.height * (scale.y);
Posts: 8
Joined: Mon Apr 23, 2012 8:01 am

Re: Automatically adjust collision box for scaled sprite

Postby nehal » Sat Sep 14, 2013 4:18 am

If anyone's using mappy as their tile editor I've modified the flixel exporter plugin to export AXGL maps. The file is attached to this post; it's in a zip because the lua extension wasn't allowed. Just unzip the file and drop it in the luascr folder for mappy. You will also need to update the MAPWIN.INI file to include the plugin. Open the ini file in a text editor and find the lua scripts section. Just add one more line for the new plugin.
We offer ccna security dumps complete collection of dumps including CISCO study guide to help you pass University of Cambridge exam on first of luck.
Posts: 1
Joined: Sat Sep 14, 2013 4:16 am

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 2 guests