Timothy Lawman
Timothy Lawman

Reputation: 2312

Variables in cloned Scratch sprites - global or local?

I am creating a falling snow background in Scratch and stumbled across the following code:

enter image description here

I then read the wiki on cloning sprites and was stumped at this sentence:

Variables for all sprites will be the same for each clone, but variables for this sprite only will be different for each clone.

Question:

In my when clicked block I create a clone of myself. Both the clone and the when clicked block share the variable y pos but they seem to be acting independently (thus in oops terms) a variable of type 'this' ie a separate variable instantiated only for the clone object. I cannot display it with a tick so I am assuming it is local to the clone?

Or is y pos global (a class variable)? I am guessing the latter but the sentence above is not helping.

Upvotes: 1

Views: 4681

Answers (3)

Peter B.
Peter B.

Reputation: 391

A local variable in Scratch is also a seperate instance in each clone. A global variable only exists once and is the same for all objects. Scratch has no class variables, which would be shared among clones of the same object.

Upvotes: 2

GreenHawk1220
GreenHawk1220

Reputation: 121

The variable accessibility will vary depending on how you create it. When you create the variable in the variable menu (ie where you choose the name) it will ask you if you want the variable accessible by "all sprites" or "just this one". If you choose "all sprites," the variable will be created global, and will be accessible (and changeable) by all sprites. If you select "just this one," it should be created a local variable, and only be accessible by each sprite individually.


Hope I helped. -GreenHawk1220

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

In order for this to work properly, the variable ypos must be local (for this Sprite only), otherwise the two would interfere with each other.

Upvotes: 1

Related Questions