显示图像&根据变量隐藏它

时间:2014-10-17 16:55:54

标签: lua corona

我想根据变量在特定区域显示图像。例如,当用户点击化身时,它显示特定图像。一旦他点击其他头像,将显示其他图像。

我已尝试删除上一张图片,并显示新图片,但它说"尝试使用零值"调用removeself()。

在开头定义表:

local item_bigsize = {}

所以我试过这个:

item[i] = display.newImageRect("items/"..items['pos'..i..'_name']..".png", 80 , 80)
item[i].x = holder_2[i].x+10
item[i].anchorX=0
item[i].y=holder_2[i].y
itemGroup:insert (item[i])
item[i].destination=i
item[i]:addEventListener( "touch", onSwitchPress )

并且函数onSwitchPress()的工作方式如下:

function onSwitchPress( event )

i=event.target.destination

if (event.phase=="began") then

    title_item.text=""
    desc_item.text=""

    for n=1,3 do
        if n~=i then
            item_bigsize[n]:removeSelf( )
        end
        n=n+1
    end

elseif (event.phase == "ended") then

        item_bigsize[i] = display.newImageRect("items/"..items['pos'..i..'_name']..".png", 240 , 240)
        item_bigsize[i].x = 950
        item_bigsize[i].anchorX=0
        item_bigsize[i].y=display.contentCenterY-130
        group:insert (item_bigsize[i])


    title_item.text=items['pos'..i..'_title']
    desc_item.text=items['pos'..i..'_details']

end  


end

4 个答案:

答案 0 :(得分:2)

而不是:

if n~=i then
  item_bigsize[n]:removeSelf( )
end

尝试:

if(n~=i and item_bigsize[n].x~=nil)then --Check whether the object/properties exists
  item_bigsize[n]:removeSelf()
end

保持编码................:)

答案 1 :(得分:0)

for n=1,3 do
    if n~=i then
        item_bigsize[n]:removeSelf( )
    end
    n=n+1
end

首先,为什么要手动递增n的值,for循环会自动将n递增1.因此,您不要递增它。

那么第一次i = 1和n = 1时会发生什么,条件失败就不会删除。

第二次n增加,并且你手动增加n乘以1,因此它将变为三满足因为我将2并且将进入循环,它将尝试删除图像item_bigsize [3]:removeSelf( )不存在。

我认为这可能是错误,所以删除n = n + 1。

for n=1,3 do
    if n~=i then
        item_bigsize[n]:removeSelf( )
    end
end   

答案 2 :(得分:0)

你没有

item_bigsize[i]

因为“i”不是要在“for”中迭代的整数。

答案 3 :(得分:0)

最后,我选择隐藏/显示项目@albert在之前的评论中说(更简单)。我也选择使用DataBase SQLite3而不是文件,因为它更快。

以下是解决方案:

function ShowItems( event )


local count = 1
local sql = "SELECT * FROM items WHERE active='oui'"

n=1
number=0

    for x in db:urows "select count(*) from items" do 

            for row in db:nrows(sql) do

                            item[count] = display.newImageRect("items/"..row.src..".png", 80 , 80)
                            if n <= 7 then 
                                item[count].x = holder[n].x+10
                                item[count].y=holder[n].y
                            elseif n >= 8 and n<15 then
                                item[count].x = holder_2[n].x+10
                                item[count].y=holder_2[n].y
                            elseif n >=15 and n<=21 then
                                item[count].x = holder_3[n].x+10
                                item[count].y=holder_3[n].y
                            end
                            item[count].anchorX=0
                            itemGroup:insert (item[count])
                            item[count].destination=row.id
                            item[count]:addEventListener( "touch", onSwitchPress )
                            n=n+1  


                            item_bigsize[count] = display.newImageRect("items/"..row.src..".png", 240 , 240)
                            item_bigsize[count].x = 950
                            item_bigsize[count].anchorX=0
                            item_bigsize[count].y=display.contentCenterY-130
                            bigimages:insert (item_bigsize[count])
                            item_bigsize[count].isVisible=false                   


            count = count + 1
            end

        end  

end

和我的功能,以显示&#34;大&#34;尺寸图片是:

function onSwitchPress( event )

i=event.target.destination

    if (event.phase=="began") then

        number = n-1
        number_equip = a-1

        title_item.text=""
        desc_item.text=""

        item_bigsize[i].isVisible=true

                            if n <= 7 then 
                                holder[i].alpha=1
                            elseif n >= 8 and n<15 then
                                holder_2[i].alpha=1
                            elseif n >=15 and n<=21 then
                                holder_3[i].alpha=1
                            end    

                      for increment=1,number do
                            if increment~=i then
                                  if item_bigsize[increment]~= nil then
                                      item_bigsize[increment].isVisible=false
                                        if n <= 7 then 
                                            holder[increment].alpha=0.5
                                        elseif n >= 8 and n<15 then
                                            holder_2[increment].alpha=0.5
                                        elseif n >=15 and n<=21 then
                                            holder_3[increment].alpha=0.5
                                        end

                                      --print( increment )
                                  end

                              end
                      end

    elseif (event.phase == "ended") then                                    

        result = "SELECT name,desc FROM items WHERE id ="..i..""

        for col1,col2 in db:urows( result ) do
            title_item.text=col1
            desc_item.text=col2
        end
    end  


end

这个解决方案对我来说效果很好!

相关问题