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

#1 2007-06-01 07:01:13

PeterB
Scratcher
Registered: 2007-05-18
Posts: 14

Bug! Variables not displaying correctly in switch to costume

Please see the sample code http://scratch.mit.edu/projects/PeterB/9034
the variable is being displayed 1 less then the actual value.

Offline

 

#2 2007-06-01 08:17:14

Duncan
Scratcher
Registered: 2007-05-15
Posts: 47

Re: Bug! Variables not displaying correctly in switch to costume

See what you mean but I guess the costumes have names rather than a value.  If you renamed them with words then Costume 1 = Zero, Costume 2 = One, Costume 3 = Two etc. etc.

Then the "bug" vanishes  wink

Offline

 

#3 2007-06-01 08:51:55

PeterB
Scratcher
Registered: 2007-05-18
Posts: 14

Re: Bug! Variables not displaying correctly in switch to costume

Stranger and stranger

Thanks, having tried that it still did not work. But that did lead me to dabbling a bit more and finding a possible reason.

The sprite costumes list appears to be referenced in number order with variable/costume number 1 being at the top and so on,  so if I remove the zero costume from the top of the list they all display okay.

But then if I import a new zero and this then sits in costume list position 10 but the variable value is 0 then it gets displayed.

Stanger still, if I now import another zero in position 11 (which gets automatically named 02) and set the random to 0-11 the zero costume in position 11 is now referenced/displayed whenever the variable is either 0 or 11.

I suspect the only answer is to ensure that for this type of coding, that the zero sprite costume is the last sprite costume in the list.

~~~~~~~  Edit text follows below  ~~~~~~~

Just tried a further proof and the following rule applies:

If the number of costumes is less the then number of the variable then the remainder references from the top of the list again.

For example if you have 4 costumes in your list and the variable is 7 then the sprite costume returned will be the third costume in the list.

For much bigger numbers continuious subdivision of the number of costumes in the list is applied until the remainder is satisfied. So rather then stop the program and report a error or stop because no costume is available,  it will instead continue to run having displayed an incorrect costume, incorrect that is in the view of the programmer unless of course you are aware of this feature.

Now to go and turn over some more rocks...

Last edited by PeterB (2007-06-01 09:15:13)

Offline

 

#4 2007-06-01 09:05:56

weissjd
Scratcher
Registered: 2007-05-16
Posts: 64

Re: Bug! Variables not displaying correctly in switch to costume

Peter,

I ran into this before. I think the key is that the first cosutme is one, there is no zero. If you use zero, it references the last costume (my guess is that it's circular, -1 probably gets you the second to last costume, number of costumes + 1 probably gets you the first). If your costumes actually represent numbers and you want the one that corresponds to the value of a variable, I would put the zero costume first for clarity and then do set costume to myvar + 1.

Offline

 

#5 2007-06-01 09:19:14

PeterB
Scratcher
Registered: 2007-05-18
Posts: 14

Re: Bug! Variables not displaying correctly in switch to costume

wiessjd,

please see my edit above, your right the costumes are managed as a list beginning at 1.

If I thought about it its obvious really how else would you operate a next costume command.

Offline

 

#6 2007-06-01 13:50:06

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

Re: Bug! Variables not displaying correctly in switch to costume

The costume numbers start at 1, and setting a costume to a variable really sets it to
( round(x-1) mod num_costumes) +1
So integers 1..num_costumes do the obvious thing, but 0 maps to the last costume.

Offline

 

Board footer