movement issue of sprite

Hi all,

 

I am making my first game in lua (corona),… and want your help related to movement of my character in game, the issue is that the character is at option 0, 0 and does not move (character is a sprite sheet), here is my code and sprite sheet,… please help me out to solve this problem I tried my best but could not get it right,… please

 

[lua]local function movebad(obj)

    – let’s make him bounce around the screen

    if obj.x < 0 or obj.x > display.contentWidth then 

        obj.moveX = -obj.moveX 

    end

    if obj.y < 0 or obj.y > display.contentHeight then 

        obj.moveY = -obj.moveY 

    end

    obj.x = obj.x + obj.moveX

    obj.y = obj.y + obj.moveY

    return obj

end

function spawnCharacter()

    local enemy

    local good

        local sheetData1 = { width=32, height=32, numFrames=12, sheetContentWidth=96, sheetContentHeight=128 }

        local mySheet1 = graphics.newImageSheet( “bad5.png”, sheetData1 )

        local sequenceData1 = {

                                { name = “front”, frames={ 1,2,3 }, time=1000 },

                                { name = “left”, frames={ 4,5,6 }, time=1000 },

                                { name = “right”, frames={ 7,8,9 }, time=1000 },

                                { name = “back”, frames={ 10,11,12 }, time=1000 }

                            }

        enemy = display.newSprite( mySheet1, sequenceData1 )

        enemy.name = “bad”

        enemy:setSequence( “right” )

        enemy:play()

        enemy.moveX = math.random( 2, 5 )

        enemy.moveY = math.random( 2, 5 )

        timer.performWithDelay( 150, movebad(enemy), 0 )

        

        --local sheetData2 = { width=31, height=32, numFrames=12, sheetContentWidth=96, sheetContentHeight=128 }

        --local mySheet2 = graphics.newImageSheet( “good4.png”, sheetData2 )

        --local sequenceData2 = {

            –                    { name = “front”, frames={ 1,2,3 }, time=1000 },

                –                { name = “left”, frames={ 4,5,6 }, time=1000 },

                    –            { name = “right”, frames={ 7,8,9 }, time=1000 },

                        –        { name = “back”, frames={ 10,11,12 }, time=1000 }

                            --}

        --good = display.newSprite( mySheet2, sequenceData2 )

        --good.name = “good”

        --good:setSequence( “right” )

        --good:play()

        --good.moveX = math.random( 2, 5 )

        --good.moveY = math.random( 2, 5 )

        --timer.performWithDelay( 150, movegood, 0 )

end

[/lua]

On your place I would use enterFrame event. performWithDelay in your case worked only once - 150 ms after starting implementation. I improved your code and it works fine.

local function movebad(obj) -- let's make him bounce around the screen if obj.x \< 0 or obj.x \> display.contentWidth then obj.moveX = -obj.moveX end if obj.y \< 0 or obj.y \> display.contentHeight then obj.moveY = -obj.moveY end obj.x = obj.x + obj.moveX obj.y = obj.y + obj.moveY return obj end function movement (self, event) movebad(self) end function spawnCharacter() local enemy local good local sheetData1 = { width=32, height=32, numFrames=12, sheetContentWidth=96, sheetContentHeight=128 } local mySheet1 = graphics.newImageSheet( "bad5.png", sheetData1 ) local sequenceData1 = { { name = "front", frames={ 1,2,3 }, time=1000 }, { name = "left", frames={ 4,5,6 }, time=1000 }, { name = "right", frames={ 7,8,9 }, time=1000 }, { name = "back", frames={ 10,11,12 }, time=1000 } } enemy = display.newSprite( mySheet1, sequenceData1 ) enemy.name = "bad" enemy:setSequence( "right" ) enemy:play() enemy.moveX = math.random( 2, 5 ) enemy.moveY = math.random( 2, 5 ) enemy.enterFrame = movement Runtime:addEventListener ("enterFrame", enemy) end spawnCharacter()

I will also changed movebad function to

 if obj.x \< obj.width or obj.x \> display.contentWidth -obj.width then obj.moveX = -obj.moveX end if obj.y \< obj.height or obj.y \> display.contentHeight - obj.height then obj.moveY = -obj.moveY end

It will help with bounding

thanks anton2111

I have modified my code with  enterFrame event and it is working properly, and also I have change the movebad function a little bit so that character moves in all possible sequences on the screen I will update you about this shortly,… thank you very much again :slight_smile:

Actually, for the bets perfomance it is better to use next function - self:translate(dX, dY) instead of self.x = self.x + dX, self.y = self.y + dY. And it will save a one string in your code :wink:

On your place I would use enterFrame event. performWithDelay in your case worked only once - 150 ms after starting implementation. I improved your code and it works fine.

local function movebad(obj) -- let's make him bounce around the screen if obj.x \< 0 or obj.x \> display.contentWidth then obj.moveX = -obj.moveX end if obj.y \< 0 or obj.y \> display.contentHeight then obj.moveY = -obj.moveY end obj.x = obj.x + obj.moveX obj.y = obj.y + obj.moveY return obj end function movement (self, event) movebad(self) end function spawnCharacter() local enemy local good local sheetData1 = { width=32, height=32, numFrames=12, sheetContentWidth=96, sheetContentHeight=128 } local mySheet1 = graphics.newImageSheet( "bad5.png", sheetData1 ) local sequenceData1 = { { name = "front", frames={ 1,2,3 }, time=1000 }, { name = "left", frames={ 4,5,6 }, time=1000 }, { name = "right", frames={ 7,8,9 }, time=1000 }, { name = "back", frames={ 10,11,12 }, time=1000 } } enemy = display.newSprite( mySheet1, sequenceData1 ) enemy.name = "bad" enemy:setSequence( "right" ) enemy:play() enemy.moveX = math.random( 2, 5 ) enemy.moveY = math.random( 2, 5 ) enemy.enterFrame = movement Runtime:addEventListener ("enterFrame", enemy) end spawnCharacter()

I will also changed movebad function to

 if obj.x \< obj.width or obj.x \> display.contentWidth -obj.width then obj.moveX = -obj.moveX end if obj.y \< obj.height or obj.y \> display.contentHeight - obj.height then obj.moveY = -obj.moveY end

It will help with bounding

thanks anton2111

I have modified my code with  enterFrame event and it is working properly, and also I have change the movebad function a little bit so that character moves in all possible sequences on the screen I will update you about this shortly,… thank you very much again :slight_smile:

Actually, for the bets perfomance it is better to use next function - self:translate(dX, dY) instead of self.x = self.x + dX, self.y = self.y + dY. And it will save a one string in your code :wink: