在Corona中显示精灵表中的随机精灵

时间:2014-12-10 12:27:13

标签: random lua corona sprite

对Corona来说很新,并尝试制作简单的记忆游戏。我想从spritesheet(包含大约20个精灵)中闪现约5个随机精灵,然后让用户在显示表单时输入他们认为闪现的内容。希望我已经解释好了吗?我之前做过一些JavaScript,但对编码没有信心。尝试做一些具有挑战性的事情 - 可能需要做一些更简单的事情。被告知Corona是一个很好的起点:( math.random会是正确的方法吗?

任何想法我都会感激不尽。目前我只能显示整张表,不知道如何使随机精灵出现。

2 个答案:

答案 0 :(得分:2)

这里有一些更深入的例子,它可以帮助你建立这个系统(之前的答案有些错误)

local cards = {{name="one",count=0,flipped=false,matched=false},{name="two",count=0,flipped=false,matched=false},{name="three",count=0,flipped=false,matched=false},{name="four",count=0,flipped=false,matched=false},{name="five",count=0,flipped=false,matched=false},{name="six",count=0,flipped=false,matched=false}};
function getAvailableCards(Count)
    local suitable_cards = (Count and {} or nil); -- create a table if you want to get keys of a valid cards in the cards table
    local empty = true;
    for key,data in pairs(cards) do
        if (data.count < 2)then
            if (Count) then -- add key if you want an output (table with valid keys)
                table.insert(suitable_cards,key);
                -- table.insert(suitable_cards,key); you can insert it again for higher range of numbers to select from, making math.random a bit more "random".
                empty = false;
            else
                return true;
            end
        end
    end
    if (empty) then
        return false;
    end
    return suitable_cards;
end
function getRandomCard()
    local valid_cards = getAvailableCards(true); -- get a table with all valid keys of the cards table, to pick a random key
    if (not valid_cards) then
        return valid_cards; -- return false
    end
    local index = valid_cards[math.random(1,#valid_cards)]; -- select random key
    cards[index].count = cards[index].count + 1; -- increase the count of key generation
    return cards[index].name,index -- return card id and index from the cards table
end
function setupCards()
    while getAvailableCards() do
        local card_id,index = getRandomCard(); -- gets a random card id and it's index
        -- setup the card in GUI and such,
        -- like create a board and then create an image and load the image (based on card id)
        -- As an example (no such functions, but you should create them or find functions similiar to them)
        -- card = getBoard().NewImage();
        -- card.id = index;
        -- card.loadImage("hidden_card.png");
    end
end
function TryToMatch()
    local child1,child2 -- child tables that will be copied from the cards table, for later comparing
    -- get all flipped but not matched cards
    for key,child in pairs(cards) do
        if (child.flipped and not child.matched) then
            if (child1) then
                child2 = key; -- get key
            else
                child1 = key; -- get key
            end
        end
    end
    if ( child1 and child2) then -- two revealed cards can be checked
        if (child1.name == child2.name) then -- reveal
            cards[child1].matched,cards[child2].matched = true,true;
            TryToEnd();
        else
            --un-reveal it
        end
    end
end
function TryToEnd()
    local matched = 0;
    for key,child in pairs(cards) do
        if (child.flipped and child.matched) then
            matched = matched + 1;
        end
    end
    if (#cards == matched) then
        -- display that the game is finished
    end
end
function Click(object)
    -- When an object is clicked make it call this function with the object
    if (object and object.id and cards[object.id] and not (cards[object.id].flipped and cards[object.id].matched)) then
        cards[object.id].flipped = true;
        -- You should load here the card image (actual card image, like you revealed the card)
        -- Example
        -- object.loadImage(cards[object.id].name.. ".png");
        TryToMatch();
        return true
    end
    return false
end

编辑: 我没有测试这个脚本也没有使用Corona,它可能有一些bug,理论上它应该可以正常工作。

答案 1 :(得分:1)

游戏中用于从集合中获取随机对象的常用技术是使用随机生成的数字,如math.random,然后使用模运算符%从{{1}获取数字} 0

例如,假设您有一张卡片列表:

number of objects

如果您想随机选择一张卡片,可以执行以下操作:

local cards = {"one","two","three","four","five"};
local numCards = #cards;

我希望这能指出你正确的方向。