Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How To Make Character Movement and Animation with Corona SDK
Started by votrongtin882003 May 19 2019 03:48 AM

3 replies to this topic
animation player movement corona sdk
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

votrongtin882003

[GLOBAL: userInfoPane.html]
votrongtin882003
  • Observer

  • 2 posts
  • Corona SDK

Hello everyone, I just learned programming with corona SDK. Recently, I have a problem in animate object.

 

I have made the character movement and I want to add the animation for that. I have try a lot of way to solve the problem but I can't. The problem is: when I pressed the control button, my player stop at frame 1 of the animation. Although, my animation have 6 frames.

 

This is my code:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here
-- walkSheet
local walkSheetData = { width =30.4, height =43, numFrames =6, sheetContentWidth = 182, sheetContentHeight = 43 }
local walkSheet = graphics.newImageSheet( "walk.png", walkSheetData )

local idleSheetData = { width =30.4, height =43, numFrames =1, sheetContentWidth =30.4, sheetContentHeight =43 }
local idleSheet = graphics.newImageSheet( "idle.png", idleSheetData )

local jumpSheetData = { width =28, height =43, numFrames =3, sheetContentWidth=84, sheetContentHeight =43 }
local jumpSheet = graphics.newImageSheet( "jump.png", jumpSheetData)

local sequenceData = 
{
    { name = "walk", sheet = walkSheet, start = 1, count =6, time = 800, loopCount = 0},
    { name = "idle", sheet = idleSheet, start = 1, count =1, time = 800, loopCount = 0},
    { name = "jump", sheet = jumpSheet, start = 1, count =3, time = 800, loopCount = 0}
}

local player = display.newSprite(  walkSheet, sequenceData )
player.x = display.contentWidth/2 ; player.y = display.contentHeight/2

player:setSequence( "walk" )
player:play()

--keyboardEvent

local upPressed = false
local downPressed = false
local leftPressed = false
local rightPressed = false

local function key( event)
    if (event.phase == "down") then
        if (event.keyName == "w") then
            upPressed = true
        elseif (event.keyName == "s") then
            downPressed = true
        elseif (event.keyName == "a") then
            leftPressed = true
        elseif (event.keyName == "d") then
            rightPressed = true
        end
    elseif (event.phase == "up") then
        player:setSequence("idle")
        player:play()
        if (event.keyName == "w") then
            upPressed = false
        elseif (event.keyName == "s") then
            downPressed = false
        elseif (event.keyName == "a") then
            leftPressed = false
        elseif (event.keyName == "d") then
            rightPressed = false
        end
    end

end

Runtime:addEventListener( "key", key)

local function enterFrame(event )
    if (upPressed) then
        player.y = player.y - 5
        player:setSequence("walk")
        player:play()
    end
    if (downPressed) then
        player.y = player.y + 5
        player:setSequence("walk")
        player:play()
    end
    if (leftPressed) then
        player.x = player.x - 5
        player:setSequence("walk")
        player:play()
    end
    if (rightPressed) then
        player.x = player.x + 5
        player:setSequence("walk")
        player:play()
    end

end

Runtime:addEventListener( "enterFrame", enterFrame )

I think it have some thing wrong here, a logic error. When I pressed the button, it repeat ( player:Play()...player:Play()...player:Play()... ) so my animation always stop at frame 1. I don't know how to fix that problem. 

 

Is someone help me please! Or if you have some better animation method let you teach me.

 

Thank you a lot to help me and so sorry because my English is not good, I am Vietnamese I use GG Translate. This is my first step in game development and I dont want to be defeated. I want to integrated all animation into my player( like walk, jump, attack...) and call it to use when I want. Please sympathize with me because I was asking so stupid.

 

Thanks you so much. Hope there will be a way to help me :)) It will be more good if you show me the example.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,588 posts
  • Corona SDK

Tip: It will be easier for folks to help if you also zip up the entire project and share it here as a link.  Use dropbox or github to host the file, and not some other less trustworthy site.


  • votrongtin882003 likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,588 posts
  • Corona SDK

Be aware, I have a ton of examples here https://github.com/roaminggamer/RG_FreeStuff

 

See the 'AskEd' folder, which contains over 400 code examples I have made over time answer various questions here in the forums.

 

Here is a particular example you may find interesting:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/12/walk_jump.zip

 


  • votrongtin882003 likes this

[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,820 posts
  • Corona SDK

I think you'll find the issue is you're setting the animation sequence and calling :play() every frame.

Therefore it never has a chance to go beyond frame 1. You only need to set the sequence when it needs to change from one to another.
  • nick_sherman, roaminggamer and votrongtin882003 like this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: animation, player movement, corona sdk