New to FlashPunk, however have developed with AS3 before!


(Nate ) #1

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!


(TheHuckleberryWill) #2

Hey, not exactly answering your question (my AS3 is not as good as I would like)

So, I would just like to point out a awesome way of moving entities in FlashPunk:

moveBy(xAmount, yAmount, "Things that are solid");

In the string parameter, you pass it the type of the entities to consider as “solid” (meaning it will collide with it). You can also pass it an array of strings!


(Jacob Albano) #3

The move functions are invaluable. They’ll save you so much code.

If you aren’t sure that your hitbox is set up correctly, it might be helpful to enable the debug console.

FP.console.enable();

You can open it up with the ~ key to be able to see hitboxes and positions of entities.


(Nate ) #4

Okay guys! I will look into this function! Thank you so much!


(Nate ) #5

Sorry for the double post guys!

I was wondering what the simplest way to create an array of the same block image and have them in my test world would be.

What I have so far creates the array and places the boxes in the world. However for some reason only the hit detection for one of the boxes is functioning properly.

TestWorld.as

for (var b:int = 0; b < 50; b++)
{
    var space:int = 60;
    GameVariables.blockLocation = [[b * space, FP.rand(FP.stage.height)]];
    
    for (var c:int = 0; c < GameVariables.blockLocation.length; c++)
    {
    
    GameVariables.block1 = new Block();
    this.add(GameVariables.block1);

    GameVariables.block1.x = GameVariables.blockLocation[i][0];
    GameVariables.block1.y = GameVariables.blockLocation[i][1];

    
    }
    
}

This is my hero.as class

if (collide("block", x, y))
{
    v.y = 0;
    
    for (var c:int = 0; c < GameVariables.blockLocation.length; c++)
    {
        y = GameVariables.blockLocation[c][1] - height;
        
        trace("colliding with " + GameVariables.blockLocation[c] [1]);
    }
}

I feel like I am very close but am pretty draining I have been messing with the same few lines of codes for quite some time now!

Once again sorry for the double post! I thought I would at least keep it in the same thread.


Collision with a large amount of block objects
(Jacob Albano) #6

It’s actually a good idea to split unrelated questions into multiple threads; there’s no limit on them, and giving each question its own topic is a great way to help future readers to find answers. I’ll reply to your latest question as a new thread,