How to detect the center (x,y) of a pressed object

Dear all,

I want to click something then print the center x, y of that object (say, some image)

local image = display. new image (“1.png”)

image:setReferencePoint( display.TopLeftReferencePoint )

imagex, imagey = image:localToContent(0,0)

print(imagex…imagey) – up here it works

function click (object, event)

contentx, contenty = object: localToContent(0,0)

print (contentx…contenty) --it doesn’t work and return nil for localToContent

end

image:addEventListener(“tap”,click)


I have also tried to use object.x, object.y

but it end up return the x,y of where I have clicked.

Thanks!

Did you try it with event.target instead of object?

[LUA]

local function click (event)

   local target = event.target

   contentx, contenty = target.x, target.y

end

[/LUA]

Also you can try to attach the function to the object.

[LUA]

local object = display.newImage(“1.png”)

function object:tap(event)

   contentx, contenty = self.x, self.y

end

object:addEventListener(“tap”)

[/LUA]

I hope one of those helps you out.

Thanks!

I have changed the (object) to (event) and it works now @@

actually, what is the different between them?

sometimes i think “event” stands for the (x,y) where a event happen,

sometimes I think “object” stands for the object that have event happened,

but at other timing they are not lol.

I’m glad the soultion works for you.

About the event thing, let me explain: Every time an eventListener notices an event and calls the related function, he sends a table with the event information. depending on the event these information can be very different.

In short and incomplete:

Tap event -> (target, numTap, x, y …)

Touch event -> (target, phase, x, y …)

Collision event -> (self, other, x, y, selfElment, otherElement …)

So it depends which properties you can use.

Did you try it with event.target instead of object?

[LUA]

local function click (event)

   local target = event.target

   contentx, contenty = target.x, target.y

end

[/LUA]

Also you can try to attach the function to the object.

[LUA]

local object = display.newImage(“1.png”)

function object:tap(event)

   contentx, contenty = self.x, self.y

end

object:addEventListener(“tap”)

[/LUA]

I hope one of those helps you out.

Thanks!

I have changed the (object) to (event) and it works now @@

actually, what is the different between them?

sometimes i think “event” stands for the (x,y) where a event happen,

sometimes I think “object” stands for the object that have event happened,

but at other timing they are not lol.

I’m glad the soultion works for you.

About the event thing, let me explain: Every time an eventListener notices an event and calls the related function, he sends a table with the event information. depending on the event these information can be very different.

In short and incomplete:

Tap event -> (target, numTap, x, y …)

Touch event -> (target, phase, x, y …)

Collision event -> (self, other, x, y, selfElment, otherElement …)

So it depends which properties you can use.