Hello,
I was thinking about creating a restart button which returns the objects moved by the player to their initial positions and their initial rotation. So lets say the player moves an object to a certain location and rotates it then clicks on the start button which applies physics on the objects and since the object is dynamic it will move to a new position, then the player clicks on the retry button and i want to create a function which returns the objects moved to the initial location set by the player.
I tried something out which didn’t work that well. I basically tried to save the location of the objects in the ended phase of the touch function and then try to set the x and y of the objects to their previous coordinates by using the retry button.
Here is the initial code:
-- Here is the code i used for rotating the objects: local function rotatePlatform(event) local t = event.target local phase = event.phase if (phase == "began") then display.getCurrentStage():setFocus( t ) platformTouched.isFocus = true -- Store initial position of finger platformTouched.x1 = event.x platformTouched.y1 = event.y elseif platformTouched.isFocus then if (phase == "moved") then platformTouched.x2 = event.x platformTouched.y2 = event.y angle1 = 180/math.pi \* math.atan2(platformTouched.y1 - platformTouched.y , platformTouched.x1 - platformTouched.x) angle2 = 180/math.pi \* math.atan2(platformTouched.y2 - platformTouched.y , platformTouched.x2 - platformTouched.x) print("angle1 = "..angle1) rotationAmt = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - rotationAmt print ("platformTouched.rotation = "..platformTouched.rotation) platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 elseif (phase == "ended") or (phase == "cancelled") then display.getCurrentStage():setFocus( nil ) platformTouched.isFocus = false display.remove( rotationalert ) rotationalert = nil end end return true end -- Here is the code i used to move the objects: local function movePlatform( event ) platformTouched = event.target transition.to(platformTouched, {onComplete = inventory}) if event.phase == "began" then display.getCurrentStage():setFocus( platformTouched ) platformTouched.isFocus = true audio.play ( selectionSound ) display.remove( rotationalert ) rotationalert = nil platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y elseif platformTouched.isFocus then if event.phase == "moved" then -- The line below is the 392th line, where the error arises: platformTouched.x = ( event.x - event.xStart ) + platformTouched.startMoveX platformTouched.y = ( event.y - event.yStart ) + platformTouched.startMoveY elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) platformTouched.isFocus = false end end return true end
Here is the move function with the extra code that i added to the ended phase. The code didnt work because it only saved the location of the last object moved using the function ( Im assuming that this was because platformtouched (event.target) could only have one set of coordinates?)
I’m hoping someone would be able to help me create this retry button.
platformTouched = event.target transition.to(platformTouched, {onComplete = inventory}) if event.phase == "began" then display.getCurrentStage():setFocus( platformTouched ) platformTouched.isFocus = true audio.play ( selectionSound ) display.remove( rotationalert ) rotationalert = nil platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y elseif platformTouched.isFocus then if event.phase == "moved" then -- The line below is the 392th line, where the error arises: platformTouched.x = ( event.x - event.xStart ) + platformTouched.startMoveX platformTouched.y = ( event.y - event.yStart ) + platformTouched.startMoveY elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) platformTouched.isFocus = false if platformTouched.name == "box" then platformTouched.endMoveX = platformTouched.x platformTouched.endMoveY = platformTouched.y elseif platformTouched.name == "woodenPlank1" then platformTouched.woodendMoveX = platformTouched.x platformTouched.woodendMoveY = platformTouched.y end end end return true end