Search Unity

Problem by selecting a Card in My Card Game

Discussion in '2D' started by mooncake888, Apr 16, 2017.

  1. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    Hello Friends in Unity Forum!
    I am developing a Card Game with Unity, but i have some problem by selecting a card from a card stack:
    At the beginning of the Card Game, the player gets some card from the dealer,e.g. Cards A,B,C,D....the player select one of them, for example,card A, then cards B,C,D,will dispeare with a fade out animation.

    PS: All the cards are prefabs.

    this is my code to do this
    Code (CSharp):
    1. void Update () {
    2.         if(isDone)
    3.         {
    4.            
    5.             fadeAnimation ();
    6.         }
    7.  
    8.     }
    9.  
    10. void OnMouseDown(){
    11.  
    12.         isDone = true;
    13.  
    14.     }
    15.  
    16. void fadeAnimation(){
    17.         timer += Time.deltaTime;
    18.  
    19.         if (timer >= lifetime) {
    20.             //Transparency.
    21.             fade = GetComponent<SpriteRenderer> ().material.color;
    22.             fade.a = fade.a /1.1f;
    23.             GetComponent<SpriteRenderer>().material.color = fade;
    24.        
    25.             if(fade.a <=.1){
    26.  
    27.                 Destroy (gameObject);
    28.            
    29.             }
    30.     }
    31. }
    with that code the card which the play clicks will dispear, not the cards which the player dosent click....how can i solve the problem,thanks alot!:)
     
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    This script is on the cards itself, right? So when you click on a card it gets the GameObject's SpriteRenderer that you clicked on and fades that. You need a way to reference all cards on the screen, so you can tell every card but the clicked on one to fade.

    I'd use an additional CardManager script with a list of GameObjects for this and put all the cards into that when they are shown to the player:
    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. List<GameObject> cards = new List<GameObject>();
    4. cards.Add(card1);
    5. cards.Add(card2);
    6. //and so on
    Then, when a card is clicked, it calls a method in the CardManager that fades all the other ones away. For example you call that 'FadeCards(GameObject chosenCard)' and then go through the list with a foreach loop while excluding the chosenCard.

    Code (CSharp):
    1. foreach (GameObject card in cards)
    2. {
    3.      if (card != chosenCard)
    4.           card.fadeAnimation();
    5. }
     
  3. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    thanks for the advise! i will try it.:)
     
  4. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    Hi Zephus! i found some problems after i try with your suggestion:my function fadeAnimation() is called by update().because it has a timer, which is changed in update() function. Do u have some idea to make fade Animation without being called by update() function? thanks alot!:)
     
  5. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    I'm not sure what the problem is. You can still call it in Update(). You're just separating the logic which cards to fade from the cards themselves and put them in a general manager script.
    So the manager handles the timer and all the cards do is 'fade themselves' when the manager tells them to.
     
  6. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    Althought the logic is seperatet, the function is still called by update() in CardManager, and as soon as the CardManager is loaded, update() will be automatically called.
    i mean, how can the manager the timer handle without calling its update()?thx!:)
     
  7. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    Hi Zephus, i have try it again. In order not to use Update() i decide to use thread, the code looks like follow:
    Code (CSharp):
    1. void OnMouseDown(){
    2.        
    3.  
    4.         gamecontroller.SetUpBattle(cardmodel.cardIndex);
    5.         cardmodel.isSelected = true;
    6.         gamecontroller.manageFade (fetchedCards);
    7.  
    8.  
    9.     }
    10.  
    11.  
    12. // FadeAnimation start
    13.     public void startFade() {
    14.  
    15.  
    16.         StartCoroutine(fadeAnimation());
    17.  
    18.  
    19.     }
    20.  
    21.     private IEnumerator fadeAnimation()
    22.     {
    23.        
    24.         timer += Time.deltaTime;
    25.  
    26.         if (timer >= lifetime)
    27.         {
    28.             //Transparency.
    29.             fade = GetComponent<SpriteRenderer>().material.color;
    30.             fade.a = fade.a / 1.1f;
    31.             GetComponent<SpriteRenderer>().material.color = fade;
    32.             print ("fada!");
    33.            
    34.             if (fade.a <= .1)
    35.             {
    36.                 //Destroy (transform.parent.gameObject);
    37.                 Destroy(gameObject);
    38.                 yield return null;
    39.                
    40.             }
    41.            
    42.         }
    43.         yield return null;
    44.         throw new System.NotImplementedException();
    45.     }
    46.  
    47. //Manager Class
    48. public class Manage :MonoBehaviour
    49. {
    50. public void manageFade(Dictionary<int ,GameObject> fetchedCards){
    51.  
    52.         CardModel cm;
    53.         foreach(KeyValuePair <int, GameObject> cards in fetchedCards)
    54.         {
    55.             cm = cards.Value.GetComponent<CardModel>();
    56.  
    57.             if (cm.isSelected){
    58.                
    59.                 cards.Value.GetComponent<HarzardCardClick>().startFade();
    60.  
    61.             }
    62.  
    63.         }
    64.     }
    65. }
    but as it runs, there is an exception:
    NotImplementedException: The requested feature is not implemented.
    HarzardCardClick+<fadeAnimation>c__Iterator0.MoveNext () (at Assets/Script/HarzardCardClick.cs:92)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    Do u know how to fix it? Thx!;)
     
  8. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    Take out line 44? Did you add that manually? 'throw' is a C# keyword and will throw an exception.
     
  9. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    no,it is generated automatically.