We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I need some fresh eyes if you have time. I'm making a game and when the "winstate" is activated by reaching the goal, the game freezes. This didn't happen before and it doesn't freeze any other time. I don't understand why. Here is the code.
//First declare objects used for this session
Blocker obstacleBlocktopdown;
Blocker obstacleBlockacross;
Avatar playerAvatar;
Seeker seekerStone1;
GoalState goal;
PImage titlecard;
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
boolean winstate = false;
boolean playstate = false;
boolean homescreen = true;
boolean tutorialscreen = false;
boolean losestate = false;
int wincount = 0;
int losscount = 0;
void setup(){
size(950, 950);
obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 1.2);
obstacleBlockacross = new Blocker(1, height/2, 20, 20, 2, .4);
playerAvatar = new Avatar(11, 11, 18, 18);
seekerStone1 = new Seeker(1100, 600, 18, 18);
goal = new GoalState(790, 500);
titlecard = loadImage("BluePulsarWave.jpg");
//winner = new SoundFile(this, "Marvelous Battle OST's The Intrepid - DASH.mp3");
}
void draw(){
////////////////////////////////////HOME/TITLE SCREEN
if (homescreen == true){
background((mouseX+150)/4, (mouseY+150)/4, 0);
//image(titlecard, 200, 400, 500, 200);
stroke(0);
fill((mouseY)/5, 0, (mouseX)/5);
textSize(60);
text("Game Seeker", 200, height/4);
stroke(0);
fill((mouseY-50)/3, 0, (mouseX-50)/3);
textSize(24);
text("Press Spacebar to begin", 200, height - (height/4));
textSize(14);
text("Press T for a tutorial", 775, height - (height/4));
if(key == ' ') {
homescreen = false;
playstate = true;
}
if(key == 't') {
homescreen = false;
tutorialscreen = true;
}
}
/////////////////////////////////TUTORIAL SCREEN
if (tutorialscreen == true){
background(255);
stroke(0);
fill(0);
textSize(24);
text("How to play Game Seeker", 200, 200);
text("Press Q to go back to Title Screen", 200, 800);
text("Or Press spacebar to start the Game!!", 200, 850);
if (key == 'q') {
tutorialscreen = false;
homescreen = true;
}
if (key == ' '){
tutorialscreen = false;
playstate = true;
}
}
//////////////////////LOSE STATE
if (losestate == true) {
background(0);
stroke(255);
fill(255);
textSize(56);
text("You Lost!!!", 200, height/2);
textSize(24);
text("Press spacebar to try again", 800, 700);
text("Or press Q to quit", 800, 800);
if (key == ' ') {
losestate = false;
playstate = true;
}
if (key == 'q') {
losestate = false;
homescreen = true;
}
}
//////////////////////////PLAY STATE
if (playstate == true) {
playerAvatar.constrainToScreen();
if (upPressed) {// playerAvatar moves up 1 pixel
playerAvatar.movesup();
}
if (downPressed) {//playerAvatar moves down 1 pixel
playerAvatar.movesdown();
}
if (leftPressed) {//playerAvatar moves to the left 1 pixel
playerAvatar.movesleft();
}
if (rightPressed) {//playerAvatar moves to the right 1 pixel
playerAvatar.movesright();
}
background(0);
obstacleBlocktopdown.run();
obstacleBlockacross.run();
playerAvatar.display();
seekerStone1.synch2avatar();
goal.display();
}
////////////////CREATE WIN SCENARIO
if (dist(goal.x, goal.y, seekerStone1.x, seekerStone1.y) < 9) {
playstate = false;
winstate = true;
//wincount++;
} else if (winstate) {
background (0);
textSize(36);
text("You Win", 100, 100);
text("Seeker has reached Goal!", 100, 200);
textSize(24);
text("Number of games won... " + wincount, 100, 600);
text("Number of games lost... " + losscount, 100, 700);
textSize(14);
text("Press spacebar to restart", 100, 800);
//winner.play();
if (key == ' ') {
winstate = false;
playstate = true;
seekerStone1.x = 1100;
seekerStone1.y = 600;
playerAvatar.x = 11;
playerAvatar.y = 11;
obstacleBlocktopdown.x = width/2;
obstacleBlocktopdown.y = 0;
obstacleBlockacross.x = 0;
obstacleBlockacross.y = height/2;
}
}
}
//enacts the movement of desired object
void keyPressed (KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = true;
}
else if (keyCode == DOWN) {
downPressed = true;
}
else if (keyCode == LEFT) {
leftPressed = true;
}
else if (keyCode == RIGHT) {
rightPressed = true;
}
}
}
// stops movement once key(s) are released
void keyReleased(KeyEvent e) {
if (key == CODED) {
if (keyCode == UP) {
upPressed = false;
}
else if (keyCode == DOWN) {
downPressed = false;
}
else if (keyCode == LEFT) {
leftPressed = false;
}
else if (keyCode == RIGHT) {
rightPressed = false;
}
}
}
None of the other states have this, why does this game freeze? I've gone over it many times and can't understand why.
Answers
Hard to help without running the code.
I suggest to add some println() to see what is going on in your code.
I fixed the problem. The problem was where I called the winstate. I'll display the whole code for reference.