GUI.CreateButton calling a procedure with a parameter list?
Author |
Message |
skootles
|
Posted: Fri Jan 13, 2006 12:47 am Post subject: GUI.CreateButton calling a procedure with a parameter list? |
|
|
I want to make it so that when I press a button that I have created, it will call a procedure that has a parameter list. Now, when I put what variable I wanted to put into the procedure after the name of the procedure:
code: | letter(i) := GUI.CreateButton (x, y, 0, chr (i + 96), Begin(letter(i)))
|
I got an error, saying that the procedure Begin cannot return a value. Here is my full code:
code: | import GUI
View.Set ("position:middle;center") % Centers the run window on the screen
var Backgr : int := Pic.FileNew ("images/gallows.jpg") % The Background pictre
Pic.Draw (Backgr, 0, -100, 0) % Draws the background picture
procedure Begin (a:int)
put "yay"
end Begin
var letter : array 1 .. 26 of int % Array that will store the Letters' Button Values.
var x : int := 60 % Initial X position for the letter buttons
var y : int := 50 % Initial Y position for the letter buttons
for i : 1 .. 26
letter(i) := GUI.CreateButton (x, y, 0, chr (i + 96), Begin(letter(i)))
x += 35
if x >= 490 then % If the buttons are getting close to the right side
y -= 30 % of the run window, then a new line of buttons is
x := 60 % started. (x position reset, and y position decreased)
end if
end for
loop
exit when GUI.ProcessEvent
end loop |
Any ideas as to why this doesn't work? I asked my teacher, but he couldn't figure it out either |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Geminias
|
Posted: Fri Jan 13, 2006 5:48 am Post subject: (No subject) |
|
|
First off, why is the return of your: createbutton () function placed into an array?
Secondly, you're problem is that you are trying to add an argument when you are just supposed to tell it the procedure to execute when the button is pressed. SO in actual fact if you want it to execute a procudure that takes parameters then you make the action procedure of the button call the procedure that takes parameters... keeping in mind that the parameters have to be localized to the action procedure unless they are declared globally. |
|
|
|
|
|
skootles
|
Posted: Fri Jan 13, 2006 3:58 pm Post subject: (No subject) |
|
|
I put it in an array, because I didn't want to make 26 individual variables and procedures
And I don't think that solution would work. Ok, it creates 26 buttons, one for each letter in the alphabet. I wanted it so that when you pressed, for example, "a", it would call a procedure, and it would forward (not sure if that's correct terminology, but whatever) the value 1 into the procedure, with 1 being the index of that variable (b would 'forward' 2, c would 'forward' 3, etc, etc.)
I got it working.. with a few bugs, but I had to make 26 individual procedures
Thanks anyways |
|
|
|
|
|
Geminias
|
Posted: Fri Jan 13, 2006 5:46 pm Post subject: (No subject) |
|
|
you should just make one procedure with a bunch of if statements, and depending on what button was pressed you call the action procedure with your parameter. That would just help to organize things. |
|
|
|
|
|
skootles
|
Posted: Fri Jan 13, 2006 6:57 pm Post subject: (No subject) |
|
|
Yes, that's somewhat what I wanted to do, but how would I determine which button was pressed? |
|
|
|
|
|
MysticVegeta
|
Posted: Fri Jan 13, 2006 8:15 pm Post subject: (No subject) |
|
|
Look up for GUI.GetEventWidgetID, its a boolean value for example:
code: |
if GUI.GetEventWidgetID = button1 then
%Code here
end if |
|
|
|
|
|
|
skootles
|
Posted: Fri Jan 13, 2006 9:37 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: Look up for GUI.GetEventWidgetID, its a boolean value for example:
code: |
if GUI.GetEventWidgetID = button1 then
%Code here
end if |
Thanks, that really helped. My code's about 1/6 of what it was
I'm still having one issue however. So, in the beginning you're presented with 26 buttons (a-z), and you try and guess the word. When you click on one, the program determines if the leter corresponding to the button you pushed is in the word, and if it is, it displays all instances of that letter on the screen. So, that works all fine and dandy. When the program realizes that the letter (button) you pushed isn't part of the word, it adds 1 to a variable, then changes the value of another so that the button won't be re-drawn when the DrawButtons Procedure is called.
Now, I had a background that would change based on this variable, but it was messing up. So, I inserted "put WrongGuess", and it showed that it was going up by 19 when I clicked any button. Then, I moved it into the "else" part of the if statement, and it started going up by random numbers when I clicked a button. I put comments below to mark where I put it:
code: | if chr (ButtonID + 96) = word (i) then
RightGuess += 1
GUI.Hide (button (i))
ButtonEn (i) := false
Font.Draw (word (i), location (i), 150, font2, black)
%here
else
WrongGuess += 1
ButtonEn (1) := false
%BackDraw
% and here
end if
end for |
Now, here is my full code below:
code: | import GUI
var button : array 1 .. 26 of int
var ButtonEn : array 1 .. 26 of boolean
var Buttonx : int := 60
var Buttony : int := 50
var ButtonID : int
var RightGuess : int := 0 % The number of Letter Guesses that were correct
var WrongGuess : int := 0
var word : string := "abcdefghijklmnopqrst"
var font2 := Font.New ("sans serif:18:bold")
var location : array 1 .. length (word) of int
var charposition : int := 200
for i : 1 .. length (word)
location (i) := charposition
charposition += 20
end for
var Backgr : int := Pic.FileNew ("images/1.jpg") % The Background pictre
var bodyparts : array 1 .. 12 of int
bodyparts (1) := Pic.FileNew ("images/1.jpg")
bodyparts (2) := Pic.FileNew ("images/2.jpg")
bodyparts (3) := Pic.FileNew ("images/3.jpg")
bodyparts (4) := Pic.FileNew ("images/4.jpg")
bodyparts (5) := Pic.FileNew ("images/5.jpg")
bodyparts (6) := Pic.FileNew ("images/6.jpg")
bodyparts (7) := Pic.FileNew ("images/7.jpg")
bodyparts (8) := Pic.FileNew ("images/8.jpg")
bodyparts (9) := Pic.FileNew ("images/9.jpg")
bodyparts (10) := Pic.FileNew ("images/10.jpg")
bodyparts (11) := Pic.FileNew ("images/11.jpg")
forward procedure DrawButtons
%----------------------------------------------------------------------------------------
procedure RandWord
end RandWord
%----------------------------------------------------------------------------------------
procedure BackDraw
if WrongGuess = 0 then
Pic.Draw (bodyparts (1), 0, 1, 0)
elsif WrongGuess = 1 then
Pic.Draw (bodyparts (2), 0, 1, 0)
elsif WrongGuess = 2 then
Pic.Draw (bodyparts (3), 0, 1, 0)
elsif WrongGuess = 3 then
Pic.Draw (bodyparts (4), 0, 1, 0)
elsif WrongGuess = 4 then
Pic.Draw (bodyparts (5), 0, 1, 0)
elsif WrongGuess = 5 then
Pic.Draw (bodyparts (6), 0, 1, 0)
elsif WrongGuess = 6 then
Pic.Draw (bodyparts (7), 0, 1, 0)
elsif WrongGuess = 7 then
Pic.Draw (bodyparts (8), 0, 1, 0)
elsif WrongGuess = 8 then
Pic.Draw (bodyparts (9), 0, 1, 0)
elsif WrongGuess = 9 then
Pic.Draw (bodyparts (10), 0, 1, 0)
elsif WrongGuess = 10 then
Pic.Draw (bodyparts (11), 0, 1, 0)
end if
DrawButtons
end BackDraw
%----------------------------------------------------------------------------------------
procedure Initialize
for i : 1 .. 26
ButtonEn (i) := true
end for
RandWord
%BackDraw
end Initialize
%----------------------------------------------------------------------------------------
procedure ButtonPress2 (x : int)
ButtonID := x - 2000 % The button ID would normally be 20xx
for i : 1 .. length (word)
if chr (ButtonID + 96) = word (i) then
RightGuess += 1
GUI.Hide (button (i))
ButtonEn (i) := false
Font.Draw (word (i), location (i), 150, font2, black)
else
WrongGuess += 1
ButtonEn (1) := false
put WrongGuess
%BackDraw
end if
end for
end ButtonPress2
%----------------------------------------------------------------------------------------
procedure ButtonPress
ButtonPress2 (GUI.GetEventWidgetID)
end ButtonPress
%----------------------------------------------------------------------------------------
body DrawButtons
for i : 1 .. 26
if ButtonEn (i) = true then
button (i) := GUI.CreateButton (Buttonx, Buttony, 0, chr (i + 96), ButtonPress)
end if
Buttonx += 35
if Buttonx >= 490 then
Buttony -= 30
Buttonx := 60
end if
end for
end DrawButtons
%----------------------------------------------------------------------------------------
procedure main
Initialize
DrawButtons
loop
exit when GUI.ProcessEvent
end loop
end main
main
|
I'm really confused as to what's going on.. can anyone offer (more) help?
Thanks! |
|
|
|
|
|
|
|