This site requires JavaScript, please enable it in your browser!
Greenfoot back
ProfessionalNoob
ProfessionalNoob wrote ...

2015/3/7

How to run a method only once?

ProfessionalNoob ProfessionalNoob

2015/3/7

#
I have a CardSelect() method for my hearthstone clone that adds a random card into the field.
  public void CardSelect()
    {

           if(getY() > 400 && Greenfoot.mouseClicked(this))
           {   
               int value = Greenfoot.getRandomNumber(5);
               getWorld().addObject(playerCards[value], 150 * value^2, 320);
               return;
        } 
            
    }
However, when this method is called in act(), it runs it multiple times per second. This is my Cards class:
  public void act() 
    {
        Generation(); 
    }    
    
   public void Generation(){
    number = dice.Roll(number);
    GreenfootSound[] CardSounds = { new GreenfootSound("KittyPlaySound.mp3"),
                                                         new GreenfootSound("HoundPlaySound.mp3"),
                                                         new GreenfootSound("OgrePlaySound.mp3"),
                                                         new GreenfootSound("RaptorPlaySound.mp3"),
                                                         new GreenfootSound("MurlocPlaySound.mp3"),
                                                         new GreenfootSound("GrommashPlaySound.mp3")};
    GreenfootImage[] CardImages = { new GreenfootImage("Kitty.png"),
                                                         new GreenfootImage("Hound.png"),
                                                         new GreenfootImage("Ogre.png"),
                                                         new GreenfootImage("Raptor.png"),
                                                         new GreenfootImage("Murloc.png"),
                                                         new GreenfootImage("Grommash.png")};
   setImage(CardImages[number]); 
   
   //CardSounds[number].play();

}
Here is how I call cardselect:
public void act() 
    {  

     if (Greenfoot.mouseClicked(this))
    {
        if (click == false){ 
        CardSelect();
        
        click = true;
    }
    }
    }
Now, if I don't do the if statement when I try to call CardSelect, it gives me multiple images. When I do the if statement, it does not give me an image at all. So how can I make it so that it only gives me one image and stays there? Without the if statement in the act method, it loops and gives me a whole bunch of images for one card.
danpost danpost

2015/3/7

#
In the act method of your Cards class, you are unconditionally calling the 'Generation' method, which is creating a new image every act cycle.
ProfessionalNoob ProfessionalNoob

2015/3/7

#
If I comment out Generation() in my card class, what would I do to achieve the result I want? If I do playerCards.Generation(), it does not display an image at all.
danpost danpost

2015/3/7

#
Working in your Cards class code (with the Generation method) above: If you are just trying to give an initial random image to the actor, change line 1 to
public Cards()
Or, alternatively (if once the image is set, it does not change): remove lines 1 through 5 and change line 6 to the above line.
You need to login to post a reply.