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

#1 2012-10-24 17:16:19

acedannyk
Scratcher
Registered: 2009-02-26
Posts: 55

Variable Problem

Variable = "Speed Level"

There is a sprite which is like a bar with 6 costumes (A.B,C,D,E,F)
When Speed Level = 2 then costume of bar = A
When Speed Level = 2.1 then costume of bar = B
When Speed Level = 2.2 then costume of bar = C
When Speed Level = 2.3 then costume of bar = D [This is where the problem happens]

Speed levels 2 through to 2.2 always appear as "true" when checked but when speed level equals 2.3 it says it is false when checked, despite the number being CLEARLY 2.3 when looked at.

Any help would be appreciated.

Offline

 

#2 2012-10-24 18:25:24

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

Re: Variable Problem

acedannyk wrote:

Variable = "Speed Level"

There is a sprite which is like a bar with 6 costumes (A.B,C,D,E,F)
When Speed Level = 2 then costume of bar = A
When Speed Level = 2.1 then costume of bar = B
When Speed Level = 2.2 then costume of bar = C
When Speed Level = 2.3 then costume of bar = D [This is where the problem happens]

Speed levels 2 through to 2.2 always appear as "true" when checked but when speed level equals 2.3 it says it is false when checked, despite the number being CLEARLY 2.3 when looked at.

Any help would be appreciated.

What I'm thinking is that your script might be the problem, so could I see all of your script?


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

Offline

 

#3 2012-10-24 18:38:24

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Variable Problem

Instead of testing for specific decimal numbers, try multiplying by 10 and checking integers.

set [MyVar v] to (2.1)
if <((MyVar) * (10)) = (21)>
  switch to costume [A v]
end
Or, subtract to test the difference

set [MyVar v] to (2.1)
if <([abs v] of ((MyVar) - (2.1))) < (0.001)>
  switch to costume [A v]
end

Last edited by BoltBait (2012-10-24 18:44:32)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#4 2012-10-24 18:41:50

dvd4
Scratcher
Registered: 2010-06-30
Posts: 1000+

Re: Variable Problem

BoltBait wrote:

Instead of testing for specific decimal numbers, try multiplying by 10 and checking integers.

set [MyVar v] to (2.1)
if <((MyVar) * (10)) = (21)>
  switch to costume [A v]
end
Or, subtract to test the difference

set [MyVar v] to (2.1)
if <abs(((MyVar) - (2.1))) < (0.001)>
  switch to costume [A v]
end

um,what's with the "x 10" ?


I made a mod  big_smile  It's called blook!
http://i49.tinypic.com/16ia63p.png

Offline

 

#5 2012-10-24 18:43:37

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

Re: Variable Problem

BoltBait wrote:

Instead of testing for specific decimal numbers, try multiplying by 10 and checking integers.

set [MyVar v] to (2.1)
if <((MyVar) * (10)) = (21)>
  switch to costume [A v]
end
Or, subtract to test the difference

set [MyVar v] to (2.1)
if <([abs v] of ((MyVar) - (2.1))) < (0.001)>
  switch to costume [A v]
end

Fixed.


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

Offline

 

#6 2012-10-24 18:45:43

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Variable Problem

ErnieParke wrote:

BoltBait wrote:

Instead of testing for specific decimal numbers, try multiplying by 10 and checking integers.

set [MyVar v] to (2.1)
if <((MyVar) * (10)) = (21)>
  switch to costume [A v]
end
Or, subtract to test the difference

set [MyVar v] to (2.1)
if <([abs v] of ((MyVar) - (2.1))) < (0.001)>
  switch to costume [A v]
end

Fixed.

Thanks, Ernie.  I can never figure out some of the more obscure blocks.  smile

You should edit the scratch wiki and put more examples in there.  I would do it, but I can't remember my password.  sad

Last edited by BoltBait (2012-10-24 18:50:39)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#7 2012-10-24 20:43:33

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

Re: Variable Problem

BoltBait wrote:

ErnieParke wrote:

BoltBait wrote:

Instead of testing for specific decimal numbers, try multiplying by 10 and checking integers.

set [MyVar v] to (2.1)
if <((MyVar) * (10)) = (21)>
  switch to costume [A v]
end
Or, subtract to test the difference

set [MyVar v] to (2.1)
if <([abs v] of ((MyVar) - (2.1))) < (0.001)>
  switch to costume [A v]
end

Fixed.

Thanks, Ernie.  I can never figure out some of the more obscure blocks.  smile

You should edit the scratch wiki and put more examples in there.  I would do it, but I can't remember my password.  sad

I would like to do that, but I don't have an account on the Scratch Wiki, which makes me wonder... I found out how.

Anyway, I've also found some blocks to be hard to recall at times, so to figure out their code, I often would quickly open Scratch or hunt through the scratchblocks testing area for an example. Anyhow, your welcome!


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

Offline

 

#8 2012-10-25 12:24:29

acedannyk
Scratcher
Registered: 2009-02-26
Posts: 55

Re: Variable Problem

It can operate fine when the numbers are integers but that's not how it should be, I need it to use decimals. The script can recognise when the variable is 2 and switch to the corresponding costume, likewise for 2.1 and 2.2. When it gets to 2.3 however, it does not work, though the visual variable bar in the project screen says 2.3. Clicking on the script asking whether or not it is 2.3 shows that it is false, also. This is pretty odd and I cannot find any fault with anything.

I've used Scratch for a long time, not even the text glitch is this is irritating.

Last edited by acedannyk (2012-10-25 12:40:12)

Offline

 

#9 2012-10-25 15:29:09

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

Re: Variable Problem

Try using this before changing the sprite's costumes:

set [variable v] to ((round ((variable) * (10)))/(10))
Does it work now?


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

Offline

 

#10 2012-10-25 15:51:39

acedannyk
Scratcher
Registered: 2009-02-26
Posts: 55

Re: Variable Problem

Sorry, here is a look of what is fundamentally wrong

Last edited by acedannyk (2012-10-25 16:06:35)

Offline

 

#11 2012-10-25 16:49:26

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Variable Problem

That is a classic precision problem when converting base 10 to base 2.

Try your tests this way:

if <([abs v] of ((Speed Level) - (2.3))) < (0.001)>
  switch to costume [Yellow v]
end


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#12 2012-10-25 17:00:34

acedannyk
Scratcher
Registered: 2009-02-26
Posts: 55

Re: Variable Problem

It's a shame I'll have to use that bulky scripting but I'll have to try it here.

Offline

 

#13 2012-10-25 17:13:42

acedannyk
Scratcher
Registered: 2009-02-26
Posts: 55

Re: Variable Problem

Works like a charm. Thanks for the helping hand, guys. Much appreciated.

Offline

 

#14 2012-10-27 22:42:16

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Variable Problem

ErnieParke wrote:

BoltBait wrote:

You should edit the scratch wiki and put more examples in there.  I would do it, but I can't remember my password.  sad

I would like to do that, but I don't have an account on the Scratch Wiki, which makes me wonder... I found out how.

Found my password and added the math blocks to the wiki.


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#15 2012-10-30 17:59:50

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Variable Problem

I put it in and Scimonster removed it again saying "why should math blocks get a section to themselves? They shouldn't."

Obviously, I disagree.

But, he's a mod and I'm not.  So, I guess, I lose.  sad


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

Board footer