Need Help, how to turn an Image 90 Degrees everytime I touch

Hi,

Am new to Corona.

I have an Image which i need to rotate 90 degrees every time i touch it until it makes a full 360 degree turn in it’s place. So, keep rotating when touched.

This is as far as I got with the Code. Would greatly appreciate help on doing it.

local Image01 = display.newImage("Image01.png")  
  
function move(event)  
  
 transition.to(Image01, { time=150; rotation= 90; })  
   
end  
Image01:addEventListener("touch", move)  
  

Thx

[import]uid: 10542 topic_id: 3198 reply_id: 303198[/import]

Try this:

[lua]Image01 = display.newRect( 120, 200, 80, 80 )
Image01:setFillColor( 255, 110, 0 )
Image01.targetRotation = 0
Image01.isRotating = false
Image01.isTouched = false

local checkKeepRot

function rotateObj(obj)
if obj.rotation >= 360 then
obj.rotation = obj.rotation - 360
end
obj.targetRotation = obj.targetRotation + 90
if obj.targetRotation > 360 then
obj.targetRotation = obj.targetRotation - 360
end
–print(“targetRot=”…obj.targetRotation)
transition.to(obj, { time=250, rotation = obj.targetRotation, onComplete=checkKeepRot })
obj.isRotating = true
end

function checkKeepRot(target)
if target.isTouched == true then
rotateObj(target)
else
target.isRotating = false
end
end

function move(event)
if event.phase==“began” then
if Image01.isRotating == false then
rotateObj(Image01)
end
Image01.isTouched = true
end
if event.phase==“ended” then
Image01.isTouched = false
end
end

Image01:addEventListener(“touch”, move)[/lua]

Cheers
Michael [import]uid: 5712 topic_id: 3198 reply_id: 9411[/import]

thanks,

That was great :slight_smile:

One more question, I noticed that the Image keeps rotating none stop while it is touched, or the mouse is held down in the simulator. how can I make it so, that it rotates only once when it is clicked, even if the mouse (or finger) is held down, and rotates again when it is re-clicked…
Thnks [import]uid: 10542 topic_id: 3198 reply_id: 9423[/import]

Rotate once… for 90 degrees or 360 degrees? [import]uid: 5712 topic_id: 3198 reply_id: 9427[/import]

rotate 90 degrees per click.

As of the current code, it rotates 90 and stops if you release the mouse. If the mouse is held down it keeps rotating 360.

Need it to rotate 90 degrees once and stop even if mouse is not released, then 90 again when clicked again.

thnx [import]uid: 10542 topic_id: 3198 reply_id: 9432[/import]

What should happen if you click on it again while it is still rotating? [import]uid: 5712 topic_id: 3198 reply_id: 9461[/import]

i think i figured it out.

i changed this part of the code, and it seems to be working fine.

  
function checkKeepRot(target)  
  
 target.isRotating = false  
  
end  
  

thanks again Michael. You’ve been a great help :slight_smile: [import]uid: 10542 topic_id: 3198 reply_id: 9520[/import]