We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hallo guys iam currently programming a game for my project and dont know hot to solve my problem. When the figur enters a sprecific space defined in the method enterRect() a new quiz screen is called. when ur answer is correct the method correctAnswer will be called and when u answer false the method falseAnswer() will be called.My problem is after i the answer question the display doesnt change from correct/false to the game. my variable correctAnswer/falseAnswer is always on 1 after answering the question.
Code:
my question variables and methods for quiz screen:
public int falseAnswer=0;
public int correctAnswer=0;
void setup() {
frameRate(60);
questions= new Question[7];
ball= new Ball(50);
s= new Shape();
s.createRect(100, 80);
s.createRect(100, 500);
s.createRect(800, 80);
s.createRect(800, 500);
}
void draw() {
if (gameScreen==0) {
initScreen();
} else if (gameScreen==1) {
gameStartScreen();
} else if (gameScreen==2) {
gameOverScreen();
} else if (gameScreen==3) {
quizScreen();
}
//print("K "+correctAnswer+" F "+falseAnswer);
}
public void quizScreen() {
questions[0]=new Question("Wer", "Wer", "Wer", "Wer", "Wer");
question=questions[0];
if (correctAnswer==1) {
question.correctAnswer();
} else if (falseAnswer==1) {
question.falseAnswer();
}
}
void startGame() {
gameScreen=1;
}
void gameOver() {
gameScreen=2;
}
public void startQuiz() {
gameScreen=3;
}
void mousePressed() {
// if we are on the initial screen , start the game
if (gameScreen==0) {
startGame();
}
if (gameScreen==1) {
startGame();
}
if (gameScreen==2) {
startGame();
}
if (gameScreen==3) {
startQuiz();
if(correctAnswer==1){
startGame();
}else if(falseAnswer==1){
gameStartScreen();
}
}
}
public void mouseReleased() {
if (mouseX >0 && mouseX < (0+150) && mouseY >150 && mouseY<(150+150)) {
falseAnswer=1;
} else if (mouseX >150 && mouseX <(150+150) && mouseY >150 && mouseY< (150+150)) {
falseAnswer=1;
} else if ( mouseX > 300 && mouseX <(300+150) && mouseY >150 && mouseY <(150+150)) {
falseAnswer=1;
} else if (mouseX >450 && mouseX <(450+150) && mouseY >150 && mouseY <(150+150)) {
correctAnswer=1;
}
}
public void enterRect() {
//not finished
if (x>=85 && x<=245 && y>=60 && y<=135 ) {
startQuiz();
}
}
Answers
You've got a lot going on here. I suspect the issue is that, after the quiz answer is given, you never actually have the sketch leave the in-a-quiz state.
Yeah that seems to be the Problem but i dont know how to solve it. Shall I post my whole main sketch code ?
Yes
MAIN SKETCH
}
Question Class
Ball Class
class Shape
why can't people post their tabs in one go...
Iam sry, first time posting a question here.
don't worry
wait a second
is the issue around here, in
mousePressed()
? :because
startGame()
andrestart()
are almost the same: gameScreen=1;in
mousePressed()
you should use notif...if...
butif...else if.....
!!!in mouseReleased() and in keyPressed() you should use gameScreen too in this way.
you need to reset correctAnswer and falseAnswer to
0
Also because of this:
you maybe want to move the figure out of this space when going back to the ball game
Remark
when I click an answer nothing happens - only when clicking the question? The rects are totally off.
Remark II
I've seen horrible horrible things in this code. I won't comment on them. I haven't changed them.
New version
new version with reset correctAnswer and falseAnswer to
0
:first
correctAnswer and falseAnswer are redundant: only one can be 1 at a time, or both be 0.
instead, you should have 2 new gameScreens, gameScreenFalseAnswer and gameScreenRightAnswer
handle those like the other gameScreens in draw, in mousePressed, in mouseReleased, in keyPressed
so you got rid of correctAnswer and falseAnswer completely
second
you can name your states:
etc.
note that they are constants (final) and all are starting with their purpose: gameScreen.....
then your draw() is better readable:
Third, use
switch()
in draw(), in mousePressed, in mouseReleased, in keyPressed :Fourth
loadImages() only in setup
Fifth
define the entire array for your questions in setup, not on the way
Sixth
you don't need the variable question at all, the array question is enough
its because i used at first PApplet with a small size than changed it to full size but forgot to adjust the coordinates sry!
Iam just gonna adjust the coordinates in mouspressed() but my main problem is when i start the game and enter the specific space defined in enterRect()
mouseReleased() corrected
public void mouseReleased() { if (mouseX >0 && mouseX < (0+300) && mouseY >300 && mouseY<(300+300)) { falseAnswer=1; } else if (mouseX >300 && mouseX <(300+300) && mouseY >300 && mouseY< (300+300)) { falseAnswer=1; } else if ( mouseX > 600 && mouseX <(600+300) && mouseY >300 && mouseY <(300+300)) { falseAnswer=1; } else if (mouseX >900 && mouseX <(900+300) && mouseY >300 && mouseY <(300+300)) { correctAnswer=1; } }
and gameScreen corrected
gameScreen=0
our posts were at the same time, I discussed some of the issues above
what is the problem there then?
don't do the question display in the constructor of the class
make a method display() in the class
and call it
thank u alot!
i guess the problem is that i never leave the in quiz state. when i answer the question either correct or answer the code switches between quiz and answer/false display but doesnt return to the game.thats why i think the problem is in the mousePressed() method ` public void mousePressed() {
I posted the entire sketch
did you try it?
It returns to the game alright
yeah it returns to the game after the first time answering the quiz but when u enter the space again its like loop between quiz and false/correct Answer display. And when u change gameScreen to 0 and enter the quiz space u cant reenter the game and its just a loop between quiz and false/answer display.
as I said :
correctAnswer and falseAnswer are redundant: only one can be 1 at a time, or both be 0.
instead, you should have 2 new gameScreens, gameScreenFalseAnswer and gameScreenRightAnswer
handle those like the other gameScreens in draw, in mousePressed, in mouseReleased, in keyPressed
so you got rid of correctAnswer and falseAnswer completely
yeah i did it but either iam doing something wrong or it doesnt wanna work :/Below is my corrected Code did i do something wrong?
I don't know, I can't run the code
Does it work? What happens falsely, what do you want to happen instead?
not sure what you want here,
but shouldn't you use
in this:
There two problems when i run the code, first when i start the code and click on the false/correct space i get false/correct display without even entering the coordinates defined in enterRect() method. and second when i enter the defined space in enterRect() i just have the same problem as before a loop between quiz and false/correct answer
as I said, this is unnecessary
just say
that solved my first problem but now when quiz display starts and i click on one of the answers nothing happens. and i think the problem is in one of these two methods below changed version: public void mousePressed() {
What is the job of mouseReleased?
So all in one mouseReleased determine if u click on the correct or false Answer.
Sorry, but I suggested to test the gameScreen in mouseReleased()
then you said:
then you post the old code of mouseReleased() ???
Did you change it?
What is the job of mouseReleased?
mouseReleased is only appropriate when
if (gameScreen==gameScreenQuizScreen )
, right ?The functions restart() and startGame() are wrong
they must be
gameScreen=gameScreenCorrectScreen
OR
gameScreen=gameScreenFalseScreen
!!!!!
yeah as u said i changed mouseReleased and its only appropriate when my gameScreen ist Quizscreen a u said.but now when i click iam in quizdisplay and click on any of the four answers nothing happens :/
The functions restart() and startGame() are wrong
they must be
gameScreen=gameScreenCorrectScreen
OR
gameScreen=gameScreenFalseScreen
!!!!!
you can make a zip file from your entire project in the menu tools
please send it to me via e-mail
I PM you my email
thank u alot that solved my second problem i just have another question for example when i click corrctanswer i get redirect to correctAnswerscreen as decleared in mouseReleased() but how can i be redirected from that screen to the game again?
thank u alot for ur effort. iam gonna send u but we solved already most of the problems thanks to u there is only one above.
I bet there are more to come...
Are you serious?
What about mousePressed()?
yeah i modified mousePressed but when i click either correct/or false display after answering my quiz i get redirect to quiz display again
public void mousePressed() {
??
you need to check the functions:
is startGame(); doing what you expect?
yeah it changes the value of gameScreen to 1 .
aber jetzt geht es oder?
Solved.