- Home /
How to do the matches in a memory card game?
Hi everyone! I'm making a memory card game. I made 2 planes and parenting them together so they make a single card,i made another one too. Then, i added a common back side texture from gui for both cards. Every card on Start() gets randomly its front texture like below (javascript)(on StartUp.js):
public var oPlane: GameObject;
function Start () {
var texturesCollection: Object[] = Resources.LoadAll("Front_Face_cards");
var range_1 = Random.Range(0,texturesCollection.length);
if(oPlane.Find("front_card_1") && oPlane.Find("front_card_2")){
oPlane.Find("front_card_1").renderer.material.mainTexture = texturesCollection[range_1];
oPlane.Find("front_card_2").renderer.material.mainTexture = texturesCollection[range_1];
}
}
Now, on each plane i attached this code on (Flip_card.js):
public var card_1: GameObject;
card_1 = GameObject.Find("card_1");
function OnMouseDown () {
card_1.transform.localRotation.z = 180;
CheckCards();
}
So that, every time i clicked on a card this card can flip (the camera shows from above). Later on, i check for any similarities with those 2 cards by calling CheckCards(), which is below in detail (Flip_card.js continue):
function CheckCards(){
if((card_1.transform.localRotation.z == 180) && (card_2.transform.localRotation.z == 180 )){
if(card_2.renderer.material.mainTexture == card_1.renderer.material.mainTexture){
Destroy(card_1);
Destroy(card_2);
}
}
Unfortunately, it's not working. i can't destroy my two cards after i flip them both. I stuck there, i checked most of the answers close to my problem here but no luck. Where do i check if my cards are the same and then destroy them? On the script attached to one or both cards, or on Update(), or on Start()? Thanx in advance!
(UPDATED)
Answer by Nition · Nov 16, 2013 at 07:09 PM
I just tested this and your texture comparison code definitely works, so something else is wrong somewhere. I'd suggest removing the localRotation check temporarily and seeing if it works with just the texture comparison, if you haven't done that already.
If the texture comparison is the broken part, is it possible that your material has multiple textures, or that you're not checking the correct renderer?
if i remove the localRotation check what it happens is: when i click on one of two similar cards they disappear (which it works in a way) but i can't see the flipping of the card though. i click on the one and they both get the destroy effect without seeing the flip effect ( localRotation.z =180) or click on the other card as well to see if they are both the same. thanks anyway. maybe i have to put my check on somewhere else, like on the Update()?
With the current code, you'll never see them flip because the rotation and the check happen on the same frame. There are multiple ways you could solve it but a simple one is set the rotation when you click like you do now, but call the CheckCards method with Invoke: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
That way you can delay the check until the card has shown for a second, or whatever.
That's it. At least it works as i want it for now. I was trying to find a similar statement, like in C#, yield return new WaitForSeconds(5); for js. Thanx for the help!
That's a coroutine. You CAN do it with a coroutine in JavaScript as well, but in this case an Invoke is easier. You were on the right track though.
Answer by tw1st3d · Nov 16, 2013 at 07:42 PM
So, I kind of forgot what I was doing while writing this, so it's probably incomplete. This is a C# solution for handling and comparing cards. Might be a little over-doing it, but it's not throwing any errors (in the inspector, haven't bothered testing it in a scene.)
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class CardHandler : MonoBehaviour
{
private int maxCards = 0; // Define yourself.
private List<GameObject> cards = new List<GameObject>(); // Leave this
private List<Card> cardHandlers = new List<Card>(); // Leave this
private GameObject card1, card2; // Leave these
private void Start()
{
generateCards();
}
private void generateCards()
{
float x = 0, y = 0, z = 0; // Define these based on table layout.
float cardWidth = 0, cardHeight = 0; // Define these based on your models size.
for(int i = 0; i < maxCards; i++)
{
Card newCard = new Card();
cards.Add(freshCard(newCard, x, y, z));
x += cardWidth + 20;
y += cardHeight + 20;
}
}
private GameObject freshCard(Card C, float x, float y, float z)
{
System.Random rand = new System.Random();
GameObject newCard = Instantiate(C.card, new Vector3(x,y,z), C.card.transform.rotation) as GameObject;
int cID = rand.Next(0, C.materials.Length);
Material toSetMat = C.materials[cID];
newCard.renderer.material = toSetMat;
return newCard;
}
public void Update()
{
/* Your Click Logic */
GameObject clickedObject = null;
if(clickedObject)
{
// If you've clicked a card
foreach(Card k in cardHandlers)
{
// iterate through cards
if(clickedObject == k.card)
{
// If the current card is the one you clicked
if(k.isFlipped)
{
// If it's already flipped, ignore the click
continue;
} else {
// Else flip it
k.isFlipped = true;
if(card1)
{
// If this is not the first card flipped
if(card2)
{
// If this is not the second card flipped, ignore click
continue;
} else {
// Else set card two to clicked card
card2 = k.card;
if(card1 == card2)
{
/* Your logic on correct match */
} else {
/* Your logic on incorrect match */
}
}
} else {
// Else set card one to clicked card
card1 = k.card;
}
}
} else {
// Ignore all other cards
continue;
}
}
}
}
}
public class Card : Component
{
public GameObject card; // Define with your default card model. No material.
public Material[] materials; // Define with your material list.
private int cardID; // Defines themselves.
public bool isFlipped = false; // Leave this
}
Though it's C#, it's a good example for what i want, but in case of 20 cards (which my next levels would be with 20 cards for example) it would be more complicated as for the if-else statements for each card.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Not sure if you can do this.. 2 Answers
UnityScript, Delegates and SmoothMoves 1 Answer
'text' is not a member of 'OBJECT' JavaScript Not Working in Unity 3.5 - Lost Commands? 2 Answers