How do I switch color of an object but not repeat!

Get help using Construct 2

Post » Mon Feb 01, 2016 12:10 am

Hi, I'm working on a project where three boxes will keep entering the viewport and each is colored differently. I want to use only three colors e.g Blue, Red, Green. The problem is I want to use the "Create Object" event to keep generating the three box sprites in those three colors but I don't want the color to repeat.

If i use the random function to choose one of the three colors, there's a possibility that two boxes might end up with the same color. I want to know how to make it possible for my event system not to repeat colors in newly created three boxes. I'm new to this so I don't really have a good command on loops etc.

Your help will be highly appreciated! It would be such a blessing if someone can quickly make an example file for me.
B
11
Posts: 27
Reputation: 448

Post » Mon Feb 01, 2016 10:37 pm

BUMP!
B
11
Posts: 27
Reputation: 448

Post » Mon Feb 01, 2016 11:27 pm

If you search for "random without repeat" the question has come up before a few times, and I think a plugin has been made to help with this.

The basic idea is to keep track of the last color of the created object and if the the random color is equal to that make the color be picked again. So like this if you have a separate object per color:
Code: Select all
global text lastColor=""
global text rndColor=""

+-------------------------------+
| every 1.0 seconds             |
+-------------------------------+
   +---------------------------+
   | while                     | set rndColor to choose("blue","red","green")
   +---------------------------+
      +-----------------------+
      | lastColor <> rndColor | stop loop
      +-----------------------+
   +---------------------------+
   | rndColor="blue"           | create blueSprite at (0, 0)
   +---------------------------+
   +---------------------------+
   | rndColor="red"            | create redSprite at (0, 0)
   +---------------------------+
   +---------------------------+
   | rndColor="green"          | create greenSprite at (0, 0)
   +---------------------------+
   +---------------------------+
   |                           | set lastColor to rndColor
   +---------------------------+
B
89
S
30
G
96
Posts: 5,160
Reputation: 63,956

Post » Mon Feb 01, 2016 11:28 pm

Something like this should work..

Image

Maybe you'd have to pick by animationframe and compare pickedcount, but the basics are the same..

Ninjaa'd by @R0j0hound
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Feb 02, 2016 12:06 am

Doubly-ninja'd, but I just did a similar example over here: viewtopic.php?f=147&t=167888&p=1011152#p1011152
ImageImageImage
B
71
S
21
G
204
Posts: 3,586
Reputation: 115,576

Post » Tue Feb 02, 2016 3:14 am

You can use arrays too, although that might be a little advanced.

Get a random array value based on the width, delete that column, repeat.

I like to use this for card games mechanics like shuffling and drawing.
Mistakes were made.
B
47
S
23
G
107
Posts: 1,571
Reputation: 60,133

Post » Tue Feb 02, 2016 11:55 pm

Thank you so much everyone! It's all clear to me now and I have achieved what I was trying to achieve! Thank you for being such a great helpful community!
B
11
Posts: 27
Reputation: 448


Return to How do I....?

Who is online

Users browsing this forum: pynam and 6 guests