I am able to run my game through the compiler (I use Netbeans) and it seems to work fine, with music and effects (I used http://www.javazoom..../javalayer.html for the MP3s to be played), but when I clean and build to create a JAR and run it without the compiler's help the cars, which move in the compiler, don't move, and no action happens. AT first I thought it was the sound, I was using the audiostream library from sun (see here http://stackoverflow...-sound-in-java) to play wavs for the effects at first, but I had noticed then that when I wanted to make a JAR it would be created, but for some reason the compiler deletes whatever that has to do with it, so I decided to use this code instead (http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml) to play the wavs. It works nicely in the compiler, and doesn't mention any infringement of copyright or anything that would affect the code. So I cleaned and built again to see if the JAR would run well, but I get the same error.
I'm using an ActionListener with a swing Timer to recognize the actions every 5 milliseconds, and I didn't start my project as a Desktop Application (i'm not sure if that would have to do with this). My project also depends heavily on the dispose() and new creations of JFrame classes to work (I used that to transfer from level to level), so would that be the problem? I've also been thinking that maybe since the MP3 playing depends on another JAR (the one in http://www.javazoom....javalayer.html) that maybe the creation of the game's JAR interferes with the usage of this other JAR, and the error of the sounds not being played stop the game from working, would that be possible? Thank you in advance for any help!
I'm posting the class for the Level 1 here, but if you want to see the others, go here: http://www.dreaminco...1&#entry1262481 . I haven't posted them here because nothing really serious has changed in them.
package racemaster;
import java.awt.Color; //For background color
import java.awt.Graphics; //To draw
import java.awt.event.ActionEvent; //To get to do events, such as movement
import java.awt.event.ActionListener; //To be able to recognize events
import java.awt.event.KeyAdapter; //To recognize events through keyboard ket presses
import java.awt.event.KeyEvent; //To be able to determine if a key pressed will make/cause an event
import java.awt.image.BufferedImage; //To add images to the objects
import java.io.*; //To successfully use the FileInputStream
import java.util.ArrayList; //To store the walls, further used for collisions
import javax.imageio.ImageIO; //To read images
import javax.swing.JOptionPane; //To display message boxes
import javax.swing.JPanel; //To add every object used in the level
import javax.swing.Timer; //To determine the interval in which actions occur
import javazoom.jl.decoder.JavaLayerException; //JLayer Exception in case of a mp3 failure
import javazoom.jl.player.Player; //To play mp3s
//import sun.audio.*; //to play wavs (AudioStream)
public class Level1Board extends JPanel implements ActionListener{
private Timer timer; //Times interval for actions
private ArrayList walls = new ArrayList(); //Stores walls for use with collision
int carchosen = 1;
boolean lapdecrease;
boolean speedincrease;
String playercarcolor = "blue";
String AIcarcolor = "red";
int angle = 90; //Starting angle of cars
//Addresses for the images of the finish line, blue car, red car, and wall
String lineimageaddress = "images/Miscellaneous/finishline.gif";
BufferedImage line = getImage(lineimageaddress);
String playercarimagesaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + angle + ".png";
BufferedImage playercar = getImage(playercarimagesaddress);
String AIcarimageaddress = "images/Cars/" + AIcarcolor +"car/" + AIcarcolor + "car" + angle + ".png";
BufferedImage AIcar = getImage(AIcarimageaddress);
String walladdress = "images/Walls/greenmarble.png";
BufferedImage wall = getImage(walladdress);
//Addresses for the images of the lap counters
//bluecarlapnumer and redcarlapnumber will increase and then be used to change the lap display
//of both
int playercarlapnumber = 0;
String playercarlapsaddress = "images/Laps/" + playercarcolor + "lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";
BufferedImage playerlapcounter = getImage(playercarlapsaddress);
int AIcarlapnumber = 0;
String AIcarlapsaddress = "images/Laps/" + AIcarcolor + "lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
BufferedImage AIlapcounter = getImage(AIcarlapsaddress);
//Useful to play the 'readysetgo.mp3' before the cars move (especially the AI)
private boolean racestarted = false;
private String raceoutcome = "?";
//Declarations for the cars, walls, and finish line
private Car car1 = new Car(playercar, 0, 0, 0, "", false);
private Car car2 = new Car(AIcar, 0, 0, 0, true);
private Wall wall1 = new Wall(wall, 0, 0, false);
private FinishLine finishline = new FinishLine(line, 100, 200, false);
//Declarations for the lap counters
private LapCounter playercarlapcounter = new LapCounter(playerlapcounter, 0, 0, false);
private LapCounter AIcarlapcounter = new LapCounter(AIlapcounter, 0, 0, true);
//The two checkpoints
private boolean linecheckpoint = false;
private boolean midcheckpoint = true;
FileInputStream fi; //Allows to play wavs as well as mp3s without the Player class of the JAR
Player readysetgo; //Plays the first sound that indicates the beginning of race
//AudioStream as; //In charge of any other sound, except the ones not declared here
AePlayWave wv;
MP3 nightoffire;
boolean backgroundsongisplaying = false;
String level1 = "WWWWWWW\n" //Level 1 structure. Much easier to build
+ "W W\n" //than in GameMaker =)
+ "WCWWW W\n"
+ "W W\n"
+ "WWWWWWW\n";
int spritesize = 100; //Taken from the walls to increment width and height of the Board
int w = 0; //Width of the Board
int h = 0; //Height of the Board
int finishlinex = 0;
int finishliney = 0;
public Level1Board(int carchosen, boolean lapdecrease, boolean speedincrease){
this.carchosen = carchosen;
this.lapdecrease = lapdecrease;
this.speedincrease = speedincrease;
if(carchosen == 1)playercarcolor = "blue";
else if(carchosen == 2)playercarcolor = "red";
else if(carchosen == 3)playercarcolor = "yellow";
else if(carchosen == 4)playercarcolor = "green";
else if(carchosen == 5)playercarcolor = "pink";
else if(carchosen == 6)playercarcolor = "purple";
else if(carchosen == 7)playercarcolor = "turquoise";
else if(carchosen == 0)playercarcolor = "gold";
if(lapdecrease) playercarlapnumber++;
playercarimagesaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + angle + ".png";
playercar = getImage(playercarimagesaddress);
if(carchosen != 2)AIcarcolor = "red";
else AIcarcolor = "blue";
AIcarimageaddress = "images/Cars/" + AIcarcolor +"car/" + AIcarcolor + "car" + angle + ".png";
AIcar = getImage(AIcarimageaddress);
playercarlapsaddress = "images/Laps/" + playercarcolor + "lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";
playerlapcounter = getImage(playercarlapsaddress);
playercarlapcounter = new LapCounter(playerlapcounter, 0, 0, false);
AIcarlapsaddress = "images/Laps/" + AIcarcolor + "lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
AIlapcounter = getImage(AIcarlapsaddress);
AIcarlapcounter = new LapCounter(AIlapcounter, 0, 0, true);
//First sound assgined to the FileInputStream here
try{
fi = new FileInputStream("src\\racemaster\\music\\readysetgo.mp3");
readysetgo = new Player(fi);
}catch(Exception ex){
System.out.println("exception");
}
nightoffire = new MP3("src\\racemaster\\music\\Initial D - Night of Fire.mp3");
addKeyListener(new TAdapter()); //Allows to recognize key presses
setFocusable(true); //Receives focus from hardware (keyboard) to allow for actions
timer = new Timer(5, this); //will execute the actionPerformed method every 5 milliseconds
timer.start(); //starts the timer, duh
startLevel();
}
//Constructs level reading the level1 String with a char
public final void startLevel(){
int x = 0;
int y = 0;
for(int i = 0; i < level1.length(); i++){
char item = level1.charAt(i);
if(item == 'C'){
y += 25;
x += 25;
car2 = new Car(AIcar, x, y, angle, true);
x += 25;
car1 = new Car(playercar, x, y, angle, "N", false);
y -= 25;
x -= 50;
x += spritesize;
finishlinex = car1.x() - 50;
finishliney = car1.y() - 25;
if(speedincrease) car1.setSpeed(car1.getSpeed() + 1);
}else if (item == ' '){
x += spritesize;
}else if(item == 'W'){
wall1 = new Wall(wall, x, y, false);
walls.add(wall1);
x += spritesize;
}else if(item == '\n'){
y += spritesize;
if(w < x){
w = x;
}
x = 0;
}
h = y;
}
}
/*Draws every object declared in the build level and those that have changed
*position/picture during its execution
*/
public final void buildLevel(Graphics g){
g.setColor(new Color(0, 100, 50)); //I love green
//Sets the whole panel with the assigned color
g.fillRect(0, 0, this.getWidth(), this.getHeight());
ArrayList level = new ArrayList(); //Will hold most objects to be drawn
level.addAll(walls);
level.add(finishline);
level.add(car1);
level.add(car2);
for(int i = 0; i < level.size(); i++){
Actor item = (Actor) level.get(i);
if(item instanceof Car){ //Draws the car wherever it is
if(!item.isAI()){
g.drawImage(car1.getBufferedImage(), item.x(), item.y(), this);
}else{
g.drawImage(car2.getBufferedImage(), item.x(), item.y(), this);
}
}
if(item instanceof Wall){ //Draws the Walls in their place
g.drawImage(wall1.getBufferedImage(), item.x(), item.y(), this);
}
if(item instanceof FinishLine){ //Draws the finish line
g.drawImage(finishline.getBufferedImage(), finishlinex, finishliney, this);
}
}
//The following 2 lines will draw the lap counters
g.drawImage(playercarlapcounter.getBufferedImage(), 0, h, this);
g.drawImage(AIcarlapcounter.getBufferedImage(), w - AIcarlapcounter.getBufferedImage().getWidth(), h, this);
}
//Takes a String as directory and gets an image from it
private BufferedImage getImage(String filename) {
try{
InputStream in = getClass().getResourceAsStream(filename);
return ImageIO.read(in);
}catch(IOException io){
System.out.println("The Image was not loaded.");
System.exit(1);
}
return null;
}
//Rotates Car for both the player and the AI
public void rotateCar(Actor c, int a){
String newplayercarimageaddress = "";
String newAIcarimageaddress = "";
BufferedImage bi = c.getBufferedImage();
if(!c.isAI()){
newplayercarimageaddress = "images/Cars/" + playercarcolor + "car/" + playercarcolor + "car" + car1.getAngle() + ".png";
bi = getImage(newplayercarimageaddress);
//I used 36 because without it the cars would rotate with respect to
//their top left corner. This number came out by initially subtracting
//the height or widths of the previous and new images
if(car1.getPreviousAngle() == 45 && car1.getAngle() == 0){
car1.y += 36;
}else if(car1.getPreviousAngle() == 270 && car1.getAngle() == 225){
car1.x -= 36;
}else if(car1.getPreviousAngle() == 180 && car1.getAngle() == 135){
car1.y -= 36;
}else if(car1.getPreviousAngle() == 135 && car1.getAngle() == 90){
car1.x += 36;
}else if(car1.getPreviousAngle() == 90 && car1.getAngle() == 135){
car1.x -= 36;
}else if(car1.getPreviousAngle() == 135 && car1.getAngle() == 180){
car1.y += 36;
}else if(car1.getPreviousAngle() == 225 && car1.getAngle() == 270){
car1.x += 36;
}else if(car1.getPreviousAngle() == 0 && car1.getAngle() == 45){
car1.y -= 36;
}
//It is necessary to call the car with its new image to rotate, but even
//more necessary to call it with its direction, so that it does not stop
car1 = new Car(bi, car1.x(), car1.y(), car1.getAngle(), car1.getDirection(), false);
if(speedincrease) car1.setSpeed(car1.getSpeed() + 1);
repaint();
car1.planMovement();
}else if(c.isAI()){
//Since this is for the AI, a is only used here. Because the angle manipulation
//occurs in this class
newAIcarimageaddress = "images/Cars/" + AIcarcolor + "car/" + AIcarcolor + "car" + a + ".png";
bi = getImage(newAIcarimageaddress);
if(car2.getAngle() == 45 && a == 0){
car2.y += 36;
}else if(car2.getAngle() == 270 && a == 225){
car2.x -= 36;
}else if(car2.getAngle() == 180 && a == 135){
car2.y -= 36;
}else if(car2.getAngle() == 135 && a == 90){
car2.x += 36;
}else if(car2.getAngle() == 90 && a == 135){
car2.x -= 36;
}else if(car2.getAngle() == 135 && a == 180){
car2.y += 36;
}else if(car2.getAngle() == 225 && a == 270){
car2.x += 36;
}else if(car2.getAngle() == 0 && a == 45){
car2.y -= 36;
}
car2 = new Car(bi, car2.x(), car2.y(), a, true);
repaint();
}
}
//Increases the laps and changes its picture display accordingly
public void increaseLap(Actor lc){
if(!lc.isAI()){
String newplayercarlapaddress = "images/Laps/" + playercarcolor +"lap/" + playercarcolor + "lap" + playercarlapnumber + ".png";
BufferedImage bi = getImage(newplayercarlapaddress);
playercarlapcounter = new LapCounter(bi, playercarlapcounter.x(), playercarlapcounter.y(), playercarlapcounter.isAI());
}else{
String newAIcarlapaddress = "images/Laps/" + AIcarcolor +"lap/" + AIcarcolor + "lap" + AIcarlapnumber + ".png";
BufferedImage bi = getImage(newAIcarlapaddress);
AIcarlapcounter = new LapCounter(bi, AIcarlapcounter.x(), AIcarlapcounter.y(), AIcarlapcounter.isAI());
}
repaint();
}
@Override
public void paint(Graphics g){
super.paint(g);
buildLevel(g);
}
//Used when the Board is built in Level1 Class
public int getBoardWidth() {
return this.w;
}
//Used when the Board is built in Level1 Class
public int getBoardHeight(){
return this.h;
}
public String RaceOutcome(){
return raceoutcome;
}
public void actionPerformed(ActionEvent e) {
if(!backgroundsongisplaying){
nightoffire.play();
backgroundsongisplaying = true;
}
if(racestarted){
try {
readysetgo.play(); //serves as countdown before the race starts
} catch (JavaLayerException ex) {
System.out.println("JLayer could not play the sound 'readysetgo.mp3'");
}
if(CheckWallCollision(car2)){
int car2angle = car2.getAngle();
car2angle -= 45; //basically a turn to the right
if(car2angle < 0){
car2angle = 315;
}
rotateCar(car2, car2angle);
}
car2.AImove();
//Checks if car2 passed the finish line
if(car2.getSpeed() == 1){
if(car2.y() == finishliney && (car2.x() > finishlinex && car2.x() < finishlinex + 100)){
AIcarlapnumber++;
if(AIcarlapnumber < 6)increaseLap(AIcarlapcounter);
}
}else if(car2.getSpeed() == 2){
if(car2.y() == finishliney - 1 && (car2.x() > finishlinex && car2.x() < finishlinex + 100)){
AIcarlapnumber++;
if(AIcarlapnumber < 6)increaseLap(AIcarlapcounter);
}
}
//Done when the AI car finishes first
if(AIcarlapnumber == 6){
try{
//fi = new FileInputStream("src\\racemaster\\music\\doofenshmirtz fail theme.wav");
//as = new AudioStream(fi);
//AudioPlayer.player.start(as);
String failtheme = "src\\racemaster\\music\\doofenshmirtz fail theme.wav";
wv = new AePlayWave(failtheme);
wv.start();
}catch(Exception ex){
}
nightoffire.close();
raceoutcome = "lost";
JOptionPane.showMessageDialog(null, "YOU LOST!");
timer.stop();
}
if(!CheckWallCollision(car1)){
//Checks if midpoint check has been passed
if((car1.y() >= 200 && car1.y() <= 203) && (car1.x() > w - 200 && car1.x() < w - 100)) midcheckpoint = true;
//Checks if finish line is passed
if((car1.y() >= finishliney && car1.y() <= finishliney + 3) && (car1.x() > finishlinex && car1.x() < finishlinex + 100)){
linecheckpoint = true;
if(linecheckpoint && midcheckpoint){
playercarlapnumber++;
linecheckpoint = false;
midcheckpoint = false;
if(playercarlapnumber < 6)increaseLap(playercarlapcounter);
}
}
//When race is completed by player
if(playercarlapnumber == 6){
try{
//fi = new FileInputStream("src\\racemaster\\music\\Ta Da.wav");
//as = new AudioStream(fi);
//AudioPlayer.player.start(as);
String wintheme = "src\\racemaster\\music\\Ta Da.wav";
wv = new AePlayWave(wintheme);
wv.start();
}catch(Exception ex){
}
nightoffire.close();
raceoutcome = "won";
JOptionPane.showMessageDialog(null, "YOU WON!");
timer.stop();
}
car1.move();
}else{
try{
//fi needed to be initialized here so that the bump.wav could
//be played whenever car collides
//fi = new FileInputStream("src\\racemaster\\music\\bump.wav");
//as = new AudioStream(fi);
//AudioPlayer.player.start(as);
String bump = "src\\racemaster\\music\\bump.wav";
wv = new AePlayWave(bump);
wv.start();
}catch(Exception ex){
}
car1.bounce();
}
if(car1.isTurning()){
rotateCar(car1, 0);
}
}else{
racestarted = true;
}
repaint();
}
//Controls collisions of Cars and Walls only
public boolean CheckWallCollision(Actor c){
if(!c.isAI()){
for(int i = 0; i < walls.size(); i++){
Wall ww = (Wall) walls.get(i);
if(c.getAngle() == 90){
if((c.x() > ww.x() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() && (c.y() < ww.y() + wall1.getBufferedImage().getHeight()))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 45){
if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 25 && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 0){
if((c.x() == ww.x() - c.getBufferedImage().getWidth() + 10 || c.x() == ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + wall1.getBufferedImage().getHeight()))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 315){
if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 10 && c.x() < ww.x() + wall1.getBufferedImage().getWidth() - 20) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 270){
if((c.x() > ww.x() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight()) && (c.y() < ww.y() + wall1.getBufferedImage().getHeight())){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 225){
if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 10 && c.x() < ww.x() + wall1.getBufferedImage().getWidth() - 20) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 180){
if((c.x() > ww.x() - c.getBufferedImage().getWidth() && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y()) && (c.y() < ww.y() + wall1.getBufferedImage().getHeight())){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}else if(c.getAngle() == 135){
if((c.x() > ww.x() - c.getBufferedImage().getWidth() + 25 && c.x() < ww.x() + wall1.getBufferedImage().getWidth()) && (c.y() > ww.y() - c.getBufferedImage().getHeight() + 10 && (c.y() < ww.y() + wall1.getBufferedImage().getHeight() - 10))){
if(c.getDirection().equals("D")){
car1.collisiondirection = "D";
}else{
car1.collisiondirection = "R";
}
return true;
}
}
}
}else if(c.isAI()){
for(int i = 0; i < walls.size(); i++){
Wall ww = (Wall) walls.get(i);
if(c.getAngle() == 90){
if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() + 15))){
return true;
}
}else if(c.getAngle() == 45){
if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() - 10))){
return true;
}
}else if(c.getAngle() == 0){
if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 40 && c.x() < ww.x()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
return true;
}
}else if(c.getAngle() == 315){
if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 20 && c.x() < ww.x()) && (c.y() > ww.y() + 35 && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
return true;
}
}else if(c.getAngle() == 270){
if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() - ww.getBufferedImage().getHeight() + 30 && (c.y() < ww.y()))){
return true;
}
}else if(c.getAngle() == 225){
if((c.x() > ww.x() - ww.getBufferedImage().getWidth() + 30 && c.x() < ww.x()) && (c.y() > ww.y() - 80 && (c.y() < ww.y()))){
return true;
}
}else if(c.getAngle() == 180){
if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth()) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight()))){
return true;
}
}else if(c.getAngle() == 135){
if((c.x() > ww.x() && c.x() < ww.x() + ww.getBufferedImage().getWidth() - 10) && (c.y() > ww.y() && (c.y() < ww.y() + ww.getBufferedImage().getHeight() + 20))){
return true;
}
}
}
}
return false;
}
//Class that is in charge of controlling the key presses
private class TAdapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
car1.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
car1.keyReleased(e);
}
}
}

New Topic/Question
Reply




MultiQuote






|