Issue with adding a plugin to a Tiled object

Hi, everyone, I am currently having a problem with attaching a plugin to a Tiled object using PonyTiled. As you may know, PonyTiled allows you to add custom code to certain objects on a map. I am having some trouble doing this. I have done it before, quite easily actually, I just don’t know why it is not working now.

Here’s the code I have so far:

local M = {} function M.new(instance) physics.addBody(instance, "static") instance.isSensor = true function instance:collision(event) if event and event.phase == "began" then if event.other.name == "character" then print("collided with door") end end end instance:addEventListener("collision") instance.name = "door" instance.type = "door" return instance end return M 

I hope it’s not something really simple. I haven’t looked at Java in a few weeks and I need to get back into the swing of things.

Be more specific. How do you add custom code to objects? Do you named objects on map right?

I started new project (github) based on _ponytiled _to add new functionality. So far I have added:

  • support for Text objects (you don’t need label plugin)
  • support for Groups

Have a nice day:)

I know the object on the map is named right. To add the custom code, I place a new file with the name of the object in the ponytiled/plugins folder, and then in that file, I return the object.

Could you share your code?

Does “character” object is physic object?

Check that :new function is called.

The code I posted above is the code for my door.lua file. Do you need any other code?

I would test all neccessary code related to your problem. It is simplest method for me to help you. In my opinion your plugin seems ok.  May be I can find your code on github?

@SDK Tester, I actually suspect that you and I are on similar tracks, with similar issues :slight_smile: - While I’m struggling with ladders, you are working on doors - I am getting to doors next :slight_smile:

Here’s the instance code I’ve used on ponytiled. I named it character to match your explanation.

The key, is in Tiled, I set the name and type to ‘door’, and then save this as the door.lua and extend it like you probably did already in game.lua

I can get the collision to work just fine, except with a ladder I don’t want the the edge of a player, I want him nearer the middle. This I struggle with, but hopefully this is what you need for doors.

-- Extends an object to act as a door -- Define module local M = {} local composer = require( "composer" ) local fx = require( "com.ponywolf.ponyfx" ) function M.new( instance ) if not instance then error( "ERROR: Expected display object" ) end -- Get current scene and sounds local scene = composer.getScene( composer.getSceneName( "current" ) ) local sounds = scene.sounds if not instance.bodyType then physics.addBody( instance, "static", { isSensor = true } ) end function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.name == "character" and not other.isDead then -- other:hurt() -- other:setLinearVelocity( 0, 0 ) -- other:applyLinearImpulse( math.random(700) - 350, -350 ) print( "At Door" ) end if phase == "ended" and other.name == "character" and not other.isDead then print( "Not @ Door" ) end end instance:addEventListener( "collision" ) return instance end return M

Note: In game.lua, make sure you added it at the line starting with… map:extend

Just as an addendum, for a ladder, I cloned the tile in tiled, made it really thin, and put the body type collision on that. Then I set Visible to false in Tiled.

The real ladder just appears as a tile. Worked quite well as it shows the ladder, but you can’t see the thin one.

(Screenshot added)

Just in case you run into a similar issue.

Oops, I forgot that, it works now. 

I have just been so accustomed to using one file in AP Computer Science (Java) that it might have rubbed off on Corona. 

I should probably start making checklists to do things like that.

Be more specific. How do you add custom code to objects? Do you named objects on map right?

I started new project (github) based on _ponytiled _to add new functionality. So far I have added:

  • support for Text objects (you don’t need label plugin)
  • support for Groups

Have a nice day:)

I know the object on the map is named right. To add the custom code, I place a new file with the name of the object in the ponytiled/plugins folder, and then in that file, I return the object.

Could you share your code?

Does “character” object is physic object?

Check that :new function is called.

The code I posted above is the code for my door.lua file. Do you need any other code?

I would test all neccessary code related to your problem. It is simplest method for me to help you. In my opinion your plugin seems ok.  May be I can find your code on github?

@SDK Tester, I actually suspect that you and I are on similar tracks, with similar issues :slight_smile: - While I’m struggling with ladders, you are working on doors - I am getting to doors next :slight_smile:

Here’s the instance code I’ve used on ponytiled. I named it character to match your explanation.

The key, is in Tiled, I set the name and type to ‘door’, and then save this as the door.lua and extend it like you probably did already in game.lua

I can get the collision to work just fine, except with a ladder I don’t want the the edge of a player, I want him nearer the middle. This I struggle with, but hopefully this is what you need for doors.

-- Extends an object to act as a door -- Define module local M = {} local composer = require( "composer" ) local fx = require( "com.ponywolf.ponyfx" ) function M.new( instance ) if not instance then error( "ERROR: Expected display object" ) end -- Get current scene and sounds local scene = composer.getScene( composer.getSceneName( "current" ) ) local sounds = scene.sounds if not instance.bodyType then physics.addBody( instance, "static", { isSensor = true } ) end function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.name == "character" and not other.isDead then -- other:hurt() -- other:setLinearVelocity( 0, 0 ) -- other:applyLinearImpulse( math.random(700) - 350, -350 ) print( "At Door" ) end if phase == "ended" and other.name == "character" and not other.isDead then print( "Not @ Door" ) end end instance:addEventListener( "collision" ) return instance end return M

Note: In game.lua, make sure you added it at the line starting with… map:extend

Just as an addendum, for a ladder, I cloned the tile in tiled, made it really thin, and put the body type collision on that. Then I set Visible to false in Tiled.

The real ladder just appears as a tile. Worked quite well as it shows the ladder, but you can’t see the thin one.

(Screenshot added)

Just in case you run into a similar issue.

Oops, I forgot that, it works now. 

I have just been so accustomed to using one file in AP Computer Science (Java) that it might have rubbed off on Corona. 

I should probably start making checklists to do things like that.