Hey guys! I am new to FlashPunk however I have developed full blown games from the ground up using AS3 and no third party engines! I have a pretty well structured game so far and want to start to handle the collision aspect. Currently I have a test level, a hero and one block. The hero can move left and right at a predefined constant speed of 5. I also handled the code which keeps the character inside of the stage. (For now, I eventually will have a camera that follow the characters and creates a scrolling effect)
Anyway! For my collision question! I was having some difficulty to even get a trace statement to show me that there was a collision, I was pulling my hair out, trying to figure out how to use the FP collide function! I was using it correctly, my hitbox for the hero was apparently not set up correctly!
So here is my question, I have a ready to go nice neat collision.as file which I have used in many of my past titles. I changed the parameters to accepts Entities, however, I am not 100% clear on the fundamentals behind FlashPunk and how it actually processes its entities. My question is, is there some very strong apparent reason that this collision code does not work in my game? I have tried passing the information of both the entities that I would like to collide and it does not do anything!
Here is my collision.as file!
package
{
import entities.Block;
import entities.Controller;
import entities.Grass;
import entities.Hero;
import entities.Sky;
import net.flashpunk.FP;
import net.flashpunk.graphics.Spritemap;
import net.flashpunk.World;
import net.flashpunk.graphics.Text;
import net.flashpunk.graphics.Image;
import net.flashpunk.Sfx;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.Entity;
public class Collision
{
static public var collisionSide:String = "";
public function Collision()
{
}
static public function block(r1:Entity, r2:Entity):void
{
//Calculate the distance vector
var vx:Number
= (r1.x + (r1.width / 2))
- (r2.x + (r2.width / 2));
var vy:Number
= (r1.y + (r1.height / 2))
- (r2.y + (r2.height / 2));
//Check whether vx
//is less than the combined half widths
if(Math.abs(vx) < r1.width / 2 + r2.width / 2)
{
//A collision might be occurring! Check
//whether vy is less than the combined half heights
if(Math.abs(vy) < r1.height / 2 + r2.height / 2)
{
//A collision has ocurred! This is good!
trace("a collision has occured");
//Find out the size of the overlap on both the X and Y axes
var overlap_X:Number
= r1.width / 2
+ r2.width / 2
- Math.abs(vx);
var overlap_Y:Number
= r1.height / 2
+ r2.height / 2
- Math.abs(vy);
//The collision has occurred on the axis with the
//*smallest* amount of overlap. Let's figure out which
//axis that is
if(overlap_X >= overlap_Y)
{
//The collision is happening on the X axis
//But on which side? _v0's vy can tell us
if(vy > 0)
{
collisionSide = "Top";
//Move the rectangle out of the collision
r1.y = r1.y + overlap_Y;
}
else
{
collisionSide = "Bottom";
//Move the rectangle out of the collision
r1.y = r1.y - overlap_Y;
}
}
else
{
//The collision is happening on the Y axis
//But on which side? _v0's vx can tell us
if(vx > 0)
{
collisionSide = "Left";
//Move the rectangle out of the collision
r1.x = r1.x + overlap_X;
}
else
{
collisionSide = "Right";
//Move the rectangle out of the collision
r1.x = r1.x - overlap_X;
}
}
}
else
{
//No collision
collisionSide = "No collision";
}
}
else
{
//No collision
collisionSide = "No collision";
}
}
}
}
Please let me know what you guys think! I would like to use the collision.as simply because I am just used to it. In the past it has done everything that I need it to do, and I am scared of Flashpunk in this sense lol.
Thank you for your time!