如何在科罗纳中创建滑动控件



我正在尝试使用Corona创建一个游戏,但我不知道如何进行滑动控制。我想制作一个可以沿着x,-x,y,-y移动的角色,具体取决于您滑动的方向。需要任何建议,因为我迷路了。此代码当前使用按钮在按下按钮时移动播放器,播放器向该方向移动。我想将按钮按下更改为滑动。移动事件位于底部,按钮按下事件位于中间。

local backdrop = display.setDefault("background", 1, 1, 1)
local physics = require("physics")
physics.start()
local square = display.newRect( 0, 0, 5, 5 )
--print(square)
_W = display.contentWidth; -- Get the width of the screen
_H = display.contentHeight; -- Get the height of the screen
motionDown = 0;
motionUp = 0;
motionRight = 0;
motionLeft = 0;
motionSquare = 5
speed = 4
local left = display.newRect(0,0,_W/2,_H/1.19)
left.x = _W/4.5; left.y = _H/2;
local right = display.newRect(0,0,_W/2,_H/1.19)
right.x = _W/1.25; right.y = _H/2;
local top = display.newRect(0,0,_W,_H/5.5)
top.x = _W/2; top.y =0;
local bottem = display.newRect(0,0,_W,_H/5.5)
bottem.x = _W/2; bottem.y =_H;
player = display.newImage("player.png", display.contentCenterX, display.contentCenterY)
player.x = math.random(10,_W-10)
player.y = math.random(10,_H-10)
player:toFront(player)
physics.addBody( player, "static" )

function left:tap()   --Left
motionDown = 0;
motionUp = 0;
motionRight = 0;
motionLeft = -speed;
local square = display.newRect( 0, 0, 5, 5 )
square.strokeWidth = 3
square:setFillColor( 0 )
square:setStrokeColor( 0, 0, 0 )
square.x = player.x + 10 ; square.y = player.y;
local function moveSquare (event)
square.x = square.x + motionSquare;
end
Runtime:addEventListener("enterFrame", moveSquare)
return square
end
left:addEventListener("tap",left) 
square = left:tap()
function right:tap()      --Right
motionDown = 0;
motionUp = 0;
motionLeft = 0;
motionRight = speed;
local square = display.newRect( 0, 0, 5, 5 )
square.strokeWidth = 3
square:setFillColor( 0 )
square:setStrokeColor( 0, 0, 0 )
square.x = player.x - 10 ; square.y = player.y;
local function moveSquare (event)
square.x = square.x - motionSquare;
end
Runtime:addEventListener("enterFrame", moveSquare)
return square
end
right:addEventListener("tap",right) 
square = right:tap()
function top:tap()          --Top
motionDown = 0;
motionRight = 0;
motionLeft = 0;
motionUp = -speed;
local left = display.newRect(0,0,5,5)
local square = display.newRect( 0, 0, 5, 5 )
square.strokeWidth = 3
square:setFillColor( 0 )
square:setStrokeColor( 0, 0, 0 )
square.x = player.x ; square.y = player.y + 10;
local function moveSquare (event)
square.y = square.y + motionSquare;
end
Runtime:addEventListener("enterFrame", moveSquare)
return square
end
top:addEventListener("tap",top) 
square = top:tap()
function bottem:tap(event) --Bottem
motionRight = 0;
motionUp = 0;
motionLeft = 0;
motionDown = speed;
local square = display.newRect( 0, 0, 5, 5 )
square.strokeWidth = 3
square:setFillColor( 0 )
square:setStrokeColor( 0, 0, 0 )
square.x = player.x ; square.y = player.y - 10;
local function moveSquare (event)
square.y = square.y - motionSquare;
end
Runtime:addEventListener("enterFrame", moveSquare)
return square
end
bottem:addEventListener("tap",bottem) 
square = bottem:tap()
-- Move character
local function movePlayer (event)
player.x = player.x + motionRight;
if player.x > display.contentWidth then  player.x = 0 
end
end
Runtime:addEventListener("enterFrame", movePlayer)
local function movePlayer (event)
player.x = player.x + motionLeft;
if player.x < 0 then player.x = display.contentWidth
end
end
Runtime:addEventListener("enterFrame", movePlayer)
local function movePlayer (event)
player.y = player.y + motionUp;
if player.y > display.contentHeight then player.y = 0
end
end
Runtime:addEventListener("enterFrame", movePlayer)
local function movePlayer (event)
player.y = player.y + motionDown;
if player.y < 0 then player.y = display.contentHeight
end
end
Runtime:addEventListener("enterFrame", movePlayer)
local function moveSquare (event)
square.y = square.y + motionSquare;
if player.y < 0 then player.y = display.contentHeight
end
end
Runtime:addEventListener("enterFrame", moveSquare)

您是否已经找到了解决方案?我之前已经回答过类似的问题,我将使用相同的示例:

local function globalTouchHandler(event)
local swipeLength = math.abs(event.x - event.xStart) 
print(event.phase, swipeLength)
local t = event.target
local phase = event.phase
if (phase == "began") then
elseif (phase == "moved") then
elseif (phase == "ended" or phase == "cancelled") then
if (event.xStart > event.x and swipeLength > 50) then 
-- left
elseif (event.xStart < event.x and swipeLength > 50) then 
-- right
end 
if (event.y < event.yStart and swipeLength > 50) then
-- up
end
end
end
Runtime:addEventListener("touch",globalTouchHandler)

最新更新