简体   繁体   English

如何在 iPhone 上使用 Corona sdk 旋转图像

[英]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.现在我想对该图像 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
myAnim1: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使用此公式在电晕 sdk 中旋转 object

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:在您的版本 function 中,您可以添加以下内容:

myAnim1:rotate(0)

...and that will set the object to being "normal" (right side up). ...这会将 object 设置为“正常”(正面朝上)。

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.您可能还想查看正在执行旋转的代码 - 您正在将旋转度数设置为您正在触摸的屏幕上的 x 坐标。 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.问题是“触摸”事件在您的手指触摸 object 的整个过程中发送,而您正在拖动它。 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如果您只想在让 go 时做出响应,那么您需要对 event.phase 做出反应: http://developer.anscamobile.com/reference/index/eventphase-0

So your function would look like:所以你的 function 看起来像:

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".另一种方法是听“tap”而不是“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.就个人而言,我将 go 与上述解决方案一起使用,以提高代码的未来灵活性,但请注意,虽然在手指触摸的整个过程中都会发送“触摸”事件,但仅在移除手指时才会发送“点击”事件。 So:所以:

myAnim1:addEventListener("tap", rotate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM