0

这是一个问题:我正在使用 Corona SDK 和非常简单的模块制作一个简单的物理游戏:http: //developer.coronalabs.com/code/move-camera所以我在每一帧中都有带有物理体的鱼对象( enterFrame 事件)我将相机居中在它上面

camera.x = -fish.x + screenWidth / 2

而且我有两个背景 bg1 和 bg2,它们一个接一个地平铺(bg1 bg2 bg1 bg2 等)将在用户不知道只有 2 个背景的情况下产生很好的移动世界效果。问题是我无法正确计算他们的位置来产生这种效果。我用谷歌搜索了很多,但找不到任何适合我需要的东西。

这是一个免费的艺术,我正在使用http://www.vickiwenderlich.com/2013/02/free-game-art-flying-goldfish/你可以找到这两个背景。

鱼也有可能稍微朝相反的方向移动(通常它向右移动,因此要使世界移动效果背景必须向左移动),所以如果它在计算中会很好。

我想出了这样的事情(但是它不起作用,但也许我很接近):

local function updateFrame(event)

    local m
    local dfx = lfx - fish.x

    lfx = fish.x
    camera.x = -fish.x + screenWidth / 2

    --print("dfx = " .. dfx)

    bg1.x = bg1.x + dfx
    bg2.x = bg2.x + dfx

    s = s + dfx

    if mAbs(s) > screenWidth then
        m = sgn(s)

        if m ~= 0 then
            s = s - m * screenWidth
            if m < 0 then
                bg1.x = bg1.x - 2 * m * screenWidth
            else
                bg2.x = bg2.x - 2 * m * screenWidth
            end
        end
    end

end
Runtime:addEventListener( "enterFrame", updateFrame );

s = screenLeft - screenWidth

其中 s 是移动的像素的总和,如果它超过 screenWidth 我用其中一个背景跳跃,lfx 是最后一条鱼 x 位置,dfx 是最后一条和当前鱼位置之间的差异,仅此而已。

如果您可以帮助我进行计算或给我新的计算或只是提供一些有趣的链接,请在此处写下它们。

任何帮助将不胜感激,问候

4

3 回答 3

1
local road = display.newImageRect( "Images/roadBg1.png",2247/4,559/4 )
road:setReferencePoint( display.CenterLeftReferencePoint )
road.x = 0
road.y = baseline - 20
local road2 = display.newImageRect( "Images/roadBg1.png",2247/4,559/4  )
road2:setReferencePoint( display.CenterLeftReferencePoint )
road2.x = 2247/4
road2.y = baseline - 20

local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local xOffset = ( 0.2 * tDelta )

road.x = road.x - xOffset
road2.x = road2.x - xOffset
if (road.x + road.contentWidth) < 0 then
    road:translate( 2247/4 * 2, 0)

end
if (road2.x + road2.contentWidth) < 0 then
    road2:translate( 2247/4 * 2, 0)

end
Runtime:addEventListener( "enterFrame", move )

或查看丛林场景游戏示例代码。

位置:SampleApps/Sprites/JungleScene

于 2013-10-22T07:34:29.710 回答
0

Thanks @krs and @Malar, combining your answers I figured out how to do this:

local function updateFrame(event)

    local speed = fish.x - lfx
    lfx = fish.x

    camera.x = -fish.x + screenWidth / 2

    bg1.x = bg1.x - speed
    bg2.x = bg2.x - speed

    if bg1.x + bg1.width < 0 then
        bg1.x = bg1.x + screenWidth * 2
    end
    if bg2.x + bg2.width < 0 then 
        bg2.x = bg2.x + screenWidth * 2
    end

    if bg1.x - bg1.width > 0 then
        bg1.x = bg1.x - screenWidth * 2
    end
    if bg2.x - bg2.width > 0 then 
        bg2.x = bg2.x - screenWidth * 2
    end

end
Runtime:addEventListener( "enterFrame", updateFrame );

Starting positions:

bg1.x = screenLeft
bg2.x = bg1.x + bg1.width

And the last most important change: I put bg1, bg2 and camera into a group and other objects into camera group:

local camera = require("camera")
group = display.newGroup()
group:insert(bg1)
group:insert(bg2)
camera:insert(fish)
camera:insert(ground)
group:insert(camera)

So we have ground and fish, which can move forever, but the backgrounds are only moving from -screenWidth to screenWidth.

All these things combined works like a charm!

于 2013-10-22T17:40:48.597 回答
0

尝试这个:

local bg1 = display.newImageRect( "bg_1.png" ,480 ,320)
bg1.x = display.contentWidth/2; bg1.y = display.contentHeight/2

local bg2 = display.newImageRect( "bg_2.png" ,480 ,320 )
bg2.x = bg1.x + bg1.width; bg2.y = display.contentHeight/2


local speed = 30
function move()
  bg1.x = bg1.x-speed;
  bg2.x = bg2.x-speed;
  if(bg1.x + bg1.width/2 < 0)then bg1.x = bg1.width*3/2-speed
  elseif(bg2.x + bg2.width/2 < 0)then bg2.x = bg2.width*3/2-speed end
end
Runtime:addEventListener( "enterFrame", move )

继续编码............ :)

于 2013-10-22T10:21:44.580 回答