rajesh.k
rajesh.k

Reputation: 125

How to Move Objects in Fixed Distance in Corona SDK

I want to move the Object in x axis for Fixed Distance . say I have object sprite, I have Placed in Scene . my Requirement is i want to move the Object for X to Some -X and -X to X.

function scrollBackgroundImages(self, event)
    if self.x < -477 then
        self.x = 480
        else
        self.x = self.x - self.speed
    end
end

backgroundImage1 = display.newImage("goldfish-background-01.png", 768, 1024)
    backgroundImage1:setReferencePoint(display.BottomLeftReferencePoint)
    backgroundImage1.x = 0
    backgroundImage1.y = 320
    backgroundImage1.speed = 1
    screenGroup:insert(backgroundImage1)



carbSpritesheetData = { width=216, height=167, numFrames=3, sheetContentWidth=650, sheetContentHeight=167 }
        mycrabSheet = graphics.newImageSheet( "crab-sprite.png", carbSpritesheetData )
        crabSequenceData = {
        { 
            name = "normalRun", start=1, count=3, time=800}
        }
        crabMoving = display.newSprite( mycrabSheet, crabSequenceData )
        crabMoving:play()
        crabMoving:scale(0.3, 0.3)
        crabMoving.x =_W/2
        crabMoving.y = _H-55
        crabMoving.speed = 1
        physics.addBody(crabMoving, "static", {density = 0.1, bounce = 0.1, friction = 0.2, radius = 12})
        screenGroup:insert(crabMoving)


    crabMoving.enterFrame = scrollBackgroundImages
        Runtime:addEventListener("enterFrame",crabMoving )

Upvotes: 0

Views: 1840

Answers (2)

Rob Miracle
Rob Miracle

Reputation: 3063

There are three ways to move something:

  1. Physics using the various ways to add force or impulse.
  2. Using the transition.to() API call. http://docs.coronalabs.com/api/library/transition/to.html
  3. Move it using a Runtime:addEventListener("enterFrame", moveMyObject) function where you provide "moveMyObject" and it incrementally moves your object a little bit.

Upvotes: 1

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

    -- create background
    local backgroundImage1 = display.newImage("goldfish-background-01.png", 768, 1024)
    backgroundImage1:setReferencePoint(display.BottomLeftReferencePoint)
    backgroundImage1.x = 0
    backgroundImage1.y = 320
    screenGroup:insert(backgroundImage1)

    -- create sprite
    carbSpritesheetData = { width=216, height=167, numFrames=3, sheetContentWidth=650, sheetContentHeight=167 }
    mycrabSheet = graphics.newImageSheet( "crab-sprite.png", carbSpritesheetData )
    crabSequenceData = {
                        {name = "normalRun", start=1, count=3, time=800}
                       }
    crabMoving = display.newSprite( mycrabSheet, crabSequenceData )
    crabMoving:play()
    crabMoving:scale(0.3, 0.3)
    crabMoving.x =_W/2
    crabMoving.y = _H-55
    screenGroup:insert(crabMoving)


    local speed = 1 -- variable controls the speed of background/sprite movement

    -- create a function to move background and sprite
    local function scrollBackgroundImages()
       crabMoving.x = crabMoving.x-speed
       backgroundImage1.x = backgroundImage1.x - speed
       if(backgroundImage1.x< -477)then 
           backgroundImage1.x = 480
           crabMoving.x = 480+_W/2
       end
    end
    Runtime:addEventListener("enterFrame",scrollBackgroundImages )  

Upvotes: 1

Related Questions