Collision help (collide()) not returning true


(Jazz) #1

I have a game entity which loads up the character/map etc the usual game things. In this it makes a call to my entity called “Map” which generates my Ogmo map. This works fine, it adds everything it should, in this function I loop through the entities found in the map and one of them is called “House”.

I set the hitbox of the house using setHitbox(80, 56); and I set the type to house using type = "house"; and I have also set the hitbox and type in player by using the following:

type = "player";
setHitbox(14, 24);

in my player entity file I have the following check function

public function checkCollide(Position:Point):Boolean {

    if (collide("house", Position.x, Position.y)) {
	    trace("true collide");
	    return true;
    } else {
	    //trace("False collide");
	    return false;
    }
}

and for the player controls I have the following

if (Input.check("run_up")) {
	if (!checkCollide(new Point(player.x, player.y))) {
		player.play("runUp");
		player.y -= speed;
	}
}

the problem is, checkcollide never returns true. I run over the house like a god! The way I tested to check if the values were setting correct was simply added moveBy(player.x, player.y, "house"); and this collides, it shows that the type is set correctly but why does my checkCollide always return false?

The problem with moveBy is that it doesn’t allow multiple types so it doesn’t seem very practical!


(Ultima2876) #2

MoveBy allows multiple types if you pass an array, eg:

["type1", "type2"]

(Jazz) #3

Oh ok, thanks! But any idea what’s going on with the other code I have?


(Jacob Albano) #4

Can you show the line where you call checkCollide()?


(Jazz) #5

Sure! run_up is defined as: Input.define("run_up", Key.W); this is repeated with all the movement keys. Movement is working fine


(Jacob Albano) #6

Sorry, I totally missed it when you posted it the first time. :expressionless:

The checkCollide() function is on your player class, right? When you pass (player.x, player.y), what is player?

Is it possible you’re calling checkCollide() from the wrong class? It might be checking the wrong entity’s collision at the player’s position. You might try something like this:

public function checkCollide(player:Entity):Boolean {
    // player.collide(), not this.collide()
    if (player.collide("house", player.x, player.y)) {
        trace("true collide");
        return true;
    } else {
        trace("False collide");
        return false;
    }
}

(Jazz) #7

The checkcollide function is within the player class correct! Player originally was public var player:Spritemap = new Spritemap(Assets.player_1, sprite_width, sprite_height);

I altered feeding the function just the simple variables x and y which are predefined by flashpunk and no different results occurred, I implemented your code and same result as well.


(Jazz) #8

Okay your indication of [quote=“jacobalbano, post:6, topic:407”] what is player? [/quote]

Hit the nail on the head. The co-ords the function was checking just simply were out of sync with the players location, once I changed the actual input increment (player.y -= speed;) to y -= speed; etc this fixed it.

I can’t believe it was such a simple problem :confused: I did not suspect this as the character moved with key presses so I figured using the var player was correct!

Thank you for your help :smiley:


(Jacob Albano) #9

Awesome, gad you were able to figure it out! For the future, it’s generally most effective to only change the position of graphics within the entity for graphical effects, as the collision functions work based on the entity’s position instead of that of the graphic. Might save you a few headaches in the future. :slight_smile: