【问题标题】:Corona SDK Display Group Issue - Where is the error in this code?Corona SDK 显示组问题 - 此代码中的错误在哪里?
【发布时间】:2012-05-04 08:05:18
【问题描述】:

有人能看出 Corona 的这个 lua 代码中的逻辑缺陷吗?

概念是您创建一个新的“扩展”显示对象(例如 MyGroup:new(...)),然后您可以使用百分比将对象插入到它在父对象中的位置(因此无需使用像素数)。

问题 - 问题是绘制的对象大致在正确的位置,但只是稍微偏离了一点?我有一种感觉,这可能是由于“self:insert(displayO)”行中父级中的坐标发生了变化?

main.lua

------------------------------------------------------------------------------
-- HELPERS 
------------------------------------------------------------------------------
function rpadString (str, len, char)
    if char == nil then char = ' ' end
    return  (str .. string.rep(char, len - #str))
end

function drawBorder(obj)
    local b = obj.contentBounds 

    local myRectangle = display.newRect(b.xMin, b.yMin, obj.contentWidth, obj.contentHeight)
    -- print ("Rect X,Y,Width,Height: ", b.xMin, b.yMin, obj.contentWidth, obj.contentHeight)
    myRectangle.strokeWidth = 1
    myRectangle:setFillColor(140, 140, 140, 1)
    if obj.gcGroupName then
        -- Group
        myRectangle:setStrokeColor(100, 250, 180)
        myRectangle.strokeWidth = 1
    else
        -- Image/Rect
        myRectangle:setStrokeColor(250, 180, 180)
        myRectangle.strokeWidth = 2
    end


end

function dumpDisplayObjects(doParent, level)
    drawBorder(doParent)
    if doParent.numChildren then
        for i=doParent.numChildren,1,-1 do
            local doChild = doParent[i]
            dumpDisplayObjects(doChild, level + 1)
        end
    end
end

------------------------------------------------------------------------------
-- MAIN 
------------------------------------------------------------------------------
require "MyGroup"

print "RUNNING ........................................."
display.setStatusBar( display.HiddenStatusBar )

-- Create Toolbar Area at Bottom
local mainGroup = MyGroup:new("Main Group", 0, 0, display.contentWidth, display.contentHeight)
local toolbarArea = MyGroup:new("Inventory", 0,0,0,0)
mainGroup:GcInsertLRTB(toolbarArea, 0, 100, 80, 100, "Inventory")    -- Percentages Used

-- Draw Rectangle 1
local toolbarAreaImage = display.newImage( "toolbarArea_button.png", 0, 0)
toolbarArea:GcInsertLRTB(toolbarAreaImage, 0, 20, 0, 100, "Inventory Button")

-- -- Setup Toolbar Group
-- local toolbarAreaToolbar = MyGroup:new("InventoryToolbar", 0, 0, 1, 1)
-- toolbarArea:GcInsertLRTB(toolbarAreaToolbar, 20, 100, 0, 100, "Toolbar")
--  -- 
-- -- Draw Rectangle 2
-- local rect2 = display.newRoundedRect(0, 0, 1000, 1000, 5)
-- rect2.strokeWidth = 1
-- rect2:setStrokeColor(140, 140, 140)
-- rect2:setFillColor(140, 140, 140, 100)
-- toolbarAreaToolbar:GcInsertLRTB(rect2, 0, 100, 0, 100, "Rect2")

-- Debug Display
dumpDisplayObjects(toolbarArea, 1)

MyGroup.lua

MyGroup = {}

function MyGroup:new(name, left, top, targetWidth, targetHeight)
    -- Inherit from Group
    myGroup = display.newGroup()

    -- Work Around (Dummy Display Object Insertion) (See Note 1)
    myGroup.dummyDO = display.newCircle( myGroup, 10, 10, 1 )
    myGroup.dummyDO:setFillColor(1,1,1,1)
    myGroup.dummyDO.debugComment = "Dummy Display Object"
    myGroup:insert(myGroup.dummyDO)

    myGroup.myGroupName = name      
    myGroup.debugComment = name
    myGroup.targetWidth = targetWidth
    myGroup.targetHeight = targetHeight
    myGroup.x = left
    myGroup.y = top


    function myGroup:GcInsertLRTB(displayO, leftP, rightP, topP, bottomP, debugComment)
        self:insert(displayO)   -- <=== DOES THIS UPSET THINGS FOR THE PARENT???

        -- Remove Work Around Dummy Display Object (See Note 1)
        if self.dummyDO then 
            self.dummyDO:removeSelf() 
            self.dummyDO = nil
        end

        displayO:setReferencePoint(display.TopLeftReferencePoint)
        displayO.x = leftP * 0.01 * self.targetWidth
        displayO.y =  (topP * 0.01) * self.targetHeight

        -- Scale Display Object (or set target's if adding another MyGroup)
        if not displayO.myGroupName then
            -- Not a MyGroup
            local xScale = (self.targetWidth * (rightP - leftP)/100) / displayO.width  
            local yScale = (self.targetHeight * (bottomP - topP)/100) / displayO.height
            displayO:scale(xScale, yScale)
        else
            -- Inserted object is a MyGroup
            displayO.targetWidth = self.targetWidth * (rightP - leftP)/100
            displayO.targetHeight = self.targetHeight * (bottomP - topP)/100 
            displayO:scale(1,1)  -- TODO: Probably don't need this
        end

    end

    return myGroup
end

-- Class Variables

-- Class Methods

return MyGroup

【问题讨论】:

  • 我刚刚使用此代码创建了一个新项目,替换了 toolbarArea_button.png 资产。我真的无法弄清楚问题是什么,你能创建一个更清晰,更精确的例子吗? (首先删除注释代码,不确定是什么导致了问题还是什么?)。
  • 我开始认为这个概念可能有缺陷?也就是说,如果其中一个组发生更改,则不会有触发事件来根据百分比重置我的对象。你怎么看?也许需要通过像素距离坚持使用 Corona 设置?

标签: coronasdk


【解决方案1】:

我认为这个问题可能是一个错误:“myGroup:setReferencePoint(display.TopLeftReferencePoint) 导致项目移动”

即:

  • AnscaMobile 博客指出:“主要区别在于组的默认参考点是 display.TopLeftReferencePoint,...
  • 将现有默认值应用于组会更改组中不应出现的项目位置:例如代码:“myGroup:setReferencePoint(display.TopLeftReferencePoint)

因此,解决方法可能必须在类中执行 if/then 检查以确保 setReferencePoint 仅应用于非组。

作为案例 #14049 与 anscamobile 一起提出

【讨论】:

    最近更新 更多