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

2016/3/11

if confirmation dialog = something...

AdamMyczkowski AdamMyczkowski

2016/3/11

#
Hey I'm building my game and I want to implement single and two player game, I have a confirmation dialog but I cannot find a code for that if you press eg YES it will spawn single player actors etc. Anyone have ideas/ advice?
danpost danpost

2016/3/11

#
It would be important how the confirmation dialog was implemented, code-wise. Also, any classes you created relating to the dialog and the issue at hand should be shown.
AdamMyczkowski AdamMyczkowski

2016/3/11

#
This is my code for world 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import greenfoot.*;
import javax.swing.*;
 
public class TurtleWorld extends World
{
    public TurtleWorld()
    {
        super(600, 480, 1);
         
        String option = (String) JOptionPane.showConfirmDialog(null, "Do you want to play a sigle player game?", JOptionPane.YES_NO_OPTION);
         
        if ("this is where I don't know what to use")
        {
            singlePlayer();
        }
        if ("this is where I don't know what to use")
        {
            singlePlayer();
        }
        else
        {
            Greenfoot.stop();
        }
    }
    public void singlePlayer()
    {
            Bug[] bugs = new Bug[2];
            for(int i = 0; i<bugs.length; i++)
            {
                bugs[i] = new Bug();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(bugs[i], flyX, flyY);
            }
         
            Lettuce[] lettuce = new Lettuce[25];
            for(int i = 0; i<lettuce.length; i++)
            {
                lettuce[i] = new Lettuce();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(lettuce[i], flyX, flyY);
            }
             
            Snake[] snakes = new Snake[2];
            for(int i = 0; i<snakes.length; i++)
            {
                snakes[i] = new Snake();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(snakes[i], flyX, flyY);
            }
             
            Timer timer = new Timer();
            addObject(timer, 45, 462);
             
            Counter counter = new Counter();
            addObject(counter, 94, 20);
             
            Turtle turtle = new Turtle(counter);
            addObject(turtle ,Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
    public void twoPlayer()
    {
            Bug[] bugs = new Bug[2];
            for(int i = 0; i<bugs.length; i++)
            {
                bugs[i] = new Bug();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(bugs[i], flyX, flyY);
            }
         
            Lettuce[] lettuce = new Lettuce[30];
            for(int i = 0; i<lettuce.length; i++)
            {
                lettuce[i] = new Lettuce();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(lettuce[i], flyX, flyY);
            }
             
            Snake[] snakes = new Snake[2];
            for(int i = 0; i<snakes.length; i++)
            {
                snakes[i] = new Snake();
             
                int flyX = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
                int flyY = 15 + Greenfoot.getRandomNumber(getWidth() - 25);
             
                addObject(snakes[i], flyX, flyY);
            }
  
            Timer timer = new Timer();
            addObject(timer, 45, 462);
         
            CounterP1 counterP1 = new CounterP1();
            addObject(counterP1, 94, 20);
         
            CounterP2 counterP2 = new CounterP2();
            addObject(counterP2, 505, 20);
         
            Player1 player1 = new Player1(counterP1);
            addObject(player1 ,Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
         
            Player2 player2 = new Player2(counterP2);
            addObject(player2 ,Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
danpost danpost

2016/3/11

#
There seems to be a couple of issues with line 10. First, there is no 'showConfirmDialog' method that uses the number and types of parameters as given in the line. Second, all four of the 'showConfirmDialog' methods returns an int value (none return a String object, which you are trying to cast the return value to and set to a String variable ). Have you even review the JOptionPane class documentation before trying to use it? Please do so, it is here. You should note the class fields supplied with the class -- they should help as far as what to put for your 'if' conditions.
AdamMyczkowski AdamMyczkowski

2016/3/11

#
Oh sorry I didn't mean to include the string in this code, I tried method with an input before and forgot to delete the 'string'
You need to login to post a reply.