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

#1 2012-11-21 16:02:15

dimasrm
New Scratcher
Registered: 2012-11-21
Posts: 4

command if

Dear Colleagues,

I have no idea why my scratch code doesn't work:

For one Sprite, I create a global numerical variable and load it with a random number between 1 and 10.
in the sequence I try to use this variable in a IF instruction (on the same Sprite or on the other) but, unfortunately it doesn't work.
I never run the TRUE condition of the IF...

thank's for any help or idea.

Offline

 

#2 2012-11-22 16:00:34

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: command if

Hi dimasrm, Welcome to Scratch! You put this in the Portuguese forums, so I'll move it over to the "Help with Scripts" Section where you'll probably get a lot more replies  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#3 2012-11-22 16:38:56

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: command if

The problem might be in your scripts, so could you give them?


http://i46.tinypic.com/35ismmc.png

Offline

 

#4 2012-11-22 17:14:35

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: command if

There is a probably a specific problem with your script.  As ErnieParke asked, could you post your scripts.  Our responses should be much more helpful if we have concrete code to deal with.  smile


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-11-23 17:54:43

dimasrm
New Scratcher
Registered: 2012-11-21
Posts: 4

Re: command if

Hi, friends!
I put my script below. I appreciate your tips.

when gf clicked
  set myNumber to <peek random <(1) to [10]>>
  if myNumber > 5 
     say <BIG>
  else
     say <little>
end

Offline

 

#6 2012-11-23 18:21:27

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: command if

dimasrm wrote:

Hi, friends!
I put my script below. I appreciate your tips.

when gf clicked
  set [myNumber v] to <pick random (1) to (10)>
  if <(myNumber) > (5)>
     say [BIG]
  else
     say [little]
end

Okay, I think that I see the problem. Was your sprite showing when you ran this code?


http://i46.tinypic.com/35ismmc.png

Offline

 

#7 2012-11-23 18:32:48

dimasrm
New Scratcher
Registered: 2012-11-21
Posts: 4

Re: command if

ya, my sprite was showing...and always say "little" doesn't matter what is the value of myNumber..

Offline

 

#8 2012-11-23 18:38:04

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: command if

dimasrm wrote:

ya, my sprite was showing...and always say "little" doesn't matter what is the value of myNumber..

This is strange; could you upload your project?


http://i46.tinypic.com/35ismmc.png

Offline

 

#9 2012-11-24 09:00:33

awesomeness321
Scratcher
Registered: 2012-08-10
Posts: 100+

Re: command if

dimasrm wrote:

ya, my sprite was showing...and always say "little" doesn't matter what is the value of myNumber..

Did you check the value of myNumber, or did you just assume that it wasn't greater than 5?


My newest project:http://blocks.scratchr.org/API.php?user=awesomeness321&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1Color:#30F9A5 I am currently http://blocks.scratchr.org/API.php?user=awesomeness321&amp;action=onlineStatus&amp;type=text

Offline

 

#10 2012-11-24 09:49:03

mathfreak231
Scratcher
Registered: 2012-10-24
Posts: 80

Re: command if

dimasrm wrote:

ya, my sprite was showing...and always say "little" doesn't matter what is the value of myNumber..

Have it show the variable, then test. Like awesomeness321 said, you may need to check the variable.


~This sig is false~
I'm on teh wiki!

Offline

 

#11 2012-11-25 15:04:46

dimasrm
New Scratcher
Registered: 2012-11-21
Posts: 4

Re: command if

I changed my code and it worked very well.
I put an If instruction like this:
  if <<pick random 1 to 10> GREATER THAN <myNumber>>
and this solved my problem

before I was trying to put the result of <pick random 1 to 10> in a variable and it didn't work.

Thank very much to all...

Now I'm going to finish my project

Offline

 

#12 2013-03-01 09:34:12

Cactus4u
New Scratcher
Registered: 2013-03-01
Posts: 2

Re: command if

But I still don't understand: Why didn't the first approach work? I really want to understand why I cannot generate a global variable and then use it for an if-statement. In particular, I would like to use it across different objects. Any ideas?

Offline

 

#13 2013-03-01 14:56:47

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: command if

Cactus4u wrote:

But I still don't understand: Why didn't the first approach work? I really want to understand why I cannot generate a global variable and then use it for an if-statement. In particular, I would like to use it across different objects. Any ideas?

Well, I'm also curious at to what might've been causing this. One possibility is that they created two separate variables that look alike, but aren't. Therefore, when they used the variable, they're evaluating the wrong one, giving false results. Also, the other possibility is that something might've occurred during download, messing up a file or two.

Anyway, you should be able to use a global variable in your if statement without any problems. I don't see anything saying otherwise.

With regards,

ErnieParke


http://i46.tinypic.com/35ismmc.png

Offline

 

#14 2013-03-01 16:09:14

mathfreak231
Scratcher
Registered: 2012-10-24
Posts: 80

Re: command if

ErnieParke wrote:

Cactus4u wrote:

But I still don't understand: Why didn't the first approach work? I really want to understand why I cannot generate a global variable and then use it for an if-statement. In particular, I would like to use it across different objects. Any ideas?

Well, I'm also curious at to what might've been causing this. One possibility is that they created two separate variables that look alike, but aren't. Therefore, when they used the variable, they're evaluating the wrong one, giving false results. Also, the other possibility is that something might've occurred during download, messing up a file or two.

Anyway, you should be able to use a global variable in your if statement without any problems. I don't see anything saying otherwise.

With regards,

ErnieParke

The most likely reason is that the variable is changed and indexed in multiple sprites.

If you use the variable in more than one place, it may mess up your results.


~This sig is false~
I'm on teh wiki!

Offline

 

#15 2013-03-01 20:10:22

Cactus4u
New Scratcher
Registered: 2013-03-01
Posts: 2

Re: command if

Wow, you are really fast to respond :-) Thanks a lot for your thoughts!

Offline

 

Board footer