Rotation function isn't working?

I have this rotate platform function provided by christian peeters, however it doesn’t seem to be working, could anyone shed some light?

First I have the function to move the platform which works correctly, but when the rotation is added in corona just crashes.

local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) display.remove( rotationalert ) rotationalert = nil -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif (event.phase == "moved") then -- here the distance is calculated between the start of the movement and its current position of the drag 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 rotationalert = display.newImage ("images/rotation.png") rotationalert.x = platformTouched.x rotationalert.y = platformTouched.y rotationalert.alpha = 0.5 rotationalert:addEventListener ("touch", rotatePlatform) group:insert(rotationalert) display.getCurrentStage():setFocus( nil ) end return true end platform:addEventListener("touch", movePlatform) ------------------------------------------------------------------ local function rotatePlatform(event) alerttouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( alerttouched ) elseif (event.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) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) display.remove( rotationalert ) rotationalert = nil end end

Is it hooked up to a touch event listener?

Yes, these two

platform:addEventListener(“touch”, movePlatform)

rotationalert:addEventListener (“touch”, rotatePlatform)

The error is because you are referencing rotatePlatform in movePlatform, but rotatePlatform is defined after movePlatform.

The simplest fix is to move your rotatePlatform code so it is above the movePlatform code. Also, you need to add a “return true” to the bottom of the rotatePlatform code, otherwise the touch will propagate through the rotationAlert object and onto the platform object.

local function rotatePlatform(event) alerttouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( alerttouched ) elseif (event.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) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) display.remove( rotationalert ) rotationalert = nil end return true end local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) display.remove( rotationalert ) rotationalert = nil -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif (event.phase == "moved") then -- here the distance is calculated between the start of the movement and its current position of the drag 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 rotationalert = display.newRect(0, 0, 50, 50) rotationalert.x = platformTouched.x rotationalert.y = platformTouched.y -- platformTouched.height rotationalert.alpha = 0.5 rotationalert:addEventListener ("touch", rotatePlatform) group:insert(rotationalert) display.getCurrentStage():setFocus( nil ) end return true end

In fact you could even condense this down into 1 function, since the rotationAlert seems to be used to rotate the original platform.

Just give the platform a property (I’ve called it “touchMode” which can be toggled between “move” and “rotate” and then perform the necessary code in an if statement:

local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) if platformTouched.touchMode == "move" then -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif platformTouched.touchMode == "rotate" then end elseif (event.phase == "moved") then if platformTouched.touchMode == "move" then -- here the distance is calculated between the start of the movement and its current position of the drag platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY elseif platformTouched.touchMode == "rotate" 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) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 end elseif event.phase == "ended" or event.phase == "cancelled" then --each time we release the platform, toggle between move and rotate mode if platformTouched.touchMode == "move" then platformTouched.touchMode = "rotate" else platformTouched.touchMode = "move" end display.getCurrentStage():setFocus( nil ) end return true end platform:addEventListener("touch", movePlatform)

The first touch will move the platform, the next will rotate, the next will move again and so on.

Cheers man, worked great.

Another question, I don’t expect you to answer, but you wouldn’t happen to know how to make an inventory like function like the game “feed me oil” would you?

Is it hooked up to a touch event listener?

Yes, these two

platform:addEventListener(“touch”, movePlatform)

rotationalert:addEventListener (“touch”, rotatePlatform)

The error is because you are referencing rotatePlatform in movePlatform, but rotatePlatform is defined after movePlatform.

The simplest fix is to move your rotatePlatform code so it is above the movePlatform code. Also, you need to add a “return true” to the bottom of the rotatePlatform code, otherwise the touch will propagate through the rotationAlert object and onto the platform object.

local function rotatePlatform(event) alerttouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( alerttouched ) elseif (event.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) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) display.remove( rotationalert ) rotationalert = nil end return true end local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) display.remove( rotationalert ) rotationalert = nil -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif (event.phase == "moved") then -- here the distance is calculated between the start of the movement and its current position of the drag 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 rotationalert = display.newRect(0, 0, 50, 50) rotationalert.x = platformTouched.x rotationalert.y = platformTouched.y -- platformTouched.height rotationalert.alpha = 0.5 rotationalert:addEventListener ("touch", rotatePlatform) group:insert(rotationalert) display.getCurrentStage():setFocus( nil ) end return true end

In fact you could even condense this down into 1 function, since the rotationAlert seems to be used to rotate the original platform.

Just give the platform a property (I’ve called it “touchMode” which can be toggled between “move” and “rotate” and then perform the necessary code in an if statement:

local function movePlatform(event) platformTouched = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( platformTouched ) if platformTouched.touchMode == "move" then -- here the first position is stored in x and y platformTouched.startMoveX = platformTouched.x platformTouched.startMoveY = platformTouched.y platformTouched.x1 = event.x platformTouched.y1 = event.y elseif platformTouched.touchMode == "rotate" then end elseif (event.phase == "moved") then if platformTouched.touchMode == "move" then -- here the distance is calculated between the start of the movement and its current position of the drag platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY elseif platformTouched.touchMode == "rotate" 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) differencebetweenangles = angle1 - angle2 --rotate it platformTouched.rotation = platformTouched.rotation - differencebetweenangles platformTouched.x1 = platformTouched.x2 platformTouched.y1 = platformTouched.y2 end elseif event.phase == "ended" or event.phase == "cancelled" then --each time we release the platform, toggle between move and rotate mode if platformTouched.touchMode == "move" then platformTouched.touchMode = "rotate" else platformTouched.touchMode = "move" end display.getCurrentStage():setFocus( nil ) end return true end platform:addEventListener("touch", movePlatform)

The first touch will move the platform, the next will rotate, the next will move again and so on.

Cheers man, worked great.

Another question, I don’t expect you to answer, but you wouldn’t happen to know how to make an inventory like function like the game “feed me oil” would you?