我正在創建類似於Mario Brothers的一些平臺遊戲類型的遊戲,並且遇到了一個大問題。我的問題是,當我點擊我的方向鍵並將其拖出時,儘管我已經釋放了點擊,但角色仍在移動。我已經在Corona論壇發佈了這個問題,但我還沒有找到解決方案。滑過方向鍵后角色繼續移動
這裏是我的代碼:
local function makeControls(event)
if anim.isJumping == false then
if event.phase == "began" then
if event.target.isPressed == false then
if event.target.id == "left" then
walk(-1)
elseif event.target.id == "right" then
walk(1)
end
end
event.target.isPressed = true
elseif (event.phase == "moved" and event.target.isPressed == true) then
if (
event.x > event.target.contentBounds.xMin+4 and
event.x < event.target.contentBounds.xMax-4 and
event.y > event.target.contentBounds.yMin+4 and
event.y < event.target.contentBounds.yMax-4
) then
event.target:dispatchEvent({ name="touch", phase="began", target=event.target })
else
event.target:dispatchEvent({name = "touch", phase = "ended", target = event.target })
end
elseif event.phase == "ended" then
anim:setLinearVelocity(0,0)
event.target.isPressed = false
end
end
return true
end
local function createArrowKey(group, x, y, rotation, name)
local newButtonBoundary = display.newRoundedRect(group, x, y, 75, 75, 12)
newButtonBoundary.strokeWidth = 6
newButtonBoundary:setStrokeColor(1, 1, 1, 0.5)
newButtonBoundary:setFillColor(1, 1, 1, 0.2)
newButtonBoundary.id = name
newButtonBoundary:addEventListener("touch", makeControls)
newButtonBoundary.isPressed = false
local newArrow = display.newPolygon(group, x, y, { 0, -24, 24, 24, -24, 24 })
newArrow.rotation = rotation
newArrow.strokeWidth = 3
end
createArrowKey(parent2, left + fullw/3, bottom - fullh/5.5, 90, "right")
createArrowKey(parent2, left + fullw/5.5, bottom - fullh/5.5, 270, "left")
createArrowKey(parent2, left + fullw/3.88, bottom - fullh/8.5, 180, "down")
createArrowKey(parent2, left + fullw/3.88, bottom - fullh/3.9, 0, "up")
createButton(parent2, left + fullw - 150, bottom - fullh/5.5, "Shoot", "shoot")
createButton(parent2, left + fullw - 250, bottom - fullh/5.5, "Throw", "throw")
createButton(parent2, left + fullw - 350, bottom - fullh/5.5, "Melee", "melee")
也許嘗試[由Ponywolf joykey和vjoy(https://github.com/ponywolf/joykey)。 – ldurniat
我已經嘗試過,但我永遠無法得到左右運動的工作。 – alexjr
左右運動爲什麼不起作用? – ldurniat