Switching between two scenes
in
Programming Questions
•
11 months ago
Hi All,
I am having difficulty changing between two states. Basically there is a day and night scene with a button in the upper right hand corner that you click on to [theoretically] switch back and forth between the two. I can get it to switch from day to night but can't get it to go back to day. I feel like I might have some functions in the wrong place. I have tried using the redraw() method in various places as well. Here's the code:
- // Images
- PImage dayTime;
- PImage nightTime;
- PImage dayBut;
- PImage nightBut;
- // Coordinates
- int butDayX, butDayY;
- int butNightX, butNightY;
- // Booleans
- boolean isNight = false;
- boolean overDay = false;
- boolean overNight = false;
- void setup() {
- size(750, 500);
- // Load Images
- dayTime = loadImage("day.jpeg");
- nightTime = loadImage("night.jpeg");
- dayBut = loadImage("dayBut.jpg");
- nightBut = loadImage("nightBut.jpg");
- // Button Positions
- butDayX = width - 125;
- butDayY = 25;
- butNightX = width - 125;
- butNightY = 25;
- }
- void draw() {
- if(isNight) {
- drawNight();
- }
- if(!isNight) {
- drawDay();
- }
- }
- // Update
- void update(int x, int y) {
- if(isNight) {
- if(overDay(butDayX, butDayY, 100)) {
- isNight = false;
- }
- }
- if(!isNight) {
- if(overNight(butNightX, butNightY, 100)) {
- isNight = true;
- }
- }
- }
- // Change to Day Scene from Button Click
- boolean overDay(int x, int y, int size) {
- if(mousePressed && mouseX >= x && mouseX <= x+size &&
- mouseY >= y && mouseY <= y+size)
- {
- overDay = true;
- return true;
- }
- else
- {
- overDay = false;
- return false;
- }
- }
- // Change to Night Scene from Button Click
- boolean overNight(int x, int y, int size) {
- if(mousePressed && mouseX >= x && mouseX <= x+size &&
- mouseY >= y && mouseY <= y+size)
- {
- overNight = true;
- return true;
- }
- else
- {
- overNight = false;
- return false;
- }
- }
- // Draw Night Scene
- void drawNight() {
- image(nightTime, 0, 0);
- image(dayBut, butDayX, butDayY);
- }
- // Draw Day Scene
- void drawDay() {
- image(dayTime, 0, 0);
- image(nightBut, butNightX, butNightY);
- }
- void mousePressed() {
- update(mouseX, mouseY);
- }
Any help is greatly appreciated. Thanks in advance!
1