Time for action - ID the cards

Let's revisit our card-creation code and give each card an ID number. We'll use that number to detect matches.

  1. In the BuildDeck function, add this line:
    function BuildDeck()
    {
    var totalRobots:int = 4; // we've got four robots to work with
    var card:Object; // this stores a reference to a card
    var id:int = 0;
    
  2. Look a little further down the code. Pass the id value to the Card class with each robot and missing body part card, and then increment the ID number:
    card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
    aCards.Add(card);
    card= new Card("robot" + (i+1) + theMissingPart,id);
    aCards.Add(card);
    id++;
    
  3. Add id as a property of the Card class. Accept id as an argument in the Card class constructor function, ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.