Tilt and animation togather sync together not able to handle.

Hi Corona world,

I want to build some game but I am stuck in some where.

My requirement is I have a bike. This bike is build by 3 different sprites.

I am able to show the sprite properly. Now i want to tilt the the object on tilt of the object should move left or right. Which I am able to do. But my huddle is I am not able to do the animation based upon the tilt direction. Means if I tilt in left the bike should show some sequence. If I turn the bike on right then it should show the alternative animation sequence.

My issue is I am not able to show different animation sequence of tilt.

Please help me. I am stuck with this issue for more then 3 days.

Please help me.

The code is given below:

I have defined the sequence out side the main:

        – Sequence data defines the different sequences
        local sequenceData = {
            –            { name = “normalRun”, start=1, count=6, time=800, loopCount=10 },  --add loopCount=4
            { name = “normalRun”, start=2, count=1, time=250, loopCount=1 },  --add loopCount=4

            { name = “midToleftRun”, frames={ 2,1 }, time=250 },

            { name = “midTorightRun”, frames={ 2,3 }, time=250 }
        }


This is below the sprite definition:

        local sheetData = { width=55, height=90, numFrames=3, sheetContentWidth=165, sheetContentHeight=90 }

        --Initializing the sprites
        local mySheet = graphics.newImageSheet( “assets/images/static/anim_auto.png”, sheetData )

        – Basic animation variable for view
        animation = display.newSprite( mySheet, sequenceData )
        –        animation.gravityScale = 0

        --Placing the animation on the view
        animation.x = display.contentWidth/2  --center the sprite horizontally
        animation.y = display.contentHeight/2  --center the sprite vertically

        --Start the animation
        animation:play()


My tilt function:


    local onTilt = function( event )
        local prevAnimX = animation.x;
        local newTiltY = animation.x + (animTiltSpeed * event.xGravity);

        if prevAnimX < newTiltY then
                            myToast = toast.new(“Right”, 1000)

            animation:setSequence( “midTorightRun” )

        elseif prevAnimX > newTiltY then
                            myToast = toast.new(“Left”, 1000)

            animation:setSequence( “midToleftRun” )

        else
        --BASE CASE EMPTY
        end

        animation.x = animation.x + (animTiltSpeed * event.xGravity)
    end

PLease help me…

A little late answer, but since I am having more or less the same setting (animation based on event), I can only point you to the solution I used:

I have a cartoon which has legs and eyes, but no mouth or eyeballs. When I need to change his expression, I only change his mouth and eyes. The base animation is the cartoon running. Instead of using sequences and “play()” to do the animation, I use timers:

 timer.performWithDelay(100, function() manmouth.x=manmouth.x+manmmx[mframe] mframe=mframe+1 if mframe\>12 then mframe=1 end mangfx:setFrame(mframe) manmouth.y=mangfx.y+2+manmmy[mframe] manmouth.x=manmouth.x-manmmx[mframe] end,24)

You get the idea: The sequence has been defined and the total walk/running animation is 12 frames. Instead of using “play” I use “object:setFrame(frame)” to show the correct frame. The mouth doesn’t change here, but since his body is moving around while running, I have a correction table for mouth x and y values so that his mouth stays in his face beneath his nose…

There are probably a million ways of doing this, but using timers to choose the frames on the basis on what is happing is the only way to get good sync between several objects.

A little late answer, but since I am having more or less the same setting (animation based on event), I can only point you to the solution I used:

I have a cartoon which has legs and eyes, but no mouth or eyeballs. When I need to change his expression, I only change his mouth and eyes. The base animation is the cartoon running. Instead of using sequences and “play()” to do the animation, I use timers:

 timer.performWithDelay(100, function() manmouth.x=manmouth.x+manmmx[mframe] mframe=mframe+1 if mframe\>12 then mframe=1 end mangfx:setFrame(mframe) manmouth.y=mangfx.y+2+manmmy[mframe] manmouth.x=manmouth.x-manmmx[mframe] end,24)

You get the idea: The sequence has been defined and the total walk/running animation is 12 frames. Instead of using “play” I use “object:setFrame(frame)” to show the correct frame. The mouth doesn’t change here, but since his body is moving around while running, I have a correction table for mouth x and y values so that his mouth stays in his face beneath his nose…

There are probably a million ways of doing this, but using timers to choose the frames on the basis on what is happing is the only way to get good sync between several objects.