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

2019/4/19

Memory game help with spawning please

N0way N0way

2019/4/19

#
Hello for my group project in college i have to do memory game but the issue is after selecting a difficulty level and selecting 2 matching cards one one them is staying and its not removing to be exactly its LetterA from array. all my cards have each subclass which is 26 letters form alphabet and 26 pictures with names with this letters. I will change pictures later on so for now everywhere is apple 1 or 2 and my second problem is that even when only 2 cards should spawn with correct second card for example if letter a is card 1 and letter b is card 2 then other 2 cards will be apple and banana but its adding one more card and correct answer to it for some reason My world code where system is spawning system :
    public Actor[] LETTERS= {new LetterA(), new LetterB(),new LetterC(), new LetterD(), new LetterE(), new LetterF(), new LetterG(), new LetterH(), new LetterI(), new LetterJ(), new LetterK(), new LetterL(), new LetterM(), new LetterN(), new LetterO(), new LetterP(), new LetterQ(), new LetterR(), new LetterS(), new LetterT(), new LetterU(), new LetterV(), new LetterW(), new LetterX(), new LetterY(), new LetterZ()};
    public Actor[] PICTURES= {new Apple(), new Banana(),new Cat(), new Dog()};
    public int card1 = 0;
    public int card2 = 0;
    public int card3 = 0;
    public int card4 = 0;
    private int score = 0;
    private int difficulty = 0;
    public int flipCards = 0;
    boolean letterAflip;
    public MyWorld()
    {    
        // Create a new world with 600x500 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        
        prepare();
        
    }
    public void act()
    {
        
        showText("Score = " + score, 100, 20);
        for(int i = 0; i < 1; i++)
        {
          getCardNumbers();
        }
        /*
         public void act()
         {
          if (Greenfoot.mouseClicked(this)) 
             {
                 Greenfoot.setWorld(new NextWorld());
             }
          }
           
           */
        
    
    }
    public int flipCard()
    {
     return flipCards =+ 1;
    }
    public void difficultyUP1()
    {
     difficulty =+ 1;
    }
    public void difficultyUP2()
    {
     difficulty =+ 2;
    }
    public void difficultyUP3()
    {
     difficulty =+ 3;
    }
    public void difficultyReset()
    {
     difficulty = 0;
    }
    
    public int increaseScore()
    {
        //This method adding score
        return score = score + 1;
    }
    public void getCardNumbers()
    {
        GreenfootImage apple1 = new GreenfootImage("apple1.png");
        GreenfootImage apple2 = new GreenfootImage("apple2.png");
        card1 = Greenfoot.getRandomNumber(3);
        card2 = Greenfoot.getRandomNumber(3);
        
        while(card2==card1){
            card2 = Greenfoot.getRandomNumber(3);
        }
        
        int checkLetter1 = card1;
        int checkLetter2 = card2;
        int checkLetter3 = card3;
        int checkLetter4 = card4;
        /*
        if (flipCards == 2)
        {
                for(int i = 0; i < 4; i++)
                {
                  LETTERS[i].setImage("apple1.png");
                  PICTURES[i].setImage("apple1.png");
                }
                Greenfoot.delay(1);
                flipCards = 0;
        }
        */
        boolean card1spawn = true;
        boolean card2spawn = true;
        if(difficulty==1)
        {
        if(card1==card1 && card1spawn == true)
        {
         addObject(LETTERS[card1], 300, 100);
         addObject(PICTURES[card1], 600, 400);
         card1spawn = false;
        }
        if(card2==card2 && card2spawn == true)
        {
         addObject(LETTERS[card2], 600, 100);
         addObject(PICTURES[card2], 300, 400);
         card2spawn = false;
        }
        /*
        boolean letterAflip = LetterA.getLetterAFlip();
        boolean appleflip = Apple.getAppleFlip();
        if(letterAflip == true && appleflip == true)
        {
        removeObject(new Apple());
        
        }
        */
        }
        
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Level1 level1 = new Level1();
        addObject(level1,153,397);
    }
My apple class code
private GreenfootImage apple1 = new GreenfootImage("apple1.png");
    private GreenfootImage apple2 = new GreenfootImage("apple2.png");
    private static boolean appleflip = false;

    /**
     * Act - do whatever the Apple wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        getAppleFlip();
        MyWorld myWorld = (MyWorld) getWorld();
        if(Greenfoot.mouseClicked(this))
        {
            setImage("apple2.png");
            myWorld.flipCard(); 
            appleflip = true;
        } 
        //LetterA letterA = new LetterA();
        boolean letterAflip = LetterA.getLetterAFlip();
        if(letterAflip == true && appleflip == true)
        {
         myWorld.increaseScore();
         Greenfoot.delay(5);
         myWorld.removeObject(this);
         appleflip = false;
        }
    }    
      public void matchApple()
    {
    }
    public static boolean getAppleFlip()
    {        
        return appleflip;
    }
my LetterA code
private GreenfootImage apple1 = new GreenfootImage("apple1.png");
    private GreenfootImage apple2 = new GreenfootImage("apple2.png");
    private static boolean letterAflip = false;
 
    //public boolean correctmatchA = false;
    /**
     * Act - do whatever the A wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    
    public void act() 
    {
        // Add your action code here.        
       getLetterAFlip();
       MyWorld myWorld = (MyWorld) getWorld();
       if(Greenfoot.mouseClicked(this))
        {
         setImage("apple2.png");
         myWorld.flipCard();
         letterAflip = true;
         //correctmatchA = correctMatch(letterAflip, appleflip);
        } 
       
       //Apple apple = new Apple();
       boolean appleflip = Apple.getAppleFlip();
       if(appleflip == true && letterAflip == true)
        {
         myWorld.increaseScore();
         Greenfoot.delay(5);
         myWorld.removeObject(this);
         letterAflip = false;
        }
    }    
    public void matchA()
    {
       
    }
    public static boolean getLetterAFlip()
    {
          return letterAflip;
    }
N0way N0way

2019/4/21

#
could someone help with this one please? :D
nccb nccb

2019/4/21

#
When you remove the apple from the world, you reset appleflip. Similarly, when you remove the letter, you reset letterAflip. So whichever one gets removed first will prevent the other seeing the state where both are flipped. You should probably make one class responsible for removing both the items rather than making them only remove themselves. (Greenfoot.delay delays the whole simulation not just that actor, so it won't make a difference to the behaviour.)
You need to login to post a reply.