Button To Reset Ball Position

Hi everyone, i.m fairly new to CORONA SDK.

I need to create button that resets the ball to its original position when it is pressed by the player.
here is my code so far… (Note: I have tried playing with the button widget and I can get a button to display, it’s making it perform the action of resetting the balls position that I’m struggling with. I have searched the internet for the past hour or so to no avail. Hopefully someone here can help me.

Best Regards,
Chris
display.setStatusBar( display.HiddenStatusBar )

[html]
– Create a background colour
local back = display.newRect(0, 0, display.contentWidth, display.contentHeight)
back:setFillColor(165, 210, 255)

–Adding physics
require(“physics”)
physics.start()

–Creating the ball
local myCircle = display.newCircle(display.contentWidth /15, display.contentHeight /8 -25, 12.5, 12.5 )
myCircle:setFillColor(255,0,0)

–Add physics to the ball
physics.addBody( myCircle, “dynamic”, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 12.5 } )

local ballretainer = display.newRect(0, 0, 25,25)
ballretainer:setFillColor(0, 210, 255)

ballretainer.xOrigin = display.contentWidth /15
ballretainer.yOrigin = display.contentHeight / 8

physics.addBody( ballretainer, “static”, { friction=0.5, bounce=0.3} )

function ballretainer:touch( event )
if event.phase == “began” then

self.markX = self.x – store x location of object
self.markY = self.y – store y location of object

ballretainer:removeSelf()

end

return true
end

ballretainer:addEventListener( “touch”, ballretainer) [import]uid: 173546 topic_id: 31010 reply_id: 331010[/import]

I was going to use write the code for you, but this is a learning exercise and you’re not far from the solution. So…

  1. The button (ballretainer) does not need to be a physics object.Eg: The HUD in Unreal Tournament never actually collides with your character!

  2. I’m not sure why you’re removing the ballretainer in its own event handler. Do you want the button to disappear afte the first use? Not wrong, just unusual.

  3. To move the ball you want to set the x and y value of the ball, not ‘self’ to the position of the button. I imagine thesis why you are removing the button… Because the ball is being moved to its location. Don’t make the button a physics object and this won’t be a problem.

  4. I don’t think you need to t the origin values of the retainer, just set the x and y values.

  5. Probably not important in your particular code, but in in general you should not force the position values of physics objects because this effectively fights with the physics engine. You should set the isBodyActive to false (effectively removing the ball from the physics world while you move it) the set it to true. You can probably ignore this unless you intend to force move objects often.

Make your changes, repost your code and let me know if you’re still having trouble.

Matt [import]uid: 8271 topic_id: 31010 reply_id: 123973[/import]

Below should work, but like most of my answers on here I just type it directly into the forum so it’s prone to syntax errors but the logic works. Just adjust the myCircleOrigin variables and, of course, the button size/placement.

[lua]local myCircleXOrigin = 50
local myCircleYOrigin = 50

function resetBalls()

myCircle.x = myCircleXOrigin
myCircle.y =myCircleYOrigin

end

btnResetMyBalls = widget.newButton{

label = “Reset balls”,
font = genFont,
fontSize = 16,
yOffset = -2,
labelColor = { default={ 65 }, over={ 0 } },
emboss = true,
height = 50,
width = 150,
onRelease = resetBalls
}
btnResetMyBalls.mode = “endless”
btnResetMyBalls.x = 50
btnResetMyBalls.y = 50[/lua] [import]uid: 147305 topic_id: 31010 reply_id: 123974[/import]

I was going to use write the code for you, but this is a learning exercise and you’re not far from the solution. So…

  1. The button (ballretainer) does not need to be a physics object.Eg: The HUD in Unreal Tournament never actually collides with your character!

  2. I’m not sure why you’re removing the ballretainer in its own event handler. Do you want the button to disappear afte the first use? Not wrong, just unusual.

  3. To move the ball you want to set the x and y value of the ball, not ‘self’ to the position of the button. I imagine thesis why you are removing the button… Because the ball is being moved to its location. Don’t make the button a physics object and this won’t be a problem.

  4. I don’t think you need to t the origin values of the retainer, just set the x and y values.

  5. Probably not important in your particular code, but in in general you should not force the position values of physics objects because this effectively fights with the physics engine. You should set the isBodyActive to false (effectively removing the ball from the physics world while you move it) the set it to true. You can probably ignore this unless you intend to force move objects often.

Make your changes, repost your code and let me know if you’re still having trouble.

Matt [import]uid: 8271 topic_id: 31010 reply_id: 123973[/import]

Below should work, but like most of my answers on here I just type it directly into the forum so it’s prone to syntax errors but the logic works. Just adjust the myCircleOrigin variables and, of course, the button size/placement.

[lua]local myCircleXOrigin = 50
local myCircleYOrigin = 50

function resetBalls()

myCircle.x = myCircleXOrigin
myCircle.y =myCircleYOrigin

end

btnResetMyBalls = widget.newButton{

label = “Reset balls”,
font = genFont,
fontSize = 16,
yOffset = -2,
labelColor = { default={ 65 }, over={ 0 } },
emboss = true,
height = 50,
width = 150,
onRelease = resetBalls
}
btnResetMyBalls.mode = “endless”
btnResetMyBalls.x = 50
btnResetMyBalls.y = 50[/lua] [import]uid: 147305 topic_id: 31010 reply_id: 123974[/import]