Lua 显示图像并根据变量隐藏它

Lua 显示图像并根据变量隐藏它,lua,coronasdk,Lua,Coronasdk,我想显示一个图像,在一个特定的区域,根据一个变量。例如,当用户单击化身时,它显示特定的图像。一旦他点击了其他的化身,其他的图像就会显示出来 我尝试删除以前的图像,并显示新图像,但它表示尝试使用nil值调用removeself 在“开始”中定义表: local item_bigsize = {} 所以我试了一下: item[i] = display.newImageRect("items/"..items['pos'..i..'_name']..".png", 80 , 80) item[i].

我想显示一个图像,在一个特定的区域,根据一个变量。例如,当用户单击化身时,它显示特定的图像。一旦他点击了其他的化身,其他的图像就会显示出来

我尝试删除以前的图像,并显示新图像,但它表示尝试使用nil值调用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 )
开关压力机的功能如下:

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
首先,为什么要手动增加n的值,for循环会自动将n增加1,所以不增加它

因此,当i=1和n=1第一次发生时,条件失败,它不会移除

第二次n增加1,您手动将n增加1,因此它将变为3。条件满足时,因为i将为2,并将进入循环,它将尝试删除图像项_bigsize[3]:removeSelf,它不存在

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

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

item_bigsize[i]

因为我不是一个你想要迭代的整数

最后,我选择了隐藏/显示项目,正如@albert在之前的评论中所说的更简单。我还选择使用数据库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
我显示大尺寸图像的功能是:

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
这个解决方案对我来说非常有效

而不是:

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


继续编码

您在哪里定义item_bigsize[]表?此外,您还将该表中的索引n忘在所需文件之后的起始文件LUA中。您可以使用item[i].isVisible=true来显示图像,而false来隐藏图像。@Albert:我已经尝试过了!但是我想删除该对象,然后在用户单击该对象时创建它。我将有200多个项目,无法为每个项目创建图像necessary@Mehmet:Soga,你的意思是同时只显示一张图像,对吗?在您的代码中,我发现您的项[X]都是80x80,项[X]都是240x240,您可以使用display.newSprite和项[I]:setFrameX来显示图像,看到它仍然不工作!我说尝试将字段“?”索引为nil值。事实上,我想知道我删除图像的代码的位置是否正确。。首先,我不是自动等于1!i是event.target.destination的结果。这意味着,如果用户单击项目1,它不会删除项目_bigsize[1],而是删除从2到3的所有其他项目。请提供更多详细信息。。为什么你说我没有item_bigsize[i]?那么请解释item[i]=display中的i值是什么。newImageRecti值将用于item[i]。destination,它是我需要显示的项的ID。不可能。必须设置i的值才能获取项[i]。目标。否则它将是项[nil]。目标。最常见的是将i设置为整数1、2、3、4等。因此,如果您键入item[i],您将从表item中获得第i个元素。看看这个教程
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