How do I maek four sprites of the same object have random frames and all different between them?

Not favoritedFavorited Favorited 0 favourites
  • 13 posts
From the Asset Store
********* Suv four by four game sprite **************
  • Ive tried many ways but I just cant do it, I want four blocks of the same sprite to set a different frames of the same animation without having the same.

    pd: Im desperate, Ive been trying to do it the whole week by myself but just cant get it right xD

  • I’m becoming more and more convinced that construct isn’t as simple to use as it appears.

    There are many solutions to that, each with their own set of things to be familiar with.

    The first approach I’d use is how would you do that in real life? What would the steps be?

    Probably: roll 4 dice, then look for two or more dice that are the same, and reroll the duplicates and check again. So roll, and reroll if there are duplicates.

    However picking seems to be hard to most, so is picking two separate instances at the same time.

    So you could put all the possibilities values in an array, shuffle that, and use that to set frames.

    Start of layout

    — array: set size to (sprite.frame count,1,1)

    Start of layout

    Repeat array.width times

    — array: set at loopindex to loopindex

    Start of layout

    — array: shuffle

    Start of layout

    For each sprite

    — sprite: set frame to array.at(loopindex)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Wow thanks fpr the answer, sadly I´m using construct 2 and the shuffle function doesn´t appear, that´s why I have´nt done that aproach, could there be another way?

  • I don't think you need an array here. If there are only 4 frames, then it's very easy:

    For each Sprite order by random(1) -> Sprite set frame to loopindex
    

    If there are more than 4 frames in the animation, you can use the Advanced Random object.

    AdvancedRandom create permutation table with size Sprite.animationFrameCount
    Sprite set frame to AdvancedRandom.permutation(self.IID)
    
  • I have Construct 2, and have more than 4 franes son I cant do it as you say

    Can someone find another way?

  • In c2 you can do shuffle by having another y in the array with a random value and use sort. Here is the solution I posted above modified to do that.

     Start of layout
    — array: set size to (sprite.frame count,2,1)
    
    Start of layout
    Repeat array.width times
    — array: set at (loopindex,0) to random(1)
    — array: set at (loopindex,1) to loopindex
    
    Start of layout
    — array: sort x
    
    Start of layout
    For each sprite
    — sprite: set frame to array.at(loopindex,1)

    Now as an exercise you probably can reduce that to less events. One to populate a shuffled list of numbers, and one to set animation frames. You could use a text variable instead of the array if you wanted too.

  • You can also try the self-calling function method, where you set up a function that will go through a random instance that hasn't been randomized, set its frame and move to the next instance

    Example capx :

    drive.google.com/file/d/1bS84qMrTWlIFO0Tqq3V5YLDGuLTvPWtS/view

  • Thanks to both!!!! I´ve made it work, I would´ve never imagined such a simple task would take me to such a mess xD

  • To be fair instance picking can be tricky in C2 and C3 so it's worth the head-scratching;

    Btw I over-complicated it, you don't need a function or an array to achieve the shuffle, try this :

  • Btw I over-complicated it, you don't need a function or an array to achieve the shuffle, try this :

    With your code, if there are four instances of the sprite, they will receive frames 0–3. As I understand it, there are more than four frames in the animation, and OP wants to assign random frames without repeats.

    Yeah, there are many ways to do this, even in C2, but none of them are very straightforward.

  • Frames 1-4* as I used loopindex+1, but yes this one is made with the idea that there's as much frames that there's instances;

    For a pool based on the total number of frames (still excluding frame 0), this does the job :

  • Say, if there are 20 frames in the animation, I would put 20 sprite instances on the layout - 4 in their places and 16 off-screen. Then assign them random frames:

    For each sprite order by random(1) : Set frame to loopindex

    Then destroy off-screen sprites. This is the shortest and easiest solution.

  • More ideas.

    This one starts with a list of all the numbers and removes random ones one by one. For example 20 possibilities.

    Var list=“”
    Var n=0
    
    Start of layout
    Repeat 20 times
    — add zeropad(loopindex,2) to list
    
    Start of layout
    For each sprite
    — set n to int(random(len(list))/2)*2
    — sprite: set frame to mid(list, n, 2)
    — set list to left(list, n) & right(len(list)-2-n)

    This one just randomizes till all four numbers are different.

    Var done = 1
    Start of layout
    While
    Compare: done>0
    — sprite: set animation frame to int(random(sprite.animationFrameCount))
    — set done to 0
    — add sprite(0).animationFrame = sprite(1).animationFrame to done
    — add sprite(0).animationFrame = sprite(2).animationFrame to done
    — add sprite(0).animationFrame = sprite(3).animationFrame to done
    — add sprite(1).animationFrame = sprite(2).animationFrame to done
    — add sprite(1).animationFrame = sprite(3).animationFrame to done
    — add sprite(2).animationFrame = sprite(3).animationFrame to done

    Same as above in a cleaner way.

    Var r0=0
    Var r1=0
    Var r2=0
    Var r3=0
    
    Start of layout
    while
    compare: (r0=r1 | r0=r2 | r0=r3 | r1=r2 | r1=r3 | r2=r3) = 1
    — sprite: set frame to int(random(sprite.animationFrameCount))
    — set r0 to sprite(0).animationFrame
    — set r1 to sprite(1).animationFrame
    — set r2 to sprite(2).animationFrame
    — set r3 to sprite(3).animationFrame

    This one uses a tuned pseudo random number generator to generate random numbers without duplicates. This one was tuned to handle 10 possible frames, but it would work for any count by changing the right numbers.

    Var r=0
    
    Start of layout
    — set r to int(random(1000))
    — for each sprite
    — — set r to (r*906+1866)%1000
    — — sprite: set frame to r%10

    I’m sure there are other ways. Each have pros and cons.

    Edit:

    One more for good measure using an array.

    Start of layout
    -- array: set size to (0,1,1)
    -- for each sprite
    -- -- sprite: set frame to int(random(self.animationFrameCount-loopindex))
    -- -- sprite: set frame to (loopindex>0 & Array.at(0)<=self.AnimationFrame)+self.AnimationFrame
    -- -- sprite: set frame to (loopindex>1 & Array.at(1)<=self.AnimationFrame)+self.AnimationFrame
    -- -- sprite: set frame to (loopindex>2 & Array.at(2)<=self.AnimationFrame)+self.AnimationFrame
    -- -- array: push sprite.animationFrame
    -- -- array: sort x axis
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)