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

  • 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.

  • BUMP!

  • 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:

    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
    	+---------------------------+[/code:1yta3k2o]
  • Something like this should work..

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

    Ninjaa'd by R0J0hound

  • Doubly-ninja'd, but I just did a similar example over here:

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)