Entities not colliding


(Noah) #1

Two objects in my game are not colliding and I have no idea why…

I have one Entity for all the background objects and one for the player. One of the props is a door and it has collision associated with it that allows for the player to open the door and move to the next area.

Here’s the code for the Prop entity:

import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import net.flashpunk.utils.*;
import net.flashpunk.graphics.Image;

public class Prop extends Entity
{

	[Embed('assets/props/bedsidetable.png')]
	private const BEDSIDETABLE:Class;
	
	[Embed('assets/backgrounds/apartment.png')]
	private const APARTMENT:Class;
	
	[Embed('assets/props/door.png')]
	private const DOOR:Class;

	public function Prop(x:Number,y:Number,type:String)
	{
		super(x,y);
		if(type == "bedsidetable")
		{
			graphic = new Image(BEDSIDETABLE);
		}
		if(type == 'apartmentB')
		{
			graphic = new Image(APARTMENT);
		}
		if(type == 'door')
		{
			graphic = new Image(DOOR);
			setHitbox(50,100);
			type = 'doorO';
		}
	}

}

and here is the code for the Player

import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.Graphic;
import net.flashpunk.utils.*;
import net.flashpunk.Sfx;
import net.flashpunk.FP;
import net.flashpunk.*;

public class Hero extends Entity{
	
	public var sprintDistance:Number = 100;
	public var speed:Number = 1;
	public static var xRestrictionOne:Number = 30;
	public static var xRestrictionTwo:Number = 725;
	
	[Embed('assets/protagonist/idle.png')] private const TEST:Class;

	public function Hero(setx:Number,sety:Number,type:String) {
		graphic = new Image(TEST);
		super(setx,sety);
		if(type == 'hero')
		{
			setHitbox(38,82);
			type = 'protag';
		}
	}
	
	override public function update():void
	{
		if(Input.check(Key.RIGHT))
		{
			if(this.x < xRestrictionTwo)
			{
				this.x += speed;
			}
		}
		if (this.collide('door',x,y))
		{
			trace('test');
		}
		if(Input.check(Key.LEFT))
		{
			if(this.x > xRestrictionOne)
			{
				this.x -= speed;
			}
		}
		if(Input.check(Key.SHIFT))
		{
			if(sprintDistance > 0)
			{
				if(Input.check(Key.RIGHT) || Input.check(Key.LEFT))
				{
					sprintDistance -= 1;
					if(speed < 4)
					{
						speed += 1;
					}
				}
			}
			if(sprintDistance == 0)
			{
				speed = 1;
			}
		}
		if(!Input.check(Key.SHIFT))
		{
			speed = 1;
			
			if(sprintDistance < 100)
			{
				sprintDistance += 1;
			}
		}
		if(this.x > xRestrictionTwo)
		{
			this.x = xRestrictionTwo;
		}
		if(this.x < xRestrictionOne)
		{
			this.x = xRestrictionOne;
		}
	}
}

Thank you for any help you can offer.


(Ortson) #2

In the prop class you say if type = “door”, type=“door0”. And then you check for collision with “door” in the player class. I would simply suggest removing type=“door0” in the props class and everything should work!


(Martí Angelats i Ribera) #3

Actually you are overwritting the variable type wich in this context is a constructor variable. I would suggest to use

this.type = type;

At the end of the contructor (outside of any if). This will work for any type you use, so you don’t have to set it manually for every type you want. FP doesn’t care if you define a unused type becouse it actually have a default value so overwritting it won’t hurt.


Also (and as asside note) you don’t need the this in

if (this.collide("door", x, y))

you can simply use

if (collide("door", x, y))

Don’t missunderstand me. This is right and it will work as well but be aweare that both codes are exactly the same but the second one is the most commonly used.


PS: If you want to set them manually, you should do:

this.type = "door";

becouse type is also a constructor parameter.


(Noah) #4

Thank you for your help.

Setting the type wasn’t the problem, as I had been experimenting with changing the type to things other than it was already to see if that was the problem.

I was able to fix it by using:

this.type = "door";

as suggested by @Copying

Thank you all for your help.