简体   繁体   中英

Image rotate and show transition in Corona SDK

I am trying to click on the screen and have my tank barrel rotate to where I click. I have gotten that piece of code to work as of right now. The next step that I am attempting to do is lets say for example my tank barrel is pointing to the right hand side, if i click on the left hand side my tank barrel will rotate immediately to the left side. I want my tank barrel to transition itself from the right side to the left side so we can see the motion/movement occur rather than it just appearing from right to left.

So again I want to see the tank barrel actually rotate/transition from right to left rather than it just disappearing from the right side and reappearing on the left side (im trying to mimic a real rotation of tank barrel).

The code to tap the screen and the tank barrel aims in that direction is below (there is also a code implemented in it where if i click and hold, then i can drag my tank barrel to the desired location which im trying to get rid of and i only want it to tap and transition to now).

local function rotateObj(event)
    local t = event.target
    local phase = event.phase
if event.y < 225 then        
    if (phase == "began") then
            display.getCurrentStage():setFocus( t )
            t.isFocus = true


    local diffX = tankbarrel.x - event.x
    local diffY = tankbarrel.y - event.y
  tankbarrel.rotation = math.atan2(diffY, diffX) / RADIANS_TO_DEGREES + 180          

            -- Store initial position of finger
            t.x1 = event.x
            t.y1 = event.y

    elseif t.isFocus then

            if (phase == "moved") then
                    t.x2 = event.x
                    t.y2 = event.y

                    angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
                    angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
                    print("angle1 = "..angle1)
                    rotationAmt = angle1 - angle2 

                    --rotate it 
                    tankbarrel.rotation = tankbarrel.rotation - rotationAmt
                    print ("tankbarrel.rotation = "..tankbarrel.rotation)

-- set limits to how far the barrel can rotate                    
if tankbarrel.rotation < 210 then 
                    tankbarrel.rotation = 209
                    end

                    if tankbarrel.rotation > 330 then
                        tankbarrel.rotation = 329
                        end

                    t.x1 = t.x2
                    t.y1 = t.y2

            elseif (phase == "ended") then

                    display.getCurrentStage():setFocus( nil )
                    t.isFocus = false
            end
    end
end
    -- Stop further propagation of touch event
    return true
end

This may help you...

local tankbarrel = ..........  -- create your tank  

local function immediateTransRotation(e)
    if(e.x<tankbarrel.x)then
       transition.to(tankbarrel,{time=100,rotation=0})    -- rotate, if clicked on left side of the tank
    else
       transition.to(tankbarrel,{time=100,rotation=180})  -- rotate, if clicked on right side of the tank
    end
end
Runtime:addEventListener("tap",immediateTransRotation)

Keep coding ...... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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