在我的项目中,如果我拖动一个对象,则对一个对象施加一个力,另一个对象生成并沿手指滑动的方向移动,并且根据施加的力的大小,此代码可以正常工作。但我想根据手指滑动的角度旋转生成的第二个对象。我想知道如何获得手指滑动的角度并将第二个对象旋转到该角度,请提供任何建议。谢谢...手指滑动的代码如下
local Objectswipe = display.newImage( "object1.png")
Objectswipe.x = 100
Objectswipe.y = 200
target = display.newImage( "target.png" )
target.x = Objectswipe.x; target.y = Objectswipe.y; target.alpha = 0
local function fingerswipe( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( "enterFrame", startRotation )
local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if "moved" == phase then
if ( myLine ) then
myLine.parent:remove( myLine ) -- erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local stopRotation = function()
Runtime:removeEventListener( "enterFrame", startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
if ( myLine ) then
myLine.parent:remove( myLine )
end
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
end
end
return true
end
Objectswipe:addEventListener( "touch", fingerswipe )