2013-03-26 3 views
0

화면을 클릭하여 클릭 한 위치로 탱크 배럴을 회전하려고합니다. 나는 지금 당장이 코드를 사용했다. 내가하려고하는 다음 단계는 예를 들어 탱크 배럴이 오른쪽을 가리키고 있다고 말할 수 있습니다. 왼쪽을 클릭하면 탱크 배럴이 즉시 왼쪽으로 회전합니다. 나는 탱크 배럴을 오른쪽에서 왼쪽으로 옮기고 싶기 때문에 모션/움직임이 오른쪽에서 왼쪽으로 나타나는 것보다 발생하는 것을 볼 수 있습니다.이미지가 회전하여 코로나 SDK에서 전환 표시

다시 말해 나는 탱크 배럴이 오른쪽에서 사라지고 왼쪽에서 다시 사라지는 것보다는 (실제로 탱크 배럴의 실제 회전을 모방하려고 시도하는 것) 오른쪽에서 왼쪽으로 천이하는 것을보고 싶습니다.

화면을 누르고 탱크 배럴을 그 방향으로 향하게하는 코드는 아래에 있습니다 (여기에 코드가 구현되어있어 클릭하면 그 탱크 배럴을 원하는 위치로 드래그 할 수 있습니다). 제거하고 지금은 탭하고 전환하기 만하면됩니다.)

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 

답변

1

이 당신을 도울 수 ...

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) 

...... 코딩 유지 :

관련 문제