lua - 根据变量显示和隐藏图像

标签 lua coronasdk

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

我尝试删除上一张图像并显示新图像,但它说“尝试使用 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 )

函数 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

最佳答案

而不是:

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

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

关于lua - 根据变量显示和隐藏图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429880/

相关文章:

string - Lua - 检测字符串是否包含密码的有效字符

android - 如何让多个乒乓球弹跳

multidimensional-array - 在 Lua 中向表添加附加值

android - 在 Corona SDK 中,如何让一条线在绘制另一条线时消失?

lua - Lua功能的性能和优化

lua - 更改对象中表的值会更改所有对象的值。如何让它只针对特定对象进行更改?

lua - 添加 '' physics.addBody''本地imgpath lua

ios - 停止功能 Corona SDK

sockets - Lua打开套接字报错

lua - 计算字符串出现的次数