как создать прокрутки управления в короне - PullRequest
0 голосов
/ 29 мая 2018

Я пытаюсь создать игру с 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)

1 Ответ

0 голосов
/ 03 июня 2018

Вы уже нашли решение для этого?Я уже отвечал на аналогичный вопрос раньше, я буду использовать тот же пример:

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)
...