Spawning A Ball Where I Click

So I’m making a putt putt game and I want the ball to spawn where i click on the screen. I currently have a click listener working called myTouchListener    I know its working because I have it print the X and Y coordinates of every click to the corona output screen. How do I 

local ball = display.newImage(“ball.png” , {bounce = .3})

ball.x = ((display.contentWidth/2)+62)

ball.y = (80)

ball:scale(.04,.04)

physics.addBody(ball, whiteballdata:get(“ball”))

Thats my code for the ball, and I want the ball.x and ball.y to spawn the ball at the click, how do i do this?

Thanks!

As you’re probably aware then, event.x and event.y will give you the coordinates of the touch event, so place your code inside the touch listener, or have it call a function to create the ball at that spot like so:

local ball ... local function spawnBall( x, y ) if ball and ball.x then -- ball already spawned, do not create a new one else ball = display.newImage( "ball.png", { bounce = 0.3 } ) ball.x, ball.y = x or ( display.contentWidth \* 0.5 ) + 62, y or 80 ball:scale( 0.04, 0.04 ) physics.addBody( ball, whiteballdata:get( "ball" )) end end local function myTouchListener( event ) if event.phase == "ended" or event.phase == "cancelled" then spawnBall( event.x, event.y ) end end

sweet that should work i think, thanks!

As you’re probably aware then, event.x and event.y will give you the coordinates of the touch event, so place your code inside the touch listener, or have it call a function to create the ball at that spot like so:

local ball ... local function spawnBall( x, y ) if ball and ball.x then -- ball already spawned, do not create a new one else ball = display.newImage( "ball.png", { bounce = 0.3 } ) ball.x, ball.y = x or ( display.contentWidth \* 0.5 ) + 62, y or 80 ball:scale( 0.04, 0.04 ) physics.addBody( ball, whiteballdata:get( "ball" )) end end local function myTouchListener( event ) if event.phase == "ended" or event.phase == "cancelled" then spawnBall( event.x, event.y ) end end

sweet that should work i think, thanks!