When i create this 3 levels with 6, 10 and 20 cards and when create last 10 pairs how level 1 and 2 will be working if game dont should have too many cards?
My example simply shows how you can make it fairly simple using a fixed amount of cards. If you need more cards or more flexibility you will have to adjust the code for that. Its only meant as a starting point or to give you some ideas.
If you need different amount of cards for each level you have to change the code so it can do that. Because you are right the current code doesn't work if the amount of cards changes. The reason for that is:
Event 4: Here we add all cards in the card object to the list 2 times. So we can assign them to the cards that we place on the table in Event 6 and 7.
Event 6-7: But here is the problem as we create the cards we also assign them a random card from the list. But if we only need 10 cards to be on the table but have added 50 different cards to the list for it to choose from. The chance is that we will end up with none matching cards.
So to fix it, we have to be sure that only the needed amount of cards are available in the list and that we don't add more cards to the table than we have cards for.
To make it easier to work with I add a function that will handle that for us.
Its always a good idea, I think whenever you create a function to write with comment what the function need to work.
So at the top you can see, it requires:
"Needed cards" - This is the total number of cards needed. So 10 for instant means we have 5 cards with 2 of each type, so they match.
"Cards_in_X" - How many cards should be in the horizontal direction. So 5 means you will have 5 cards in a line.
"Cards_in_Y" - How many cards in the vertical direction, so we could make that 2 so you would have 5 cards in each line and 2 rows. Which equal 10 cards.
Event 18: Since we added all cards in our game to the list in Event 4. We might have to many cards in the list for what we actually need. So the first thing we need to do, is to make sure that we only have the correct amount of cards to choose from. So we check whether the "Itemcount" in the list is greater than what we need. If that's the case we need to remove some cards from it. So we choose a random card from the list and store it in the variable "Card_to_remove". The reason to do this is because we added cards twice, so we also need to remove two entries from the list.
Event 19: And we do that here by going through the list and remove all the cards from it which match this "Card_to_remove".
I have a total of 10 different cards in the game. So if I use all of them, the list would look like this:
Imagine that I only wanted to use 6, then the list could look like this:
So now we can remove cards from the list and only end up with the ones we need.
Event 20 - 21: These are the same as in the original, except that we use the "Cards_in_X" and "Cards_in_Y" to decide how we want the cards to be arranged.
So we use the parameters (1) and (2) which we use when calling the functions in the loops to decide that. The reason I subtract one, is simply to avoid having to think about the 0 index in C2, when calling the function.
Event 6: Now that the function is made we don't really have to think about it anymore, all we have to do is to make sure that we call it with the correct values. In this case we want 10 cards, 5 in each line and 2 rows.
So as long as you don't go higher than what you have cards for in the game and you make sure that when calling the function this is true:
[Needed cards = Cards_in_X * Cards_in_Y]
Then it will work.
CAPX - https://dl.dropboxusercontent.com/u/109921357/Simple_match%20game%20tut%20files/Simple_match_game.capx