I want to be able to place my finger on the screen (upper y-axis) and depending where my finger touches the screen I want a tank barrel (located at lower y-axis) to aim/rotate in the direction of my touch on the screen. I tried to swap out the “tankbarrel” with “Runtime” from tankbarrel:addEventListener(“touch”) but that did not work.
Wondering if anyone has any tips?
local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == “began”) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– 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
t.rotation = t.rotation - rotationAmt
print ("t.rotation = "…t.rotation)
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == “ended”) then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
– Stop further propagation of touch event
return true
end
tankbarrel:addEventListener(“touch”, rotateObj) [import]uid: 223314 topic_id: 36297 reply_id: 336297[/import]