[Resolved] Weird touch events?

As it said in the title, this is a bit weird.

I am trying to creating a function that when you tap a specific spot, an arrow appears. When the ball touches the arrow, the ball boosts and the arrow disappears.

But when I tap the scene in the simulator, the arrow doesn’t shows up in the spot where I tapped, it shows up in the bottom-right!

Typed the code myself. Maybe it’s because I’m not that experienced?

How can I make the image appear at the center of where I tapped?

Any help will be appreciated.

[code]
local function draw_liner(event)

if event.phase == “began” then

local blt = display.newImage( “blt.png”, event.x, event.y )
blt:setReferencePoint(CenterReferencePoint);
physics.addBody( blt, “static”, { isSensor = true } )

–Boost right function
local function onLocalCollisionbr( self, event )

if ( event.phase == “began” ) then

ball:applyLinearImpulse( 0.2, 0, ball.x, ball.y )
blt:removeSelf()

end
end

blt.collision = onLocalCollisionbr
blt:addEventListener( “collision”, blt )
return true

end

Runtime:addEventListener(“touch”, draw_liner) [import]uid: 44110 topic_id: 27227 reply_id: 327227[/import]

I am not positive about this but you seem to be having an issue similar to something that was going wrong with my code. I don’t know if this will work for you but try changing this:

[lua] local blt = display.newImage( “blt.png”, event.x, event.y )
blt:setReferencePoint(CenterReferencePoint);
physics.addBody( blt, “static”, { isSensor = true } )[/lua]

To this:

[lua] local blt = display.newImage( “blt.png”, 0, 0 )
blt.x = event.x; blt.y = event.y;
blt:setReferencePoint(CenterReferencePoint);
physics.addBody( blt, “static”, { isSensor = true } )[/lua]

I don’t know why but when I separate the calling of the image and the placement of the image, things seem to work more often. Let me know if that helped! [import]uid: 50511 topic_id: 27227 reply_id: 110585[/import]

It worked. Thanks! [import]uid: 44110 topic_id: 27227 reply_id: 110587[/import]

Cool! Glad to help! [import]uid: 50511 topic_id: 27227 reply_id: 110590[/import]

Just a question: Does the ,0,0 thing beside “blt.png” do anything? [import]uid: 44110 topic_id: 27227 reply_id: 110591[/import]

Well it sets the x and y position of the variable being initialized. But, I think the reason it does not work is because I don’t think you can add a dynamic variable into a initialization. In English, it is because the initialization only runs once and if there is a dynamic variable in there it will never adjust.

If you are going to place something in a spot and not move it or add physics to it then you can position it using that code. [import]uid: 50511 topic_id: 27227 reply_id: 110592[/import]

The difference between setting the x,y during display.newImage and after is that:

  1. Passing x,y as newImage parameter causes the top left of the image to be at x,y
  2. Setting x,y as properties after the image is created sets the centre (or where you have the reference point set) to x,y

The simpler version of your code is:

[lua]local blt = display.newImage( “blt.png” )
blt.x, blt.y = event.x, event.y
physics.addBody( blt, “static”, { isSensor = true } )[/lua]

Above, I have removed the centering of the reference point, because it is unnecessary.

I have also reduced the x,y setting to a more lua standard - which I, personally, find easier to read. I don’t think other languages have this.

For an Ansca description of what is happening, read this:

http://developer.anscamobile.com/reference/index/displaynewimage

Where it says that the x,y parameters are [left,top] and that you should keep in mind that setting x,y after loading the image is, by default, setting the centre location of the image.

You can see the same thing when create a rectangle, with newRect:

http://developer.anscamobile.com/content/displaynewrect

Where the parameters are left, top, width and height. Setting x,y after creating the rectangle sets the centre of the rectangle.

The following code loads image A and places it at 0,0:

[lua]local A = display.newImage(“img.png”,0,0)[/lua]

The following code also loads image A and places it at 0,0:

[lua]local A = display.newImage(“img.png”)
A.x, A.y = A.width/2, A.height/2[/lua]

I know this was a long description for something fairly short, but I hope this is clear now. It tripped me up more than once when I was starting out.

Matt. [import]uid: 8271 topic_id: 27227 reply_id: 110616[/import]