How do I move a circle object?

I am writing a program in Lua.

I drew a circle and want the circle to move up or down depending on which button is clicked. I am very new and have been reading Burton’s book. I have tried different ways to do this but am getting weird results. I commented out some of the attempts. Clicking on the button makes it move a little then go back into original position. The move button down function is commented out and yet clicked on the down button makes it move.  Can someone please point me in the correct direction?

local w = display.contentWidth local h = display.contentHeight local btnUp = display.newImage("btnUp.png") local btnDown = display.newImage("btnDown.png") --local wB = btnUp.width -- the buttons are the same size so only one set of sizes needs to be saved off --local hB = btnUp.height --[[Center the buttons based on display width and height --]] btnUp.x = w / 2 - 100 -- sets the left & right to half the display width btnUp.y = h -75 -- sets the up & down to 50px from bottom btnDown.x = w / 2 + 100 -- sets the left & right to half the display width btnDown.y = h -75 -- sets the up & down to 50px from bottom local circle1 = display.newCircle(150, 150, 50) circle1.strokeWidth=10 circle1:setFillColor(0,0,0) circle1:setStrokeColor(255,0,0) circle1.yReference = 110 function moveCirclesUp(event) --circle1.x = circle1.x - 50 --circle1.y = circle1.y - 50 transition.to(circle1, { y = circle1.y - 50}) end function moveCirclesDown(event) --circle1.x = circle1.x + 50 circle1.y = circle1.y + 50 --transition.to(circle1, {x = circle1.x , y = circle1.y + 50}) -- circle1:translate(0, 50) end btnUp:addEventListener("tap" , moveCirclesUp) btnDown:addEventListener("tap" , moveCirclesDown)

This program is in Lua.

I changed the circle object to a .png file. The idea is to place all the Olympic circles at the top of the screen. When the up button is clicked all the circles move up together, down button moves them all down. So now I can place them and move them but I have to comment out one function or the other to get one to work. If they are both uncommented neither works. When one function is commented out and the other function will work no matter which button is clicked. I must be missing something basic here. Can anyone look over my new code below and give me a clue:

local w = display.contentWidth local h = display.contentHeight local rect = display.newRect(5,5,w\*2,h\*2); rect.strokeWidth =3; rect:setFillColor(255,255,255); rect:setStrokeColor (0,0,0); local background = rect; local btnUp = display.newImage("btnUp.png") local btnDown = display.newImage("btnDown.png") local blueCircle = display.newImage("blue\_circle.png") local blackCircle = display.newImage("black\_circle.png") local redCircle = display.newImage("red\_circle.png") local yellowCircle = display.newImage("yellow\_circle.png") local greenCircle = display.newImage("green\_circle.png") local bW = blueCircle.width local bH = blueCircle.height blueCircle.x = w / 4 blueCircle.y = h / 10 blackCircle.x = w / 2 blackCircle.y = h / 10 redCircle.x = w - (w / 4) redCircle.y = h / 10 yellowCircle.x = w / 3 + 20 yellowCircle.y = h / 8 greenCircle.x = w / 4 + (2 \* bW) - 15 greenCircle.y = h / 8 --[[Center the buttons based on display width and height --]] btnUp.x = w / 2 - 100 -- sets the left & right to half the display width btnUp.y = h -75 -- sets the up & down to 50px from bottom btnDown.x = w / 2 + 100 -- sets the left & right to half the display width btnDown.y = h -75 -- sets the up & down to 50px from bottom --local function moveCirclesUp(event) -- blueCircle.y = blueCircle.y - 50 -- blackCircle.y = blackCircle.y - 50 -- redCircle.y = redCircle.y - 50 -- yellowCircle.y = yellowCircle.y - 50 -- greenCircle.y = greenCircle.y - 50 --end local function moveCirclesDown(event) blueCircle.y = blueCircle.y + 50 blackCircle.y = blackCircle.y + 50 redCircle.y = redCircle.y + 50 yellowCircle.y = yellowCircle.y + 50 greenCircle.y = greenCircle.y + 50 end --btnUp:addEventListener("tap", moveCirclesUp) btnDown:addEventListener("tap", moveCirclesDown)

Hi @mtercek

Both your code work.

I believe your miss out on commenting/uncommenting – .

1st code you did not – on line 2 of function moveCirclesDown which explain the move down.

2nd code uncomment the function moveCirclesUp and also the btnUp:addEventListener(“tap”, moveCirclesUp)

Now, both button should work.

Good Luck!

burhan

This program is in Lua.

I changed the circle object to a .png file. The idea is to place all the Olympic circles at the top of the screen. When the up button is clicked all the circles move up together, down button moves them all down. So now I can place them and move them but I have to comment out one function or the other to get one to work. If they are both uncommented neither works. When one function is commented out and the other function will work no matter which button is clicked. I must be missing something basic here. Can anyone look over my new code below and give me a clue:

local w = display.contentWidth local h = display.contentHeight local rect = display.newRect(5,5,w\*2,h\*2); rect.strokeWidth =3; rect:setFillColor(255,255,255); rect:setStrokeColor (0,0,0); local background = rect; local btnUp = display.newImage("btnUp.png") local btnDown = display.newImage("btnDown.png") local blueCircle = display.newImage("blue\_circle.png") local blackCircle = display.newImage("black\_circle.png") local redCircle = display.newImage("red\_circle.png") local yellowCircle = display.newImage("yellow\_circle.png") local greenCircle = display.newImage("green\_circle.png") local bW = blueCircle.width local bH = blueCircle.height blueCircle.x = w / 4 blueCircle.y = h / 10 blackCircle.x = w / 2 blackCircle.y = h / 10 redCircle.x = w - (w / 4) redCircle.y = h / 10 yellowCircle.x = w / 3 + 20 yellowCircle.y = h / 8 greenCircle.x = w / 4 + (2 \* bW) - 15 greenCircle.y = h / 8 --[[Center the buttons based on display width and height --]] btnUp.x = w / 2 - 100 -- sets the left & right to half the display width btnUp.y = h -75 -- sets the up & down to 50px from bottom btnDown.x = w / 2 + 100 -- sets the left & right to half the display width btnDown.y = h -75 -- sets the up & down to 50px from bottom --local function moveCirclesUp(event) -- blueCircle.y = blueCircle.y - 50 -- blackCircle.y = blackCircle.y - 50 -- redCircle.y = redCircle.y - 50 -- yellowCircle.y = yellowCircle.y - 50 -- greenCircle.y = greenCircle.y - 50 --end local function moveCirclesDown(event) blueCircle.y = blueCircle.y + 50 blackCircle.y = blackCircle.y + 50 redCircle.y = redCircle.y + 50 yellowCircle.y = yellowCircle.y + 50 greenCircle.y = greenCircle.y + 50 end --btnUp:addEventListener("tap", moveCirclesUp) btnDown:addEventListener("tap", moveCirclesDown)

Hi @mtercek

Both your code work.

I believe your miss out on commenting/uncommenting – .

1st code you did not – on line 2 of function moveCirclesDown which explain the move down.

2nd code uncomment the function moveCirclesUp and also the btnUp:addEventListener(“tap”, moveCirclesUp)

Now, both button should work.

Good Luck!

burhan