No background shown

edited March 2014 in JavaScript Mode

The program does not even show in javascript mode, but work perfectly in java mode. Can someone tell me what's the problem is??

/* @pjs preload="bg0.jpg, bg1.jpg, bg2.jpg, bg3.jpg, bg4.jpg , goodBean.png, badBean.png"; */

int beansNum = 10;
final int fwidth = 1280;
final int fheight = 720;
final int timeAddBeans = 30000;
KillingBeans game;
PFont defaultFont;
Background bg;
PImage pp;

void setup() {
   size(1280, 720);
   pp = loadImage("bg0.jpg");
   bg = new Background();
   defaultFont = createFont("VVCorrupto.otf", 40, true);
}

void draw() {
   if(game == null) {
       background(pp);
       textFont(defaultFont, 100);
       color textCol = color(172,  172,  172);
       fill(textCol);
       text("Killing Beans", fwidth/2-250, 130);
       textFont(defaultFont, 35);
       textCol = color(255,  255,  12);
       fill(textCol);
       text("Kill the red beans", fwidth/2-130, 300);
       Button timeMode = new Button("Time Mode(Press k)", fwidth/2-150, fheight/2+100, 350, 80, color(56,143,137,40));
       Button normalMode = new Button("Normal Mode(Press m)", fwidth/2-150, fheight/3+100, 350, 80, color(56,143,137,40));
       timeMode.drawing(30, 50, 0, color(255), 30);
       normalMode.drawing(30, 50, 0, color(255), 30);
       if(keyPressed) {
          if(key == 'k' || key == 'K'){
             game = new KillingBeans(fwidth, fheight, 150, true, false, bg);
             game.addBeans(beansNum);
          }
          else if (key == 'm' || key == 'M'){
             game = new KillingBeans(fwidth, fheight, 0, true, true, bg);
             game.addBeans(beansNum);

          }
       }
   }
   if(game != null && game.isStart()) {
       game.renderBg();
       game.displayBeans();
       game.render(false);
       if(millis() % timeAddBeans <= 10) {
           game.addBeans(beansNum);
       }
       if(mousePressed) {
           game.checkIsClicked(mouseX, mouseY);
       }
       if(game.getNumOfBadBeans() == 0) game.addBeans(beansNum);
       if((game.getChances() == 0 || game.getNumOfBadBeans() >= game.MAX_BAD_BEANS) || (!game.getMode() && game.getTime() == 0)) {
           noLoop();
           game.render(true);
           game.setFinish(true);
       }
   }

}



void mouseClicked() {

   if(game != null && game.isStart() && game.isFinish() && game.isRestart(mouseX,mouseY)) {
       game = null;
       loop();
       System.gc();
   }
}

public class Background {
    private int numOfImg = 5;
    private PImage[] bag = new PImage[numOfImg];

    public Background() {
        for(int i = 0; i < numOfImg; i++) {
           String bgStr = "bg" + i + ".jpg";
           bag[i] = loadImage(bgStr);
        }
    }

    public PImage getBg(int i) {return bag[i];}
}



public class Button {
   private String name;
   private int btnWidth;
   private int btnHeight;
   private int xPos;
   private int yPos;
   private color col;
   private PFont myFont = createFont("Georgia", 32);

   public Button(String name, int xPos, int yPos, int btnWidth, int btnHeight, color c) {
      this.name = name;
      this.btnWidth = btnWidth;
      this.btnHeight = btnHeight; 
      this.col = c;
      this.xPos = xPos;
      this.yPos = yPos;
   }

   public void drawing(int leftPadding, int topPadding, int round, color textColor, int fontSize) {  
       fill(col);
       rect(xPos, yPos, btnWidth, btnHeight, round);
       fill(textColor);
       textFont(myFont, fontSize);
       text(name, xPos+leftPadding, yPos+topPadding);
   }

   public boolean isClicked(int x, int y) {
       return (x >= xPos && x <= xPos+btnWidth)
               && (y >= yPos && y <= yPos+btnHeight);
   }

}


public class KillingBeans {

   private int wSize;
   private int hSize;
   private final static int offSet = 50;
   private ArrayList beans = new ArrayList();
   private int score = 0;
   private PFont[] font = new PFont[2];
   private int chances = 3;
   private int timeInitial;
   private int numOfBadBeans = 0;
   public final static int MAX_BAD_BEANS = 50;
   private Button restartBtn;
   private int time;
   private boolean mode;
   private boolean start = false;
   private boolean finish;
   private Background bg;

   public KillingBeans() {}

   public KillingBeans(int widthSize, int heightSize, int time, boolean start, boolean mode, Background bg) {
      this.wSize = widthSize;
      this.hSize = heightSize;
      this.time = time;
      this.start = start;
      this.mode = mode;
      this.bg = bg;
      font[0] = createFont("King Of The Hill.ttf", 40, true);
      font[1] = createFont("VVCorrupto.otf", 40, true);

   }



   public void setFinish(boolean finish) {
      this.finish = finish;
   }

   public boolean isFinish() {
      return finish; 
   }

   public boolean getMode() {
        return mode; 
   }

   public boolean isStart() {
        return start; 
   }

   public void addBeans(int num) {
      for(int i = 0; i < num; i++) {
          int[] coord = {(int)random(offSet, wSize-offSet), (int) random(offSet, hSize-offSet)};
          int randomNum = (int) random(10);
          if(randomNum > 3) {
              beans.add(new BadBean(coord , (int) random(2, 15), (int) random(2, 13)));
              numOfBadBeans++;
          } else {
              beans.add(new GoodBean(coord , (int) random(2, 15), (int) random(2, 13)));
          }
      }
   }

   public void renderBg() {
      if(score >= 40) 
          background(bg.getBg(3));
      else if(score >= 20)
          background(bg.getBg(1));
      else if(score >= 5) 
          background(bg.getBg(2));
      else
          background(bg.getBg(0));

   }

   public void displayBeans() {
       for(int i = 0, n = beans.size(); i < n; i++) {
          Bean bean = (Bean) beans.get(i);
          bean.drawing();
          bean.move();
          bean.preventWallHit(wSize, offSet, hSize);
       }
   }

   public void checkIsClicked(int x, int y) {
       for(int i = 0 ; i < beans.size(); i++) {  
          Bean bean = (Bean) beans.get(i);
          if(bean.isClicked(x, y)) {
               if(bean.getNameClass() == "GoodBean") chances--;
               else score++;
               beans.remove(i);
               numOfBadBeans--;
          } 
       }
   }// end of checkIsClicked

   private void displayMsg(String msg, int xPos, int yPos, int fontSize, PFont fontChoice, color fontColor) {
       textFont(fontChoice, fontSize);
       fill(fontColor);
       text(msg, xPos, yPos);
   }

   public void timePassed(int xPos, int yPos, int fontSize, PFont fontChoice, color fontColor) {
       if(mode) displayMsg("Time Lapse: " + time, xPos, yPos, fontSize, fontChoice, fontColor);
       else displayMsg("Time Left: " + time, xPos, yPos, fontSize, fontChoice, fontColor);
   }

   public void displayScores(int xPos, int yPos, int fontSize, PFont fontChoice, color fontColor) {
       displayMsg("Scores: " + score, xPos, yPos, fontSize, fontChoice, fontColor);
   }

   public void displayChances(int xPos, int yPos, int fontSize, PFont fontChoice, color fontColor) {
       displayMsg("Chances: " + chances, xPos, yPos, fontSize, fontChoice, fontColor);
   }

   public void displayWarning(int xPos, int yPos, int fontSize, PFont fontChoice, color fontColor) {
       displayMsg("Bad Beans are destroying the world!", xPos, yPos, fontSize, fontChoice, fontColor);
   }

   public int getChances() {
       return chances;
   }

   public void render(boolean finalRender) {
       color black = color(0);
       color white = color(255);
       if(finalRender) {
         background(bg.getBg(4));
         displayScores(wSize/3, hSize/2, 80, font[1], white); 
         timePassed(wSize/3, hSize/3, 80, font[1], white);
         restartBtn = new Button("Restart", wSize/3 + 100, hSize/2 + 100, 150, 70, color(46,46,46));
         restartBtn.drawing(27, 45, 10, color(255), 30);
       } else {
         if(mode) addTime();
         else if(!mode) minusTime();
         displayScores(100, 60, 40, font[0], black);
         displayChances(400, 60, 40, font[0], black);
         timePassed(800, 60, 40, font[0], black);
         if(game.getNumOfBadBeans() >= game.MAX_BAD_BEANS - 10) {
             game.displayWarning(100, 90, 20, font[0], black);
         }
       }
   } // end of render

   public boolean isRestart(int x, int y) {
      return restartBtn.isClicked(x, y); 
   }


   public int getNumOfBadBeans()
   {
       return numOfBadBeans;
   }

   private void addTime() {
       if(millis() % 1000 <= 13) time++;
   }

   private void minusTime() {
       if(millis() % 1000 <= 13) time--;
   }

   public int getTime() {
       return time; 
   }

}

public class BadBean extends Bean{
   private PImage badBean;

   public BadBean(int[] coord, int speedX, int speedY) {
       super(coord, speedX, speedY); 
       badBean = loadImage("badBean.png");
   }

   public void drawing() {
       image(badBean, coord[0], coord[1]);
   }

    public void move() {
      super.move(); 
   }

   public void preventWallHit(int widthLimit, int heightTopLimit, int heightBottomLimit) {
       super.preventWallHit(widthLimit, heightTopLimit, heightBottomLimit);
   }

   public boolean isClicked(int xMouse, int yMouse) {
        return super.isClicked(xMouse, yMouse); 
   }

   public String getNameClass() {return "BadBean";}

}

public class GoodBean extends Bean{
   private PImage goodBean;

   public GoodBean(int[] coord, int speedX, int speedY) {
      super(coord, speedX, speedY); 
      goodBean = loadImage("goodBean.png");
   }

   public void drawing() {
       image(goodBean, coord[0], coord[1]);
   }

   public void move() {
      super.move(); 
   }

   public void preventWallHit(int widthLimit, int heightTopLimit, int heightBottomLimit) {
       super.preventWallHit(widthLimit, heightTopLimit, heightBottomLimit);
   }

   public boolean isClicked(int xMouse, int yMouse) {
        return super.isClicked(xMouse, yMouse); 
   }

   public String getNameClass() {return "GoodBean";}



}

public abstract class Bean {
   protected int[] coord;
   public final static int beanWidth = 60;
   public final static int beanHeight = 70;
   private int speedX;
   private int speedY;

   public Bean(int[] coord, int speedX, int speedY) {
       this.coord = new int[2];
       this.coord[0] = coord[0];
       this.coord[1] = coord[1];
       this.speedX = speedX; 
       this.speedY = speedY;
   }

   public abstract void drawing();

   public void move() {
       coord[0] += speedX;
       coord[1] += speedY; 
   }

   public void preventWallHit(int widthLimit, int heightTopLimit, int heightBottomLimit) {
       if((coord[0] < 0 || (coord[0] + Bean.beanWidth) > widthLimit)) {
          speedX *= -1;
       } 
       if(coord[1] < heightTopLimit || (coord[1] + Bean.beanHeight) > heightBottomLimit) {
          speedY *= -1;

       }
   }

   public boolean isClicked(int xMouse, int yMouse) {
       return (xMouse >= coord[0] && xMouse <= coord[0] + Bean.beanWidth)
              && (yMouse >= coord[1] && yMouse <= coord[1] + Bean.beanHeight);
   }

   public abstract String getNameClass();
}

Answers

  • I know it is a class variable, so I don't know if the advice applies here, but in general, in JavaScript, you should avoid having variables with same name than methods. Here, I spotted start, highlighted specially above...

Sign In or Register to comment.