javascript - ProcessingJS: Draw doesn't appear to be looping? -
processingjs: draw doesn't appear looping. i'm trying make simple game have box @ bottom, , blocks fall top , need catch them. draw doesn't seem looping should temporary solution i'm using redraw upon key pressed. i've tested other programs written others on web server , work. there problem in code?
edit: changed code conform suggestions - still not working example at: http://jordantheriault.com/processing/test.html
edit2: i've narrowed down nested loops in draw.
//size of each cell box int cellwidth = 25; //width , height of playable space in cells int w = 8, h = 15; //player position int playerx = 0, playery = h-1; //score int score = 0; int lives = 10; int[][] map = new int[w][h]; // 1 = player // 2 = object catch void setup() { size(cellwidth*w+1, cellwidth*h+1); background(51); fill(255); framerate(30); //starting position player map[playerx][playery] = 1; } void draw(){ if(lives > 0) { background(51); //draw objects onto map for(int = 0; < width-1; i++) { for(int j = 0; j < height-1; j++) { if(map[i][j] == 1) { fill(255); rect(i * cellwidth, j * cellwidth, cellwidth, cellwidth); } if(map[i][j] == 2) { fill(200); rect(i * cellwidth, j * cellwidth, cellwidth, cellwidth); } } } //generate new object catch if (framecount%5==0) { //move squares //todo //generate new newsquare = random(0,w-1); println(newsquare); map[newsquare][0] = 2; } text("score: " + score, 5, 15); } else { text("game over! press x start again.", 10, (h*cellwidth)/2); noloop(); } } void keypressed() { //todo: check collisions if (key == 'a') { if(playerx != 0) { if(map[playerx-1][playery] == 2 || map[playerx][playery-1] == 2) score++; map[playerx][playery] = 0; playerx -= 1; map[playerx][playery] = 1; println("left pressed"); redraw(); } } if (key == 'd') { if(playerx != w-1) { if(map[playerx+1][playery] == 2 || map[playerx][playery-1] == 2) score++; map[playerx][playery] = 0; playerx += 1; map[playerx][playery] = 1; println("right pressed"); redraw(); } } if(key == 'x' && lives < 1) { score = 0; lives = 10; //reset game } }
if want event based updating, you'll need call redraw()
@ end of event handlers (before return, not literally @ end of function definition) draw
function gets called. don't use loop()
, turns automatic next-frame-scheduling code on (the loop
/noloop
functions control whether sketch should automatically scheduling frames).
also, note using noloop
framerate
won't anything. first voids second.
finally, note framerate(300)
insane ;) you're asking processing run draw code in fraction on 3 milliseconds. that's not going happen.
Comments
Post a Comment