【问题标题】:Image displays in top left corner in Corona Simulator图像显示在 Corona Simulator 的左上角
【发布时间】:2013-12-23 07:21:09
【问题描述】:

我刚开始玩 lua / Corona,一直在使用《用 Corona 创建移动游戏》一书。 (Silvia Domenech) 书中的第一个项目是制作一个简单的星球防御游戏。为此,我使用了一个多屏应用程序(或在 OS X 中称为“场景”)。第一步是在 Corona 中使用组显示图像。

本书让我在主场景组中输入如下代码:

local image = display.newImage( "images/iphone_767.png")
group:insert( image )

将它添加到(我认为是)正确的组后,代码如下所示:

-- Called when the scene's view does not exist:
function scene:createScene( event )
    local group = self.view

    -----------------------------------------------------------------------------
local image = display.newImage( "images/iphone_767.png")
group:insert( image )       
    --  CREATE display objects and add them to 'group' here.
    --  Example use-case: Restore 'group' from previously saved state.

    -----------------------------------------------------------------------------

end

对于图像,我首先尝试了一个 320 x 480 的图像。当它在模拟器上渲染时,它位于模拟器的左上角。这是它如何呈现的屏幕截图:http://imgur.com/Kpm0XTd

配置文件设置为 320 x 480 像素。我真的不知道是什么原因造成的,因为我没有修改我所描述的内容之外的任何内容。有什么想法吗?

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    将令牌的 x 和 y 值设置为屏幕中心(假设您想将其居中):

    image.x = display.contentWidth/2
    image.y = display.contentHeight/2
    

    谢谢 阿南德

    【讨论】:

    • 另一个你应该看的很酷的东西是图像:setReferencePoint(referencePoint),其中 referencePoint 可以是例如 display.TopLeftReferencePoint。这意味着您为图像选择的参考点将定位在您为其指定的 x 和 y 值上。如果未指定参考点(在本例中未指定),Corona 将使用 display.CenterReferencePoint 作为图像的默认参考点。
    • 这是因为,如果您没有明确设置 x 和 y 值,Corona 会将它们默认为零。由于您的图像有一个中心参考点(默认情况下),它会将图像的中心放置在左上角的位置 (0,0)。只有 1/4 的图像可见。
    【解决方案2】:

    我个人喜欢将变量设置为 display.contentWidth 和 display.contentWidth。

    _W = display.contentWidth
    _H = display.contentHeight
    image.x = _W/2
    image.y = _H/2
    

    你也可以这样格式化:

    image.x = _W*.5
    image.y = _W*.5
    

    【讨论】:

      【解决方案3】:

      Corona SDK 最近开始使用一种新的图形引擎,该引擎会影响物体的定位方式。 display.newImage() 用于在 0, 0 处绘制左上角,但现在它在 0, 0 处绘制中心。最好的办法是始终明确地将图像的 .x 和 .y 设置在所需的位置.

      【讨论】:

        【解决方案4】:

        似乎现在存在简化的方法:

        image.x = display.contentCenterX
        image.y = display.contentCenterY
        

        或单行:

        local image = display.newImage('images/iphone_767.png', display.contentCenterX, display.contentCenterY)
        

        但现在似乎更传统的模式是先创建 centeredGroup:

        -- Create centered group
        local centeredGroup = display.newGroup()
        centeredGroup.x = display.contentCenterX
        centeredGroup.y = display.contentCenterY
        
        -- Add background
        local image = display.newImage(centeredGroup, 'images/iphone_767.png', display.contentCenterX, display.contentCenterY)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-06
          • 1970-01-01
          • 1970-01-01
          • 2015-09-02
          • 1970-01-01
          • 2018-03-13
          • 2015-02-12
          • 1970-01-01
          相关资源
          最近更新 更多