function left:touch(e)
if(e.phase == "ended") then
boy:applyLinearImpulse(-0.1, .5, boy.x, boy.y)
end
end
function right:touch(e)
if(e.phase == "ended") then
print("right");
boy:applyLinearImpulse(0.1, .5, boy.x, boy.y)
end
end
left:addEventListener( "touch", left );
right:addEventListener( "touch" , right );
在我的游戏中,我使用applyLinearImpulse t0给倒立男人施力。当我单击左右按钮来改变x和y方向时。如何为每次触摸增加不同的力量?
答案 0 :(得分:1)
这是你能做的
将开始移动事件中的初始x,y保存为ix
和iy
对于每一个移动事件,
计算ix
和event.x
之间的差异并应用差异dx
对y轴执行相同操作
如果触摸事件结束,则为初始x,y ix
和iy
。
local function left:touch(event)
if event.phase == "began" then
--save the initial position of boy
boy.ix,boy.iy = event.x,event.y
elseif event.phase == "moved" then
if boy.ix and boy.iy then
--calculate the initial x,y with current event x,y difference
local dx = (event.x-boy.ix)*0.4
local dy = (event.y-boy.iy)*0.4
boy:applyLinearImpulse(dx,dy,boy.x,boy.y)
--boy:applyForce(dx,dy,boy.x,boy.y)
boy.ix,boy.iy = boy.x,boy.y
end
elseif event.phase == "ended" or event.phase == "cancelled" then
boy.ix,boy.iy = nil,nil
end
end