0

我需要一个图形窗口,当用户单击一个按钮时,它会一遍又一遍地显示一条消息。我已经在互联网上查看了有关如何不使其重叠的说明。这很可能是一个快速修复,但 idk。请帮助这里是我的代码。我正在尝试制作一个答题器游戏,但随着这个问题的发生而被拖走。

GraphicsWindow.Height = 420
GraphicsWindow.Width = 720
GraphicsWindow.CanResize = "1"

button = Controls.AddButton("Click for eggs",200,200)
Controls.SetSize(button,100,100)

eggs = 0

Controls.ButtonClicked = buttonClicked

Sub buttonClicked

lastButtonClicked = Controls.LastClickedButton

If lastButtonClicked = button Then
eggs = eggs + 1
GraphicsWindow.DrawText(0,0,"You have " + eggs + " eggs")   
ElseIf eggs >= 1 Then  
GraphicsWindow.BackgroundColor = "White"
GraphicsWindow.DrawText(0,0,"You have " + eggs + " eggs")  
EndIf
EndSub
4

2 回答 2

0

据我所知,这种确切的效果在 Small Basic 中是不可能的,因为在GraphicsWindow不清除整个窗口的情况下无法编辑或删除绘制到的内容。

相反,我会使用TextBoxfrom Controls,它可以在创建后进行编辑。因为TextBox通常可以由用户编辑,所以我还添加了代码来防止内容被编辑。

有关其工作原理的更多信息,请参阅我在代码中的注释。

GraphicsWindow.Height = 420
GraphicsWindow.Width = 720
GraphicsWindow.CanResize = "1"

button = Controls.AddButton("Click for eggs",200,200)
Controls.SetSize(button,100,100)

eggs = 0

Controls.ButtonClicked = buttonClicked

' Create a text box to show the egg count
myTextBox = Controls.AddTextBox(0, 0)

' Ensure the user can't edit its contents by resetting the text if it changes
Controls.TextTyped = updateEggs

Sub updateEggs
  ' Change the text of myTextBox
  Controls.SetTextBoxText(myTextBox, "You have " + eggs + " eggs")   
EndSub

Sub buttonClicked
  lastButtonClicked = Controls.LastClickedButton

  If lastButtonClicked = button Then
    eggs = eggs + 1
    updateEggs()
  ElseIf eggs >= 1 Then  
    GraphicsWindow.BackgroundColor = "White"
    updateEggs()
  EndIf
EndSub

此 GIF 演示了TextBox外观和工作原理,以及文本如何无法更改:

动图

于 2016-08-09T12:24:02.787 回答
0

您所要做的就是使用 Shapes.AddText。这将创建一个可以使用 Shapes.SetText 修改的文本形状

例子:

GraphicsWindow.Height = 420
GraphicsWindow.Width = 720
GraphicsWindow.CanResize = "1"

Text = Shapes.AddText("You have 0 eggs")

button = Controls.AddButton("Click for eggs",200,200)
Controls.SetSize(button,100,100)

eggs = 0

Controls.ButtonClicked = buttonClicked

Sub buttonClicked
lastButtonClicked = Controls.LastClickedButton

If lastButtonClicked = button Then
eggs = eggs + 1
Shapes.SetText(Text,"You have " + eggs + " eggs")   
EndIf
EndSub
于 2016-08-10T04:00:03.820 回答