2017-01-05 7 views
0

私はComputerCraftでLuaの使いやすいボタンAPIを作成しようとしていますが、何か問題があります。私がするとき:ボタンAPIの作成方法

os.loadAPI("button") 
action=function() 
    term.clear() 
    term.setCursorPos(1,1) 
    print("Hello!") 
end 

button.newButton("B1",5,5,20,10) 
button.drawButton("B1",colors.orange,colors.white) 
button.onClick("B1",action,true) 

何も起こらず、色を描くことさえありません。私はテストを行いました。そして、colors.whiteを変数として保存した後、変数を出力すると、colors APIからのその色の数値コードが返されます。ここで私が持っているものです。

--to use the newButton function, do this: 
--button.newButton(exampleButton) 

--to use onClick function, create a variable like this: 
--exampleFunc=function() 
--(code) 
--end 
--Then call onClick with the same variable: 

--button.onClick(exampleButton,exampleFunc) 

buttons={} 
xPos=0 
yPos=0 

function removeButton(buttonName) 
    for key, fields in pairs(buttons) do 
     if key == buttonName then 
      table.remove(button,buttonName) 
     else 
      print("ERROR: button name not available") 
     end 
    end 
end 

function onClick(buttonName,action,boolean) 
    for key, fields in pairs(buttons) do 
     if boolean then 
      testClick(action) 
     end 
    end 
end 

function drawSeparateButton(x,y,w,h,outLineColor,fillColor) 
    if key == buttonName then 
     x=buttons[buttonName]["x"] 
     y=buttons[buttonName]["y"] 
     w=buttons[buttonName]["w"] 
     h=buttons[buttonName]["h"] 
     paintutils.drawBox(x,y,x+(w-1),y+(h-1),outLineColor) 
     paintutils.drawFilledBox(x+1,y+1,x+(w-2),y+(h-2),fillColor) 
    end 
end 

function testClick(action) 
    for key, fields in ipairs(buttons) do 
     x=buttons[buttonName]["x"] 
     y=buttons[buttonName]["y"] 
     w=buttons[buttonName]["w"] 
     h=buttons[buttonName]["h"] 
     x2=x+(w-1) 
     y2=y+(h-1) 
     button,xPos,yPos=os.pullEvent("mouse_click") 
     if xPos>=x and xPos<=x2 and yPos>=y and yPos<=y2 then 
      action() 
     end 
    end 
end 


function newButton(buttonName,X,Y,W,H) 
    buttons[buttonName] = {x=X,y=Y,w=W,h=H} 
end 

function drawButton(buttonName,outLineColor,fillColor) 
    for key, fields in ipairs(buttons) do 
     if key == buttonName then 
      x=buttons[buttonName]["x"] 
      y=buttons[buttonName]["y"] 
      w=buttons[buttonName]["w"] 
      h=buttons[buttonName]["h"] 
      x2=x+w-1 
      y2=y+h-1 
      x3=x+1 
      y3=y+1 
      x4=x+w-2 
      y4=y+h-2 
      paintutils.drawBox(x,y,x2,y2,outLineColor) 
      paintutils.drawFilledBox(x3,y3,x4,y4,fillColor) 
     elseif key ~= buttonName then 
      print("Button name not availabel") 
     end 
    end 
end 

私はちょうど変数にcolors.whiteのような色を保存できるようにする必要があり、それはカラーコードをcolors.whiteとして返され、していません。私はまた、ボタンがクリックされたことを確認し、ボタンの1つがクリックされたときにユーザが指定した機能を実行できるようにする必要があります。

+0

関数 'newButton'では、for ...do'は 'if ... then'で置き換えなければなりません。 – JPG

+0

私はすでにそれを試しましたが、 'then'は 'in'の近くにあるとのエラーがあります。 – Surge12

答えて

1

私はあなたのプロトタイプコードを見て、私が見るいくつかのエラーを指摘し、あなたの質問に答えようとします。テーブルのキー値を配列に設定し、それを外部からアクセスしたいと仮定します。


あなたの質問にすばやく簡単に答えられるのは、テーブル内にテーブルを格納し、キーやインデックスでアクセスできることです。しかし、設計変更は、exampleFuncを各ボタンテーブルのメンバーとして保存して、特定のボタンに関連付けることです。

例:

buttons = {} 
buttons.playButton = {x=0, y=0, w=10, h=10, func=function() return end} 
buttons.quitButton = {x=0, y=30, w=10, h=10, func=function() return end} 
... 
buttons.quitButton.x = 10 
buttons.playButton.func() 

テーブルはキーが文字列または数値とすることができるキーと値の構造を有します。キーのデータ型に応じてキーを使用して配列にアクセスするには、複数の方法があります。

たとえば、buttons.quitButton.x = 10の代わりに、buttons["quitButton"].x = 10またはbuttons["quitButton"]["x"] = 10またはbuttons.quitButton["x"] = 10と書くことができます。

This pageは、Luaのテーブルについて学習するのに最適な出発点です。 this pageによると


os.pullEvent()はブロックされ、そしてあなただけの1つのボタンがマウスクリックごとにクリックされたかどうかを確認することができるようになります。 buttonsテーブルをループし、すべてのボタンをチェックしてマウスがその矩形範囲内に入るかどうかを確認してください。マウスがクリックしたボタンを見つけたら、funcのメンバーに電話をかけることができます。この方法についてはまだ議論していますが、ループは完全には不要です(while true do)。


function removeButton(buttonName) 
    for buttonName in pairs(button) do 
    ... 

function newButton(buttonName) 
    state=true 
    for buttonName in pairs(buttons) do 
    ... 

あなたは声明if element in listが存在しますが、Luaがそのような声明を持っていないPythonの背景から来るかもしれません。 forのループは、リストのすべてのメンバーをループしています。また、pairs()関数によって返されたすべての変数をキャプチャしていません。

function buttonFunction(buttonName) 
    for key, fields in pairs(buttons) do 
     if key == buttonName then 
     ... 
    end 
end 

あなたがbuttonsを意味するときに、変数buttonを参照する複数のインスタンスがあります。そのための修正は、以下のようになります。

+0

おかげさまで、このうちのいくつかはとても役に立ちましたが、テーブルはまだまだ私にとっては新しいものです。キーが何であるかは分かりません。 – Surge12

+0

キーと値のペアは、変数に似ています。キーは値(変数名)を識別できる任意のハンドルであり、値は格納する任意のデータ(変数値)です。同じテーブルでは、同じ値を持つ複数のユニークキーを持つことができますが、値が別のテーブルでない限り、複数の値を参照するキーを1つ持つことはできません。また、私の答えが有用で満足できるものだと思うなら、スコアの下にあるチェックマークをクリックして、それが合格とマークされていることを確認してください。 – Thelmund

+0

また、Luaはまだ私にはいくぶん新しかったので、私はどのようにボタンテーブルをループすることができないのか分かりません。 – Surge12

関連する問題