我一直在努力幾個小時才能讓我的角色左右移動。遊戲的基本目標是讓角色跳起箱子。我有精靈的代碼,讓他走動所有的屏幕,但這樣他只移動在十在電暈/ Lua基本的側面運動
local touchX, touchY
local touchScreen = function(e)
--print(e.phase, e.x, e.y)
if e.phase == "began" then
touchX = e.x
touchY = e.y
elseif e.phase == "moved" then
--spriteAsh.x = spriteAsh.x + (e.x - touchX)
--spriteAsh.y = spriteAsh.y + (e.y - touchY)
local difX = e.x - touchX
local difY = e.y - touchY
spriteAsh:applyForce(difX *50, difY * 50, spriteAsh.x, spriteAsh.y)
touchX = e.x
touchY = e.y
elseif e.phase == "ended" then
end
end
Runtime:addEventListener("touch", touchScreen)
local updateGame = function(e)
local seq
local velX, velY = spriteAsh:getLinearVelocity()
print (velX, velY)
if math.abs (velX) >= math.abs (velY) then
--horizonal
if velX > 0 then
seq ="right_run"
else
seq = "left_run"
end
else
if velY > 0 then
seq = "down_run"
else
seq = "up_run"
end
end
if spriteAsh.sequence ~= seq then
spriteAsh:setSequence(seq)
spriteAsh:play()
end
end
Runtime:addEventListener("enterFrame", updateGame)
請考慮在'updateGame'中更加一致地縮進代碼。 –