在表格中打乱图片。

我想要随机排列图像表并存储这些图像的坐标,有什么方法可以实现吗?我已经尝试了以下代码:

local alpha =   {{"alpha_a"} , {"alpha_b"} , {"alpha_c"} , {"alpha_d"} ,
    {"alpha_e"} , {"alpha_f"} , {"alpha_g"} , {"alpha_h"}}

local coordinates ={{x=092, y=470}, {x=197, y=470}, {x=302, y=470},
        {x=407, y=470}, {x=512, y=470}, {x=617, y=470} }

    for i=1, #alpha do
        local selection = table.remove(coordinates, math.random(#coordinates))
        print(selection.x,selection.y, #coordinates)
        images = display.newImage(alpha[i][1]..".png")
        images.x = selection.x
        images.y = selection.y
        images:addEventListener("touch",swapping)
    end

原文链接 https://stackoverflow.com/questions/7885730

点赞
stackoverflow用户513763
stackoverflow用户513763

并不完全清楚您试图实现什么,但我猜测可能是将图像与其自身的坐标保持在一起。

我认为完成这个的逻辑方法是重新考虑您的数据结构,并将坐标和名称放入同一个表中,例如

local alpha = {{"alpha_a",x=092, y=470} , {"alpha_b",x=197, y=470} , {"alpha_c",x=302, y=470} , {"alpha_d",x=407, y=470} , {"alpha_e",x=512, y=470}} --...
2011-10-25 07:25:41