【问题标题】:Displaying random sprites from a sprite sheet in Corona在 Corona 中显示来自精灵表的随机精灵
【发布时间】:2015-02-08 15:31:27
【问题描述】:

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

任何想法我将不胜感激。目前我只能显示整个工作表,不知道如何让随机精灵出现。

【问题讨论】:

  • 这是求教程,不是实际问题的答案。

标签: random lua coronasdk sprite


【解决方案1】:

这里有一些更深入的示例,应该可以帮助您构建该系统(之前的答案有一些错误)

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,它可能有一些错误,理论上它应该可以正常工作。

【讨论】:

  • 你能告诉我上一个答案的错误在哪里,以便我更正吗?谢谢
【解决方案2】:

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

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

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

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

local function getRandomCard()
    #This will give you a random number from 0 to 100
    math.randomseed(os.time())
    local randomNumber = math.random(100);

    #This will give a random number from 0 to 4. 
    local randomIndex = randomNumber % numCards; 
    # Lua indices start at 1, so we add this to our index
    randomIndex = randomIndex + 1; #This will get us a number from 1 to 5; =)

    return cards[randomIndex];
end

我希望这会为您指明正确的方向。

【讨论】:

  • 我认为我在正确的轨道上然后我有类似的东西 - 我从 0 开始(完全忘记他们从 1 开始)。将重试。感谢您的回复,我很感激。
  • 你做的很好(编辑是不必要的),你只是忘记了一步。 math.random() 返回一个十进制值,您必须使用math.floormath.ceil 您也可以添加此函数以舍入到壁橱值math.round = function(d)mf,mc=math.floor(d),math.ceil(d); return (mf + 0.5 &gt; d and mf or mc) end;
  • 所以基本上你的函数应该是这个(我所做的唯一改变是添加 math.round)math.round = function(d)mf,mc=math.floor(d),math.ceil(d); return (mf + 0.5 &gt; d and mf or mc) end; function getRandomNumber() return cards[math.round((math.random() * 1000) * % 4)] end
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2011-08-05
  • 2023-03-21
相关资源
最近更新 更多