Make an object fall from map's bounds

Hello, I would like the player to be able to get out of the map, for example when he dies. Is there any way to do that ? Also, for the moment I’m using sensor tiles all around the map’s bounds to detect if the player falls out from map (collision). Is there another way to check if an object is within the map’s bounds ?

mini_140424074317146463.png

Thanks you very much,

Ulysse

Hello ulydev5,

Sprites are constrained to the map by default. You can set map constraints for each edge of the map individually by setting a sprite’s constrainToMap property, which takes a table of booleans.

--sprite.constrainToMap = {left, top, right, bottom} sprite.constrainToMap = {false, false, false, false}

You can also set this in a sprite’s setup table before calling addSprite. The table parameter is formatted the same way as the sprite’s property and has the effect of setting the sprite’s property just as above.

Checking whether an object is in the maps bounds is a matter of retrieving the size of the map and checking whether the sprite’s .x and .y properties are greater than 0 and less than the map size. If any one value is outside of this area, the map is outside of the map bounds. You may want to add the sprite’s width and height to these checks to determine when the entire sprite has left the map rather than just its origin.

local map = mte.getMap() local mapWidth = map.width \* map.tilewidth local mapHeight = map.height \* map.tileheight local gameLoop = function(event) if sprite.x \< 0 or sprite.x \> mapWidth or sprite.y \< 0 or sprite.y \> mapHeight then print("The sprite has left the map") end ... mte.update() end Runtime:addEventListener("enterFrame", gameLoop)

Hello dyson, thanks you for the reply, unfortunately “player.constrainToMap = {true,true,true,false}” doesn’t work for me. I even tried to put it in the setup table for addSprite but it doesn’t work either. The player keeps getting stuck when reaching the bottom of the map…

Can you set physics.setDrawMode(“hybrid”) after physics.start() and check whether the player’s physics body immediately turns grey or dark yellow when it gets stuck? If so one thing to try is setting the player’s offscreenPhysics parameter to true in the setup table. 

It immediately turns grey when it gets stuck. So I did what you said, setting the player’s offscreenPhysics parameter to true, and now there’s an even stranger issue : it gets stuck a few pixels lower than without offscreenPhysics. Plus, it doesn’t turn gray, it stays orange.

Alright, new plan. Remove the offscreenPhysics parameter, go to line 9910, and comment out this block of code:

local locX = math.ceil(object.x / map.tilewidth) local locY = math.ceil(object.y / map.tilewidth) if (locX \< masterGroup[i].vars.camera[1] - 1 or locX \> masterGroup[i].vars.camera[3] + 1) or (locY \< masterGroup[i].vars.camera[2] - 1 or locY \> masterGroup[i].vars.camera[4] + 1) then object.isBodyActive = false elseif (locX \< masterGroup[i].vars.camera[1] or locX \> masterGroup[i].vars.camera[3]) or (locY \< masterGroup[i].vars.camera[2] or locY \> masterGroup[i].vars.camera[4]) then object.isAwake = false object.isBodyActive = true else object.isAwake = true object.isBodyActive = true end

Still not able to leave the screen. The player is still active (orange rectangle), though.

At this point the quickest way for me to debug the problem would be to have it in front of me. If you’re willing to email the project to me I’ll have a look at it for you, try to figure out why its refusing to cooperate.

Could you not just do:

physics.removeBody( player )

When the player dies?

I received your project, but it was in the form of a built application. I need the raw project folder containing your lua code and assets so that I can open them in Simulator and my code editor.

Also I’m just going to throw this out here to cover all possible bases, but the sensor tiles you’re using to detect when a player leaves the map- they aren’t solid physics objects are they? The player isn’t falling to the edge of the map and landing on them? 

dyson solved the problem. Just had to set the contrainToMap property AFTER calling AddSprite. Thanks you very much, dyson. I should have tried that before asking you.

Hello ulydev5,

Sprites are constrained to the map by default. You can set map constraints for each edge of the map individually by setting a sprite’s constrainToMap property, which takes a table of booleans.

--sprite.constrainToMap = {left, top, right, bottom} sprite.constrainToMap = {false, false, false, false}

You can also set this in a sprite’s setup table before calling addSprite. The table parameter is formatted the same way as the sprite’s property and has the effect of setting the sprite’s property just as above.

Checking whether an object is in the maps bounds is a matter of retrieving the size of the map and checking whether the sprite’s .x and .y properties are greater than 0 and less than the map size. If any one value is outside of this area, the map is outside of the map bounds. You may want to add the sprite’s width and height to these checks to determine when the entire sprite has left the map rather than just its origin.

local map = mte.getMap() local mapWidth = map.width \* map.tilewidth local mapHeight = map.height \* map.tileheight local gameLoop = function(event) if sprite.x \< 0 or sprite.x \> mapWidth or sprite.y \< 0 or sprite.y \> mapHeight then print("The sprite has left the map") end ... mte.update() end Runtime:addEventListener("enterFrame", gameLoop)

Hello dyson, thanks you for the reply, unfortunately “player.constrainToMap = {true,true,true,false}” doesn’t work for me. I even tried to put it in the setup table for addSprite but it doesn’t work either. The player keeps getting stuck when reaching the bottom of the map…

Can you set physics.setDrawMode(“hybrid”) after physics.start() and check whether the player’s physics body immediately turns grey or dark yellow when it gets stuck? If so one thing to try is setting the player’s offscreenPhysics parameter to true in the setup table. 

It immediately turns grey when it gets stuck. So I did what you said, setting the player’s offscreenPhysics parameter to true, and now there’s an even stranger issue : it gets stuck a few pixels lower than without offscreenPhysics. Plus, it doesn’t turn gray, it stays orange.

Alright, new plan. Remove the offscreenPhysics parameter, go to line 9910, and comment out this block of code:

local locX = math.ceil(object.x / map.tilewidth) local locY = math.ceil(object.y / map.tilewidth) if (locX \< masterGroup[i].vars.camera[1] - 1 or locX \> masterGroup[i].vars.camera[3] + 1) or (locY \< masterGroup[i].vars.camera[2] - 1 or locY \> masterGroup[i].vars.camera[4] + 1) then object.isBodyActive = false elseif (locX \< masterGroup[i].vars.camera[1] or locX \> masterGroup[i].vars.camera[3]) or (locY \< masterGroup[i].vars.camera[2] or locY \> masterGroup[i].vars.camera[4]) then object.isAwake = false object.isBodyActive = true else object.isAwake = true object.isBodyActive = true end

Still not able to leave the screen. The player is still active (orange rectangle), though.

At this point the quickest way for me to debug the problem would be to have it in front of me. If you’re willing to email the project to me I’ll have a look at it for you, try to figure out why its refusing to cooperate.

Could you not just do:

physics.removeBody( player )

When the player dies?

I received your project, but it was in the form of a built application. I need the raw project folder containing your lua code and assets so that I can open them in Simulator and my code editor.

Also I’m just going to throw this out here to cover all possible bases, but the sensor tiles you’re using to detect when a player leaves the map- they aren’t solid physics objects are they? The player isn’t falling to the edge of the map and landing on them?