How to define random frames for buttons without repeating?

0 favourites
  • 11 posts
From the Asset Store
Selection frame like in RTS games, works both on mobile and desktop devices.
  • I have 5 button instances with 20 frames containing icons. One of the icons is the correct one and I already know which frame it is, but I would like the other buttons to show different icons (different frames) with the exception of the correct one and not repeat themselves.

    I thought about first distributing the different frames to each button, without including the correct frame, and then selecting one of them to show the correct frame. This second part I know how to do. Does anyone have any idea how I can distribute the frames to the buttons without repeating and leaving out the correct frame?

    PS: I am already using Advanced Random for another part of the script

  • Create an AdvancedRandom permutation table. For each button set frame to AdvancedRandom.Permutation(loopindex)

    If you need to exclude the correct frame from the table, use a variable instead of loopindex. And skip one entry in the table if it equals the correct frame.

    For Each Button
    .. If AdvancedRandom.Permutation(i)=correct : Add 1 to i
    
    .. Button set frame to AdvancedRandom.Permutation(i)
    .. Add 1 to i
    
  • You can also do this without the AdvancedRandom. Add 20 button instances with frames from 0 to 19. Pick the correct one. Pick 4 other random instances, destroy the rest.

  • Hi dop2000

    Thanks for replying.

    Isn't there a way to sort the frames for each button so that they are not repeated, with the exception of the correct frame, without using advancedrandom and without using so many existences? The buttons actually have 3 different animations, each with 20 frames.

  • There are other options:

    You can use a While loop - generate a random frame number, check if there is a button already with this frame. And either repeat or stop the loop.

    Or put all numbers into an array, pick random index and then delete it.

    Or use a string variable with the list of frames - pick random token and delete it.

    But permutation table is easier than any of those.

  • You're right, advancerandom is much simpler, however, as I said, I'm already using it to display images of the exercises on the screen. Can't a conflict occur if I also use it for the buttons, since there is only one advancerandom object available?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you don't need to keep another permutation table in AdvancedRandom active, then there shouldn't be any conflicts. You can re-generate permutation tables as often as you like.

  • So a possible solution would be for me to store the result of the first advancerandom in an array and then use it again for the buttons. Thanks.

  • Just one question: how do I remove the correct frame number when generating permutation. For example: I have 20 frames and the correct frame is the 5, how to make it not to be part of it?

  • You skip to the next entry in the table, see my first comment.

  • Thanks

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