我正在创建类似于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