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

2019/6/5

How to use JOptionPane after using removeTouching?

marscentric marscentric

2019/6/5

#
I'm coding for my end of the year project and I wasn't sure how to code this. So my game has the player move around and when he picks up an item (removeTouching), I want a confirm dialog to pop up and ask a trivia question that can be answered with a yes/no/cancel option. The code below is what I have so far however it does not work, no confrim dialog pops up like it should. Any help would be appreciated, thank you
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;
 
/**
 * Player
 * Yubin
 * May 30
 * this code includes all the things that the player willing be doing (?)
 */
  
public class Player extends Actor
{
    public boolean appleBool; //declaring our booleans here, they are automatically set to "false"
    public boolean teddyBool;
    public boolean fishBool;
    public boolean spiderBool;
     
    public void act()
    {
       checkKeyPress(); //calling key control method
   
       //world1 items
       //we're checking to see what object is touched before removing it and
       //only executing the check if that object isnt removed yet (using our created boolean)
        
       if(isTouching(Apple.class) && !appleBool)
       {
           removeTouching(Apple.class); //apple disappears when player touches it
           appleBool = true;
       }
       else if(isTouching(Teddy.class) && !teddyBool)
       {
           removeTouching(Teddy.class); //teddy disappears when player touches it 
           teddyBool = true;
       }
       else if(isTouching(Fish.class) && !fishBool)
       {
           removeTouching(Fish.class); //fish disappears when player touches it
           fishBool = true;
       }
       else if(isTouching(Spider.class) && !spiderBool)
       {
           removeTouching(Spider.class); //spider disappears when player touches it
           spiderBool = true;
       }
        
       if (appleBool) //now because we've assigned the boolean to true, this block will be called
       {
               JOptionPane.showMessage(null, "Are apples and oranges the same". YES_NO_OPTION);
           appleBool = false;
         }
       else if(teddyBool)
       {
           JOptionPane.showMessage(null, "Cuddle me". YES_NO_OPTION);
           teddyBool = false;
       }
       else if(fishBool)
       {
           JOptionPane.showMessage(null, "Something smells". YES_NO_OPTION);
           fishBool = false;
       }
       if(spiderBool)
       {
           JOptionPane.showMessage(null, "Do you like spiders?". YES_NO_OPTION);
           spiderBool = false;
       }
       
 
       if (isAtEdge())
       {
           turn(20);
       }
       //player bounces off the screen
  
    }
 
    //user controls
    public void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            move(-2);
        }   
     
         if (Greenfoot.isKeyDown("right"))
        {
            move(2);
        }
 
         setRotation(90);
         if (Greenfoot.isKeyDown("up"))
        {
            move(-2);
        }
 
          if (Greenfoot.isKeyDown("down"))
        {
           move(2);
        }
          
         setRotation(0);
 
    }
}
nccb nccb

2019/6/5

#
It doesn't have a yes/no option, but in general, Greenfoot.ask() is easier than JOptionPane. In modern versions of Greenfoot there are issues to do with window ordering if you try to use dialogs in your code.
You need to login to post a reply.