object:contentToLocal() usage

local function onScreenTap(event) touchX, touchY = event.target:contentToLocal (event.x, event.y) end Runtime:addEventListener("tap", onScreenTap)

This gives me the error: attempt to index field ‘target’ (a nil value)

Anyone know how to transfer the OS touch input to the stage co-ordinates?

Thank you.

I think “target” only works when you have attached the event listener to an object, rather than the Runtime.  

If you did something like this it should work:

local function onScreenTap(event) touchX, touchY = event.target:contentToLocal (event.x, event.y) end local someObjectInMyScene = display.newImageRect("myBackground", 1000, 2000) someObjectInMyScene:addEventListener("tap", onScreenTap)

Thank you - yes I’ve managed to get it working by using the background object.

Not having much fun with anchors but I’ll try and persevere!

Hi Alan,

Just to let you, and I guess anyone else trying to do this, know - I ended up leaving my stage rotated then created a simple function to translate the touch inputs to the stage co-ordinates. As far as I can tell there is no built in function to do so - only translate one group to another.

So I ended up not using contentToLocal at all, and the only anchors I needed were for fixing the stage.

Thanks again for taking the time to assist me.

Warm regards,

Nick

I think “target” only works when you have attached the event listener to an object, rather than the Runtime.  

If you did something like this it should work:

local function onScreenTap(event) touchX, touchY = event.target:contentToLocal (event.x, event.y) end local someObjectInMyScene = display.newImageRect("myBackground", 1000, 2000) someObjectInMyScene:addEventListener("tap", onScreenTap)

Thank you - yes I’ve managed to get it working by using the background object.

Not having much fun with anchors but I’ll try and persevere!

Hi Alan,

Just to let you, and I guess anyone else trying to do this, know - I ended up leaving my stage rotated then created a simple function to translate the touch inputs to the stage co-ordinates. As far as I can tell there is no built in function to do so - only translate one group to another.

So I ended up not using contentToLocal at all, and the only anchors I needed were for fixing the stage.

Thanks again for taking the time to assist me.

Warm regards,

Nick