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

#1 2012-04-22 12:15:48

bobinthebag
New Scratcher
Registered: 2012-04-22
Posts: 6

SET Variables and If Statements - Student Help!

Hello,

I am a student at the OU learning Sense/Scratch, I seem to be stuck on a bit of code and could do with a nudge in the right direction.

I am building a very basic word processor, when a key is pressed on the keyboard the letter is printed on the stage:

when [h] key pressed
pen down
point in direction [0]
move (30) steps
point in direction [180]
move (15) steps
point in direction [90]
move (20) steps
point in direction [0]
move (15) steps
point in direction [180]
move (30) steps
set [keyPressed] to [true]
I have scripts assembled for other characters and numbers, this works for what I need and when a key is pressed it gets drawn on the stage - cool stuff!

The next part is to assemble a script which will allow me to write the letters next to each other in a straight line. I am new to this, but believe I need an If block with a comparison operator to say If keyPressed = True, then inside the IF block I have some motion instructions to move the sprite x position to the right ready for next letter:

when gf clicked
clear
set pen size to (3)
pen up
go to x: [-220] y: [140]
point in direction [90]
forever
if <<keyPressed> = [true]>
pen up
point in direction [90]
go to x: <<x position> + [20]> y: [140]
end
end
The only problem is when I run the script nothing runs within my If block and it seems to get ignored, despite keyPressed been true when I hit one of the keys.

This is part of an assignment so without giving me a direct answer can anyone help point me in the right direction or tell me where I am going wrong? I have spent days trying to figure this out and really need a nudge, thank you in advance  yikes

Offline

 

#2 2012-04-22 12:21:58

CheeseMunchy
Scratcher
Registered: 2008-10-13
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

I don't see any reason for in not to work.  hmm


6418,

Offline

 

#3 2012-04-22 12:26:00

CheeseMunchy
Scratcher
Registered: 2008-10-13
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

Can you upload the project so I can take a closer look?


6418,

Offline

 

#4 2012-04-22 12:39:12

bobinthebag
New Scratcher
Registered: 2012-04-22
Posts: 6

Re: SET Variables and If Statements - Student Help!

Sorry not to sure how to upload my project, I am using sense which is the same as scratch but does not include a share button. You can download my project from http://www.filefactory.com/file/1ifcxchoc4q7/n/myproject_sb

It definitely does not work as intended, when the green flag is clicked the sprite just shoots off the stage!!!!

Offline

 

#5 2012-04-22 12:44:14

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

Add set [keyPressed v] to [false] at the top of the green flag hat stack, and also at the end of the if clause.

Offline

 

#6 2012-04-22 13:04:26

bobinthebag
New Scratcher
Registered: 2012-04-22
Posts: 6

Re: SET Variables and If Statements - Student Help!

Fantastic thank you so much, can you tell me why the variable needed to be set to false? I was under the impression it was false until the key was pressed then it was set true?

Offline

 

#7 2012-04-22 13:17:48

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

You can probably do this without a variable or an if block by always positioning the sprite at the end of a letter so that it is ready to draw the next one.  You would probably want to left the pen up before that final positioning though.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#8 2012-04-22 13:25:07

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

bobinthebag wrote:

Fantastic thank you so much, can you tell me why the variable needed to be set to false? I was under the impression it was false until the key was pressed then it was set true?

Because once being set to true, there was nothing else telling it to go back to being false.

EDIT: Oops, forgot to rainbowify!

Last edited by scimonster (2012-04-22 13:25:39)

Offline

 

#9 2012-04-22 13:40:05

bobinthebag
New Scratcher
Registered: 2012-04-22
Posts: 6

Re: SET Variables and If Statements - Student Help!

Thanks for your help guys, Paddle2See that's an interesting point however I think the assignment is more aimed towards basic use of setting variables and making use of if blocks. I now understand how to carry out instructions within an if block when a control operator says variable name = true (whoopy)!  As scimonster says, once true there was nothing setting it back to false, which is why my sprite was flying off the stage! lol.

Offline

 

#10 2012-04-25 16:17:11

Bigdave89
New Scratcher
Registered: 2012-04-25
Posts: 1

Re: SET Variables and If Statements - Student Help!

scimonster wrote:

Add set [keyPressed v] to [false] at the top of the green flag hat stack, and also at the end of the if clause.

Hi there, im having a simliar problem as the OP. Could you explain what Add set [keyPressed v] to [false] is please?

Offline

 

#11 2012-04-25 16:33:35

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

Bigdave89 wrote:

scimonster wrote:

Add set [keyPressed v] to [false] at the top of the green flag hat stack, and also at the end of the if clause.

Hi there, im having a simliar problem as the OP. Could you explain what Add set [keyPressed v] to [false] is please?

You need to add the block

set [keyPressed v] to [false]
to start of each stamping script.

Offline

 

#12 2012-04-25 16:38:35

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: SET Variables and If Statements - Student Help!

@bobinthebag- Your intuition that a variable should be false until set to be true would seem right, but unlike in most programming languages, Scratch's variables retain their values even when the program is not running.  This allows the programmer to store massive amounts of data in variables or lists without telling the program to populate the list upon each execution.  Likewise, variables who are assigned a value never stop holding that value unless they are told to do so.

Offline

 

#13 2012-04-25 16:53:42

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

scimonster wrote:

bobinthebag wrote:

Fantastic thank you so much, can you tell me why the variable needed to be set to false? I was under the impression it was false until the key was pressed then it was set true?

Because once being set to true, there was nothing else telling it to go back to being false.

EDIT: Oops, forgot to rainbowify!

Umm... This is correct...

Last edited by ImagineIt (2012-04-25 16:54:09)

Offline

 

#14 2012-04-25 16:55:27

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: SET Variables and If Statements - Student Help!

I don't think that all of the obsolete blocks really help. Maybe there is a sense forum?

Offline

 

Board footer