There were a couple of problems with the code. It first complained that roll
wasn't a method available in Shoes::App, so I put the method definition in the
app block. Then noticed it returns a para object if the die type is invalid, so
I took the para out and returned a string instead, as expected, as it is called
later by para in the keypress block.
Finally, you're checking if if the key k == :enter
If you're using raisins, this works. I tested in policeman too, but that
doesn't work. I've had this problem before. In policeman, you should check if k
== "\n" (and those have to be double quotes I think, since it's an escape
character).
Also did some totally unnecessary stuff, like change the regex pattern and
syntax for describing the die roll to make it easier to enter, and clear the
editbox when enter is pressed. Not that it matters.
Anyways, the following code with the minor changes works for me (both raisins
and policeman):
Shoes.app do
def roll die
a = nil
if (a = die.match(/([0-9]+)\s+([0-9]+)/))
answer = 0
a[1].to_i.times do
answer += rand(a[2].to_i) + 1
end
return answer
else
return "Not a valid die type."
end
end
@text = para "Type a die roll into the box, and then hit enter. Ie: 2 6"
flow do
@editline = edit_line
end
keypress do |k|
if k == :enter or k == "\n"
a = roll @editline.text
@editline.text = ''
para a
@text.replace a
end
end
end
Subject: Is my function not working, or is it shoes?
From: [email protected]
To: [email protected]
Date: Sat, 11 Jul 2009 22:23:16 -0600
ef roll die a = nil if (a = die.match(/([0-9]+)d([0-9]+)/)) answer = 0
a[1].to_i.times do answer += rand(a[2].to_i) + 1 end return answer
else para "Not a valid die type." endend
Shoes.app do @text = para "Type a die roll into the box, and then hit enter.
Ie: 2d6"
flow do @editline = edit_line end keypress do |k| case k when
:enter a = roll @editline.text para a.to_s
@text.replace a end endend
There is my code. I know my 'roll' method works fine. When I hit 'enter' when
the editline has focus, nothing happens... any help?
--
--Brains.
_________________________________________________________________
Insert movie times and more without leaving HotmailĀ®.
http://windowslive.com/Tutorial/Hotmail/QuickAdd?ocid=TXT_TAGLM_WL_HM_Tutorial_QuickAdd_062009