Memory card game issue...

0 favourites
  • 9 posts
From the Asset Store
Template for trading card game, fully documented in comments and video
  • 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.

  • 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 1
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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".

  • 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:

    +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

    Then to know if you picked two matching cards you just have to compare their animation frame (:

  • 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 layout

  • 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.

  • 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.

  • 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.

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