繁体   English   中英

如何在Corona SDK中移动固定距离的对象

[英]How to Move Objects in Fixed Distance in Corona SDK

我想在x轴上移动Object以获得固定距离。 说我有对象精灵,我已放置在场景中。 我的要求是我想将X的对象移动到某些-X和-X移动到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 )
    -- 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 )  

移动东西有三种方法:

  1. 物理学使用各种方式来增加力量或冲动。
  2. 使用transition.to()API调用。 http://docs.coronalabs.com/api/library/transition/to.html
  3. 使用Runtime:addEventListener(“enterFrame”,moveMyObject)函数移动它,在其中提供“moveMyObject”,它会逐渐移动您的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM