So I'm doing my IB Computer Science IA and working on code for a quiz game about music theory. I'm going pretty easy, but am having a big problem. There is a world for each question (9 questions) and a "hub" that has a welcome and start arrow that opens up one of the questions if pressed. If i am to open up the world with the question of identifying major chords, it looks like this (as of right now).
This works fine, except that when I open up the "QuizHub" world, it plays whatever chord will play in the major chord world, then playing it again when the world actually opens. What did I do wrong, and how can I fix it??
Here's the code for the QuizHub world as well (if it will help):
And here's the code for the StartButton class (it is set to open the major chord world so that I could test its functionality):
And sorry for the lack of description in the class description, I usually wait until I have clean code to insert it. Thanks!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Qu1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class QuMaj extends World
{
/**
* Constructor for objects of class Qu1.
*
*/
public QuMaj()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 474, 1);
prepare();
}
public void prepare()
{
play();
}
public void play()
{
switch(Greenfoot.getRandomNumber(6))
{
case 0: Greenfoot.playSound("Major Chord #1.mid"); break;
case 1: Greenfoot.playSound("Major Chord #2.mid"); break;
case 2: Greenfoot.playSound("Major Chord #3.mid"); break;
case 3: Greenfoot.playSound("Major Chord #4.mid"); break;
case 4: Greenfoot.playSound("Major Chord #5.mid"); break;
case 5: Greenfoot.playSound("Major Chord #6.mid"); break;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
/**
* A Music Theory Quiz that tests the user's knowledge of chord qualities
* in their inversions and root position.
*/
public class QuizHub extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public QuizHub()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 474, 1);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
StartButton startbutton = new StartButton();
addObject(startbutton,747,432);
startbutton.setLocation(757,432);
addObject(new Text(), 393, 231);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class StartButton here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class StartButton extends Actor
{
World[] quList = {new QuMaj(),new QuMin(), new QuAug(), new QuDim(), new QuMaj7(), new QuMin7(),
new QuDom7(),new QuHDim7(), new QuDim7()};
/**
* Act - do whatever the StartButton wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
Greenfoot.setWorld(new QuMaj());
}
}
}
