This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-02-25 22:16:44

tygar
Scratcher
Registered: 2007-06-14
Posts: 1

Getting values for variables

Is there a way to get a value (from user) during runtime and assign it to a variable that has been created?  For example, if trying to do a simple number guessing game, is it possible to ask the user for a number, assign it to variable "guess" and compare it to variable "number", which has been randomly generated?

Offline

 

#2 2008-02-26 00:52:15

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Getting values for variables

There are a few ways.

1) Right click on the variable on the programming screen and choose "slider".  THis allows the user to change the variable in game.

2) Use a set of "If" statements to set it via keyboard entry (if 1 pressed, set "number" to 1 etc)

3) Have buttons on screen that when clicked, set the variable to a number.  (If "Button1" clicked, set "number" to one.)

Using different variables for the units, tens or even 100's and 1000's allows multi-digit numbers to be set with just the 0-9 keys, as does multiplying the exisiting number by 10 before adding to it.

IE, number set to zero.  Player presses 3, "Number" increased by 3 (to make 3).  Player presses 2, Number multiplied by 10 (30) then increased by 2 (32)


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#3 2008-02-26 08:41:28

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Getting values for variables

The calculator programs show how to enter arbitrary numbers from a keyboard:
http://scratch.mit.edu/projects/kevin_karplus/76772

Offline

 

Board footer