java - i am trying to make a pong game in android but i am stuck ,i dont know why ball isnt moving -
here class extends surface view , in run method i've called method animate ball ball isnt animating
public class ourview extends surfaceview implements runnable { canvas c; thread t = null; surfaceholder holder; boolean isitok = false; rect rackettop,racketbottom; int topleft ; int topright ; int toptop ; int topbottom; int bottomleft; int bottomright; int bottomtop; int bottombottom; gamestate state; public ourview(context context) { super(context); holder = getholder(); state = new gamestate(); } @override public void run() { while (isitok == true) { if (!holder.getsurface().isvalid()) continue; c = holder.lockcanvas(); c.drawargb(0, 0, 0, 0); topleft = ((c.getwidth() / 2) / 2) + 50; topright = (c.getwidth() / 2) + ((c.getwidth() / 2) / 2) - 50; toptop = gettop(); topbottom = 10; bottomleft = ((c.getwidth() / 2) / 2) + 50; bottomright = (c.getwidth() / 2) + ((c.getwidth() / 2) / 2) - 50; bottomtop = getbottom() - 10; bottombottom = getbottom(); rackettop = new rect(); // top racket rackettop.set(topleft, toptop, topright, topbottom); //left = ((c.getwidth()/2)/2)+50 //top = gettop() //right = (c.getwidth()/2)+((c.getwidth()/2)/2)-50 //bottom = 10 racketbottom = new rect(); // bottom racket racketbottom.set(bottomleft, bottomtop, bottomright, bottombottom); //left = ((c.getwidth()/2)/2)+50 // top = getbottom()-10 // right = (c.getwidth()/2)+((c.getwidth()/2)/2)-50 // bottom = getbottom() paint white = new paint(); white.setcolor(color.white); white.setstyle(paint.style.fill); c.drawrect(rackettop, white); c.drawrect(racketbottom, white); here called method
state.update(); state.draw(c,white); holder.unlockcanvasandpost(c); } } public void pause() { isitok = false; while(true) { try { t.join(); } catch(interruptedexception e) { e.printstacktrace(); } break; } t = null; } public void resume() { isitok = true; t = new thread(this); t.start(); } here class ball defined
public class gamestate { int ballx,bally; //ball x , y position int ball_size = 20; // size of ball static int ballvelx = 3; // velocity x of ball static int ballvely = 3; // velocity y of ball public gamestate() { } public void update() { ballx += ballvelx; bally += ballvely; } public void draw(canvas canvas, paint paint) { //objects color paint.setcolor(color.white); ballx = canvas.getwidth()/2; bally = canvas.getheight()/2; //ball canvas.drawcircle(ballx,bally,ball_size,paint); } }
help me first android game
without judging on rest of code:
ballx = canvas.getwidth()/2; bally = canvas.getheight()/2; this resets position of ball every time draw it. no wonder not move, when reset position.
call update() instead, ball moved.
Comments
Post a Comment