2014-10-17 57 views
2

我想根據變量在特定區域顯示圖像。例如,當用戶點擊一個化身時,它會顯示一個特定的圖像。一旦他點擊其他頭像,其他圖像將被顯示。根據變量顯示圖像並隱藏它

我試圖刪除以前的圖像,並顯示新的,但它說「試圖調用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 
+0

你在哪裏定義「item_bigsize []」表? (此外,您忘記了該表格中的索引n) – rsc 2014-10-17 17:00:11

+0

在「必需文件」後面的起始文件LUA中 – 2014-10-17 17:12:00

+1

您可以使用item [i] .isVisible = true'來顯示圖像,並使用'false'來隱藏他們。 – Albert 2014-10-20 05:34:19

回答

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 

保持編碼................ :)

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

第一所有爲什麼你增加價值手動,for循環會自動將n遞增1.因此,您不會增加n。

因此,第一次發生在這裏,我= 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 
+0

還是不行!我說試圖索引字段'?' (零值)。事實上,我想知道如果我的代碼「刪除」圖像的位置是正確的。 – 2014-10-18 09:03:45

+0

順便說一下..首先,我不是自動平等1!「我」是「event.target.destination」的結果。所以這意味着,如果用戶點擊項目#1,它不會刪除item_bigsize [1],但是所有其他的從2到3(例如)。 – 2014-10-18 09:27:16

0

你不必

item_bigsize[i] 

因爲「我」是不是要在迭代「爲」一個整數。

+0

請給出更多細節..你爲什麼說我沒有「item_bigsize [i]」? – 2014-10-20 14:46:56

+0

所以請解釋「item [i] = display.newImageRect」中的「i」值是什麼? – Lukis 2014-10-21 22:42:14

+0

「i」值是「item [i] .destination」的值,它是我需要顯示的項目的ID 。 – 2014-10-22 11:05:00

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 

和我的函數來顯示「大」尺寸的圖像是:

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 

該解決方案非常適用於我!