movement issue

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

end

[/lua]

here is the movement function

[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[/lua]

Hi Furqan,

what exactly is your problem? Is it that the character doesn’t move, or is it his animation that does not work?

If you want him to move, then you have to check for his movement in an enterFrame Loop. With your code above you call the movement function just once.

Try something like this:

function spawnCharacter() 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 } } local enemy = display.newSprite( mySheet1, sequenceData1 ) enemy.name = "bad" enemy.moveX = math.random( 2, 5 ) enemy.moveY = math.random( 2, 5 ) local function checkAnimation(obj) local moveX = obj.moveX local moveY = obj.moveY if math.abs(moveX) \> math.abs(moveY) then if moveX \> 0 then obj:setSequence("right") else obj:setSequence("left") end else if moveY \> 0 then obj:setSequence("front") else obj:setSequence("back") end end obj:play() end checkAnimation(enemy) function enemy:enterFrame() if self.x \< 0 or self.x \> display.contentWidth then self.moveX = -self.moveX checkAnimation(self) end if self.y \< 0 or self.y \> display.contentHeight then self.moveY = -self.moveY checkAnimation(self) end self.x = self.x + self.moveX self.y = self.y + self.moveY end Runtime:addEventListener("enterFrame", enemy) end spawnCharacter()

Don’t know it’s understandable, but yeah, I think that should do it for you.

thank you very much torbenratzlaff :)

problem is solved, now movement is working properly but another problem raises that is related to tap event (when I want to kill that moving object my code causes a runtime error) here is code for killing object

[lua]function kill(event)

    local obj = event.target

    display.remove( obj )

    --transition.cancel ( event.target.trans )

    if (obj.name == “bad”) then

        scoreAnim = display.newText(’+10’, obj.x, obj.y-10, native.systemFontBold, 16)

        transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete =  function() display.remove(scoreAnim) scoreAnim = nil end})

        scoreAnim:setTextColor(255,255,255)

        score = score + 10

        scoreTxt.text = "Score: " … score

    end

    spawnCharacter()

    return true

end[/lua]

and I am calling kill function like this [lua]enemy:addEventListener ( “tap”, kill )[/lua]

I would say that’s because of the Runtime Listener still working.

You have to remove it before you delete the enemy:

function kill(event) local obj = event.target Runtime:removeEventListener("enterFrame", obj) ...the rest of the function... end

Hope that helps.

– double post –

thanks again torbenratzlaff

removeEventListener function works good now game is working according to my needed and if I call kill and spwan function for multiple characters than I have to put all of my characters in a group or table can fullfill my needeed ???

I think that would be the easiest way. Just insert the objects in a table or an group and iterate through that group with a for loop. But remember to loop backwarts when you delete the objects. ( for i = #table, 1, -1 do ) If you use a table you have to set the table entry to nil after delting, to free the objects memory.

Hi Furqan,

what exactly is your problem? Is it that the character doesn’t move, or is it his animation that does not work?

If you want him to move, then you have to check for his movement in an enterFrame Loop. With your code above you call the movement function just once.

Try something like this:

function spawnCharacter() 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 } } local enemy = display.newSprite( mySheet1, sequenceData1 ) enemy.name = "bad" enemy.moveX = math.random( 2, 5 ) enemy.moveY = math.random( 2, 5 ) local function checkAnimation(obj) local moveX = obj.moveX local moveY = obj.moveY if math.abs(moveX) \> math.abs(moveY) then if moveX \> 0 then obj:setSequence("right") else obj:setSequence("left") end else if moveY \> 0 then obj:setSequence("front") else obj:setSequence("back") end end obj:play() end checkAnimation(enemy) function enemy:enterFrame() if self.x \< 0 or self.x \> display.contentWidth then self.moveX = -self.moveX checkAnimation(self) end if self.y \< 0 or self.y \> display.contentHeight then self.moveY = -self.moveY checkAnimation(self) end self.x = self.x + self.moveX self.y = self.y + self.moveY end Runtime:addEventListener("enterFrame", enemy) end spawnCharacter()

Don’t know it’s understandable, but yeah, I think that should do it for you.

thank you very much torbenratzlaff :)

problem is solved, now movement is working properly but another problem raises that is related to tap event (when I want to kill that moving object my code causes a runtime error) here is code for killing object

[lua]function kill(event)

    local obj = event.target

    display.remove( obj )

    --transition.cancel ( event.target.trans )

    if (obj.name == “bad”) then

        scoreAnim = display.newText(’+10’, obj.x, obj.y-10, native.systemFontBold, 16)

        transition.to(scoreAnim, {time = 1000, y = scoreAnim.y - 30, alpha = 0, onComplete =  function() display.remove(scoreAnim) scoreAnim = nil end})

        scoreAnim:setTextColor(255,255,255)

        score = score + 10

        scoreTxt.text = "Score: " … score

    end

    spawnCharacter()

    return true

end[/lua]

and I am calling kill function like this [lua]enemy:addEventListener ( “tap”, kill )[/lua]

I would say that’s because of the Runtime Listener still working.

You have to remove it before you delete the enemy:

function kill(event) local obj = event.target Runtime:removeEventListener("enterFrame", obj) ...the rest of the function... end

Hope that helps.

– double post –

thanks again torbenratzlaff

removeEventListener function works good now game is working according to my needed and if I call kill and spwan function for multiple characters than I have to put all of my characters in a group or table can fullfill my needeed ???

I think that would be the easiest way. Just insert the objects in a table or an group and iterate through that group with a for loop. But remember to loop backwarts when you delete the objects. ( for i = #table, 1, -1 do ) If you use a table you have to set the table entry to nil after delting, to free the objects memory.