私はコロナとルアの学習を始めたばかりです。私の最初のプロジェクトは小さなサイコロゲームを作ることで、私はそれを始めたばかりです! これがサイコロのオブジェクトです...
--declare table + metatable
local dice = {}
local dice_mt = {__index = dice}
--CONSTRUCTOR
function dice.new(ypos, xpos)
local newdice = {xpos = xpos, ypos = ypos,dicetext = display.newText("X", 50*xpos , 50*ypos , nil, 50)}
--dicetext = display.newText(facevalue, 50*xpos , 50*ypos , nil, 50)
return setmetatable(newdice, dice_mt)
end
--rolldice function
function dice:rolldice()
self.dicetext.text = math.random(0,6)
end
return dice
ゲームは、転がすことができる25個のサイコロのグリッドを持つことです(上記のrolldice関数を使用) Heres my main.lua
display.setStatusBar(display.HiddenStatusBar)
local diceclass = require ("dice")
for i = 1, 5, 1 do
local die = diceclass.new(i,i)
i = i+1
end
die:rolldice()
プログラムでサイコロのグリッドを生成しようとしています (atm 対角線に 5 つのサイコロ (テキストのみ) を生成しています。これまでのところ問題ありませんが、問題はロールダイス機能を実行することです。サイコロが 1 つしかない場合に機能しますが、もっとあるときではありません.問題は、クラスのこれらすべてのインスタンスを同じ名前で持っていることだと思います.forループでそれらを作成するときに、プログラムでそれらに異なる名前を付ける方法はありますか?またはありますか?これを行うより良い方法はありますか?ありがとう!