Memory card game issue...

Get help using Construct 2

Post » Thu Jun 14, 2012 4:30 pm

I'm creating a memory card game and I can't really solve how to do the following:
I have 20 objects and they all have ID Variables set to 0 as default. I would need these objects to randomly pick a value between 1-10 and make sure that there's always two objects with the same ID.
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Thu Jun 14, 2012 5:49 pm

I'm no expert at Construct but I would have done something along the line of this (it picks cards at random instead of assigning a random value and is flexible since you can place any equal number of cards in the layout, not only 20):

#Global values, ho!
Global number cardIDs = 1
Global number cardCount = 0


#Count the number of cards and store in global value
On start of layout
- set cardCount to number of cards in the layout (card.Count)

#Assign the IDs to cards that don't have one (ID = 0) at random
cardCount > 0
card ID = 0
Pick a random card instance

- Set (card) ID to cardIDs
- Subtract 1 from cardCount
- Add 1 to cardIDs

#This resets the ID enumerator halfway through assigning
cardIDs = (card.Count/2)+1
- Set cardIDs to 1McKack2012-06-14 17:51:16
B
88
S
33
G
7
Posts: 31
Reputation: 8,425

Post » Thu Jun 14, 2012 6:37 pm

@McKack Thanks. I tried to figure out your method, but I didn't get it.

Instead of cards, I have these holes where you can pull out vegetables and try to find pairs for them. These holes have an ID to tell which vegetable to create.nemo2012-06-17 11:32:17
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Thu Jun 14, 2012 7:53 pm

Perfect opportunity to plug my Memory match video tutorial series. You'll find a link to the completed project there if you just want to look at a working example. And you can try it out on the Arcade under Example games.

Instead of cards you'll use your "holes" for the card back animation and your veggies for the card face animation. Cute!

Edit: In the globals event sheet you should see where you can change the number of rows and columns and total number of "cards".

kittiewan2012-06-14 20:02:57
B
29
S
11
G
12
Posts: 281
Reputation: 11,878

Post » Thu Jun 14, 2012 11:34 pm

I would do something along those lines :
you have the 'Card' sprites with your different symbol in each animation frame.
You add an instance variable called 'set' of type boolean and default to false

You just have to do a loop like:
[code]+System: for "card" from 0 to Card.animationFrameCount-1
+System repeat 2 times
+Card: [invert] is 'Set'
+System: pick random Card
      -> Card: set animation frame to loopindex("card")
      -> Card: set 'Set' to True[/code]
Then to know if you picked two matching cards you just have to compare their animation frame (:Yann2012-06-14 23:36:13
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Jun 15, 2012 12:24 am

@Yann Yes, much more efficient than mine :) I had to mix up the system events slightly to get it to work properly though. I made an example out of it (holes.capx):
Example

R restarts the layoutMcKack2012-06-15 00:26:08
B
88
S
33
G
7
Posts: 31
Reputation: 8,425

Post » Fri Jun 15, 2012 12:48 am

I managed to make it work. I went with a really simple way. I had 20 holes.
Created a global value: HoleIDs=-1
Then I set for each order by Random(Hole.UID)ascending.
Add HoleIDs to Hole.ID

And if ID bigger of equal to 10,
then substract 10 from it.

I would like to thank you all for your help and for activating my brains. I think all this shoul be good reference for anyone creating memory games.nemo2012-06-15 00:50:19
B
40
S
5
G
5
Posts: 405
Reputation: 5,657

Post » Fri Jun 15, 2012 11:56 am

@McKack
This would be slightly better
memCard.capx
Using the 'on start of layout' you avoid the glitch you have on start when you see the text appearing and also you avoid running your loops all the time.
Last edited by Yann on Sun Sep 14, 2014 9:11 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Jun 15, 2012 1:20 pm

Hm, weird, I didn't see any glitch on my side. Even though the text was just for informational purposes in the example, thanks for the tip.
B
88
S
33
G
7
Posts: 31
Reputation: 8,425


Return to How do I....?

Who is online

Users browsing this forum: flemmig, Matthew de, semaso, Skebab, Virpoja and 16 guests