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

2015/1/12

Problem with Images in Array

elementa elementa

2015/1/12

#
Hi, so I have this problem with my images that are arranged in this array. In my game, I have a correct door and a wrong door, in which when you click either of them the image will switch to the next set of "doors". int set = 1 Here is the code for the CorrectDoor class:
1
2
3
4
5
6
7
8
public void act()
    {
      if(Greenfoot.mouseClicked(this))
      {
          GameWorld world2 = (GameWorld) getWorld();
          world2.nextSet();
}
}
The nextSet method is located in the GameWorld in which it should update the set number.
1
2
3
4
5
6
public void nextSet()
  {
      set++;
      door.updateImage(set);
      wrongdoor.updateImage(set);
  }
The two updateImage methods are located both in the CorrectDoor and the WrongDoor here, as well as the respective arrays that include the file names. CorrectDoor
1
2
3
4
5
6
7
8
String [] cimages = {"3-correct.png","5-correct.png","8-correct.png","13-correct.png","21-correct.png",
                                "34-correct.png","55-correct.png","89-correct.png","144-correct.png"};
                                 
    public void updateImage(int set)
    {
        setImage(new GreenfootImage(cimages[set - 1]));
        System.out.println(cimages[set]);
    }
Wrong Door:
1
2
3
4
5
6
7
8
String [ ] wimages = {"5-wrong.png","7-wrong.png","10-wrong.png","23-wrong.png","31-wrong.png",
                                "54-wrong.png","85-wrong.png","99-wrong.png","164-wrong.png"};
                                 
    public void updateImage(int set)
    {
        setImage(new GreenfootImage(wimages[set - 1]));
        System.out.println(wimages[set]);
    }
My problem is that when I click on the first set of doors, it skips the second set of doors for "3-correct.png" and "5-wrong.png", and also at the very end of these two arrays, when "89-correct.png" and "99-wrong.png", if you click on "89-correct.png", it only switches that individual door to "144.png" but not to the WrongDoor to "164.png" I've had a look at these lines of code but I'm still not sure what the problem is ?
danpost danpost

2015/1/12

#
Add the initial images at the beginning of the two arrays. I presume they would be "2-correct.png" and something like "3-wrong.png". Then in the GameWorld constructor, initialize the value of 'set' to zero and call the 'nextSet' method. That should prevent the skipping at the beginning. There seems to be no apparent reason for the last wrong door image change to be skipped; maybe it is some other code within the class. Please post the entire WrongDoor class code.
elementa elementa

2015/1/12

#
Thank you so much ! It works all except for the last bit! Here is the full code for the WrongDoor class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
 * Write a description of class WrongDoor here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class WrongDoor extends GamePlay
{
    GameWorld world2 = (GameWorld) getWorld();
    int set = 0;
    /**
     * Act - do whatever the WrongDoor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act()
    {
      if(Greenfoot.mouseClicked(this))
      {
          questions();
      }
    }   
     
    String [ ] wimages = {"5-wrong.png","7-wrong.png","10-wrong.png","23-wrong.png","31-wrong.png",
                                "54-wrong.png","85-wrong.png","99-wrong.png","164-wrong.png"};
                                 
    public void updateImage(int set)
    {
        setImage(new GreenfootImage(wimages[set - 1]));
        System.out.println(wimages[set]);
    }
  
     String[][] questions = {{"The numbers in the Fibonacci Sequence are named after Leonardo Fibonacci. What is his full name?", "Leonardo Pisano Bigollo"},
                        {"How is the Fibonacci Sequence most commonly presented? (diagram-wise)", "Spiral"},
                        {"The Fibonacci numbers appear in one of Dan Brown’s book. Which book is it?", "The Da Vinci Code"},
                        {"The quotient of two successive numbers in the sequence is close to which math ratio?", "The Golden Ratio"},
                        {"What are the first two numbers in the Fibonacci Sequence?", "1 and 1"},
                        {"What number is missing from this excerpt of the sequence: 1, 1, 2, ? , 5, 8", "3"},
                        {"Besides creating the Fibonacci Sequence, what else was Leonardo de Pisa famous for? Creating pizza, or introducing Arabic numbers to Europe?", "Introducing Arabic numbers to Europe"},
                        {"Is the number 377 in the Fibonacci Sequence?", "Yes"},
                        {"What number is missing from this excerpt of the sequence: 1, 1, 2, ? , 5, 8", "Yes"},
                        {"Can the Fibonacci Sequence be carried out with any 2 numbers?", "Flowers"}};
     int num = 0;
    public void questions()
    {
        JFrame frame = new JFrame("InputDialog Example #1");
        int i = num;
        String answer = "";
        while(!answer.equals(questions[i][1]))
            {
              answer = JOptionPane.showInputDialog(frame, questions[i][0]);
              if(answer.equals(questions[i][1]))
              {
                  System.out.println("Yay");
                  GameWorld world2 = (GameWorld) getWorld();
                  world2.nextSet();
              }
            }
        num++;
        }
    
    }                 
danpost danpost

2015/1/12

#
No. I do not see anything here that might cause it, either. But, you can remove both lines 13 and 14 from the code.
You need to login to post a reply.