user3178851
user3178851

Reputation: 59

Corona/LUA 'tap' to jump

I'm having problem with movement in corona. I've got it set up so my character can run left and right, now however I'm trying to make it so when you tap on him he jumps. I can't get the code to work.

code:

---------------------------------------------------------------------------------
-- CHARACTER PROPERTIES
---------------------------------------------------------------------------------

display.setDefault("magTextureFilter", "nearest")

local options =
{   --required parameters
    width = 32,
    height = 32,
    numFrames = 20,

    --optional parameters; used for dynamic resolution support
    sheetContentWidth = 160,  -- width of original 1x size of entire sheet
    sheetContentHeight = 128   -- height of original 1x size of entire sheet
}

local sheetAsh = graphics.newImageSheet( "boy.png", options )

local sequenceData =
{
    { name="down_stand", start=1, count=1},
    { name="down_run", frames={2, 3, 4, 5}, time=400, loopCount=0,  loopDirection = "bounce"},
    { name="left_stand", start=6, count=1},
    { name="left_run", frames={7, 8, 9, 10}, time=800, loopCount=0, loopDirection = "bounce"},
    { name="up_stand", start=11, count=1},
    { name="up_run", frames={12, 13, 14, 15}, time=1000, loopCount=0,  loopDirection = "bounce"},
    { name="right_stand", start=16, count=1},
    { name="right_run", frames={17, 18, 19, 20}, time=800, loopCount=0, loopDirection = "bounce"},
}

local spriteAsh = display.newSprite( sheetAsh, sequenceData )

physics.addBody(spriteAsh, "dynamic", {density =1.5 , bounce = 0.5, radius = 28})
spriteAsh.x = _W * 0.5
spriteAsh.y = _H * 0.5
spriteAsh.xScale = 2
spriteAsh.yScale = 2
spriteAsh.linearDamping = 2
--spriteAsh.gravityScale = 0.1

--spriteAsh:setSequence( "down_run" )
--spriteAsh:play()

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, 0, spriteAsh.x, spriteAsh.y)

        touchX = e.x
        touchY = e.y
    elseif e.phase == "ended" then

    end

end







local function jumptouch( event )
        if ( event.phase == "began" ) then
                spriteAsh:applyForce( 250, -1500, spriteAsh.x, spriteAsh.y )
        end
        return true
end

local function onScreenTouch( event )
        if event.phase == "began" then
                -- make character jump forward
                -- character:applyForce( 15000, -2500, character.x, character.y )
                --if event.xStart == character.x then
                        spriteAsh:applyForce( 250, -1500, spriteAsh.x, spriteAsh.y )
                --end
        end

        return true
end


spriteAsh:addEventListener( "tap", jumptouch, )

Upvotes: 0

Views: 277

Answers (1)

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

"tap" events don't have event.phase field. So if you remove event.phase == "began" check, I think it will work.

Upvotes: 3

Related Questions