How to rotate an Image using Corona sdk with iPhone
I am a newbie to Corona. I have an image that is draggable over the screen. Now i want to apply rotation to that image object.
The code I currently have is:
myAnim1 = movieclip.newAnim{"ICQ.png"}
--foreground:insert( myAnim2 )
myAnim1.x = 20
myAnim1.y = 80
local function pressFunction()
myAnim1.alpha = 0.7
end
local function releaseFunction()
myAnim1.alpha = 1.0
end
-- Make 2nd sprite draggable
myAnim开发者_StackOverflow1:setDrag{
drag=true,
onPress=pressFunction,
onRelease=releaseFunction,
bounds = { 0, 0, 320, 480 }
}
local rotate = function( event )
myAnim1.rotation = event.x
end
myAnim1:addEventListener( "touch",rotate)
In this code the image rotates while I drag it. I want rotation to happen after dropping the image at some place on the screen.
Can anyone solve this? Thanks in advance
use this formula to rotate the object in corona sdk
local rotate = function(event)
if event.phase == "ended" then
myAnim1.rotation = math.ceil(math.atan2( (event.y - myAnim1.y), (event.x -myAnim1.x) ) * 180 / math.pi) + 90
end
end
i think it useful to u.........
In your release function you could add this:
myAnim1:rotate(0)
...and that will set the object to being "normal" (right side up).
You also might want to look at the code where you're doing the rotating -- you're setting the degree of rotation to the x coordinate on the screen where you're touching. If that's what you're wanting to do, you're good. It just seems weird. :)
You have a listener set that responds to all "touch" events. The problem is that "touch" events are dispatched the entire time your finger is touching the object, while you are dragging it around. If you only want to respond when you let go then you need to react to event.phase: http://developer.anscamobile.com/reference/index/eventphase-0
So your function would look like:
local rotate = function(event)
if event.phase == "ended" then
myAnim1.rotation = event.x
end
end
OR
Another approach is to listen for "tap" instead of "touch". Personally I'd go with the solution above for future flexibility in your code, but note that while "touch" events are dispatched the entire time the finger is touching, "tap" events are only dispatched when the finger is removed. So:
myAnim1:addEventListener("tap", rotate)
精彩评论