I know that similar questions have been asked before, but I’ve been looking for several hours and tried using arrays, and I still can’t figure out how to solve my problem. I am making a card game in which a random card animation is generated when a button is pressed. I need it to generate a random number (0-51) without repeating. How would I do this?
Add the Array Tools extension to your game. Create an array of the 51 number. Shuffle the array, and then either use the Shift array child with index 0, or pop array child.
So populate it this way:
and get the next animation with a pop:
or with a shift:
Thank you, and what would I write for the card animation? Right now, I have something along the lines of “change the number of the animation of PlayingCard: set to ________” What would I put in the blank?
You’d use the variable “SelectedAnimation
”, or whatever variable you use to store the animation number pulled from the array.
Here is my code, it didnt work, it just generates the first animation every time. What did I do wrong? (sorry for the weird code formatting, i was using the mobile app)
That “Remove first child
” is in the beginning of scene, so only gets run once. Put it as a subevent of the “Green Button is clicked
” event.
Are the cards images all separate animations, or do the cards make up all the frames of one animation?
If it’s the latter, set the current frame of the animation to SelectedAnimation:
How do you know a new card get created? Is the space where it’s generated blank and then the new playingCard object placed in the scene? If not, is the value of HitButton = 1? If it is, then it’ll be an action elsewhere that’s interfering or changing the card animation.
Here is the whole code for when a card gets generated, maybe you can figure the problem. Is the shuffle array also supposed to be repeated 52 times?
That helps a lot, and I understand what you’re doing.
You need to use the “Remove the First child...
” action before you set the animation of each card.
As it stands, you set SelectedAnimation once and then use it multiple times. You need to set it every time you use it if you want a different value with every usage.
You need to do it for all the events where a new card is created.
So that’s all of these events:
And these:
Ok. Do you have a scene variable named SelectedAnimation? If so, delete it.
I only have a global variable named SelectedAnimation.
I can’t see anything that jumps out from the screen shots. Any chance you zip up your game and make it available on a shared drive for download so I can see what’s going on? PM me the link if you like.
I messaged you the link to my game.
Got it. I’ll download and check when I’m not at work.