java - Spawn balls randomly -
im making game balls spawn randomly each side of smartphone screen, , got working although balls spawn on top side of screen , 2/10 times on bottom , sides of screen. heres code:
//creates balls , sets position random timer each private void spawnballs1() { rectangle ball1 = new rectangle(); ball1.x = mathutils.random(639, 641); ball1.y = 720; ball1.width = 32; ball1.height = 32; balls1.add(ball1); lastdroptime = timeutils.nanotime(); } private void spawnballs2() { rectangle ball2 = new rectangle(); ball2.x = 0; ball2.y = mathutils.random(359, 361); ball2.width = 32; ball2.height = 32; balls2.add(ball2); lastdroptime = timeutils.nanotime(); } private void spawnballs3() { rectangle ball3 = new rectangle(); ball3.x = mathutils.random(639, 641); ball3.y = 0; ball3.width = 32; ball3.height = 32; balls3.add(ball3); lastdroptime = timeutils.nanotime(); } private void spawnballs4() { rectangle ball4 = new rectangle(); ball4.x = 1280; ball4.y = mathutils.random(359, 361); ball4.width = 32; ball4.height = 32; balls4.add(ball4); lastdroptime = timeutils.nanotime(); } //draws balls (rectangle ball1 : balls1) { batch.draw(ball, ball1.x, ball1.y); } (rectangle ball2 : balls2) { batch.draw(ball, ball2.x, ball2.y); } (rectangle ball3 : balls3) { batch.draw(ball, ball3.x, ball3.y); } (rectangle ball4 : balls4) { batch.draw(ball, ball4.x, ball4.y); } // if time minus time of last ball spawn less x spawn ball in random place if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs1(); iterator<rectangle> iter1 = balls1.iterator(); while(iter1.hasnext()) { rectangle balls1 = iter1.next(); balls1.y -= 500 * gdx.graphics.getdeltatime(); if (balls1.overlaps(square)) { score++; showscore = "score: " + score; iter1.remove(); } } // if time minus time of last ball spawn less x spawn ball in random place if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs2(); iterator<rectangle> iter2 = balls2.iterator(); while (iter2.hasnext()) { rectangle balls2 = iter2.next(); balls2.x += 500 * gdx.graphics.getdeltatime(); if (balls2.overlaps(square)) { score++; showscore = "score: " + score; iter2.remove(); } } // if time minus time of last ball spawn less x spawn ball in random place if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs3(); iterator<rectangle> iter3 = balls3.iterator(); while(iter3.hasnext()) { rectangle balls3 = iter3.next(); balls3.y += 500 * gdx.graphics.getdeltatime(); if (balls3.overlaps(square)) { score++; showscore = "score: " + score; iter3.remove(); } } // if time minus time of last ball spawn less x spawn ball in random place if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs4(); iterator<rectangle> iter4 = balls4.iterator(); while(iter4.hasnext()) { rectangle balls4 = iter4.next(); balls4.x -= 500 * gdx.graphics.getdeltatime(); if (balls4.overlaps(square)) { score++; showscore = "score: " + score; iter4.remove(); } } what should do/change make them spawn randomly on each side?
thanks in advance!
all 4 of ball spawning methods share same lastdroptime variable. execution time between
if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs1(); and
if (timeutils.nanotime() - lastdroptime > 1000000000) spawnballs4(); is going tiny percentage of 1000000000 ns, it's going unlikely spawnballs2-4 ever called.
you should have 1 test instead of four, , randomly pick 1 of methods. this:
if (timeutils.nanotime() - lastdroptime > 1000000000){ switch (mathutils.randomint(4)){ case 0: spawnballs1(); break; case 1: spawnballs2(); break; case 2: spawnballs3(); break; case 3: spawnballs4(); break; } } also, lot of copy-paste code, ballooning time investment. ought write 1 spawnballs method takes parameter affects places balls.
Comments
Post a Comment