java - Static count(Android) -
in method bounce()
value of count in first increment not taken forward second increment..i meant supose count=0
..then first time increment count value becomes 1 nd second time vaue becomes 1..thats may because both count stored in different memory location...if make count static each time run application retains value stored in last time don't want..i want value of count in first increment should taken forward second increment...also can't put count outside if loop inside bounce()
method give wroung output..what should do?
public class ball { private point p; //represents x , y position of ball private int c; //represents color of ball private int r; //represents radius of ball. private int dx; //represents change in x position of ball. (horizontal speed) private int dy; //represents change in y position of ball. (vertical speed) private paint paint; //android object holding color drawing on canvas public int count = 0; public ball(int x,int y,int col,int radius) { p=new point(x,y); c=col; r=radius; paint=new paint(); dx=0; dy=0; } public int getcount() {return count; } public int getx() //returns x position of ball integer {return p.x; } public int gety() //returns y position of ball integer { return p.y; } public int getradius() //returns radius of ball integer {return r; } public paint getpaint() //returns paint of ball paint object {return paint; } public void setcolor(int col) //sets color of ball {c=col; if (paint == null) paint = new paint(); paint.setcolor(col); } public void goto(int x,int y) //sets x , y positions of ball {p.x=x; p.y=y; } public void setdx(int speed) //sets horizontal speed of ball {dx=speed; } public void setdy(int speed) //sets vertical speed of ball { dy=speed; } public void move() //changes x , y position dx , dy values { p.x=p.x+dx; p.y=p.y+dy; } public void setx(int x) { p.x = x; } public void sety(int y) { p.y = y; } public int getdx() { return p.x; } public int getdy() { return p.y; } public void bounce(canvas canvas) //bounces off sides of canvas { move(); if(p.x>canvas.getwidth()|| p.x<0) { count++; dx=dx * -1; } if(p.y>canvas.getwidth()|| p.y<0) { count++; dy=dy * -1; } mainactivity.tv.settext(string.valueof(getcount())); } }
just thought should add comments answer. should create 1 instance of ball
class , use it. each time create new instance of ball
, set count = 0
Comments
Post a Comment