(newbie question) Trying to set multiple instances of the same actor to a random color
wirelesstkd
Member Posts: 75
Hi all, I'm just experimenting here and I can't figure out if there's a better way to do something.
At the start of my scene, I want to have multiple instances of a simple square actor, but with each one set to a random color (Red, Blue, Yellow or Green). I brought four instances of the square in to the scene and I set rules in the square's prototype to pick a random value for game.RandomColor, between 1 and 4, which then change square.color to be the corresponding color (set in the rules).
What happens (I thought it might) is all instances of the square share the same color. I guess it's only picking one random number and applying the condition to all instances (I was hoping that each instance we run it's full set of rules, allowing game.RandomColor to change value for each one).
I guess an easy way to fix this is to create something like game.RandomColor1, game.RandomColor2, etc. However, my goal is to eventually build something with lots of actors, each choosing a random color, so this method might have me creating 20 or so variables. It's doable, but I was wondering if there's a more optimal way to go about it.
At the start of my scene, I want to have multiple instances of a simple square actor, but with each one set to a random color (Red, Blue, Yellow or Green). I brought four instances of the square in to the scene and I set rules in the square's prototype to pick a random value for game.RandomColor, between 1 and 4, which then change square.color to be the corresponding color (set in the rules).
What happens (I thought it might) is all instances of the square share the same color. I guess it's only picking one random number and applying the condition to all instances (I was hoping that each instance we run it's full set of rules, allowing game.RandomColor to change value for each one).
I guess an easy way to fix this is to create something like game.RandomColor1, game.RandomColor2, etc. However, my goal is to eventually build something with lots of actors, each choosing a random color, so this method might have me creating 20 or so variables. It's doable, but I was wondering if there's a more optimal way to go about it.
Comments
What you need are self attributes. Just add self.RandomColor (index/integer) to the actor's prototype and then substitute that for game.RandomColor in the rules. That way, each instance will generate its own unique value and refer to that instead.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User