Yeah, drawing sprites on a canvas in general is slow. Not to mention with the added collision detection. I would try doing a game-loop for for the animation/calculation and have a single loop for drawing. This makes sure you draw as fast as possible and your calculations run at a steady rate.
http://www.koonsolo.com/news/dewitters-gameloop/ http://www.gamepoetry.com/blog/2008/04/18/understanding-the-game-loop/ and just google game loop. On Tue, May 24, 2011 at 8:25 AM, neuromit <stuart.lay...@gmail.com> wrote: > Thank you both for your suggestions, they are really helpful. I was > able to optimise my code quite a bit. > > My collision detection is pretty primitive, so I'll definitely try to > improve that. > > public boolean detectCollision(Point target, int dist) > { > return (Math.abs(this.x - target.x)<=dist && Math.abs(this.y - > target.y)<=dist); > } > > > > @Danie, you mentioned that I should split the drawing form the > animation code; What are the benefits of doing this? as both drawing > and moving are driven by the same loop? > > On May 24, 8:33 am, Daniel Drozdzewski <daniel.drozdzew...@gmail.com> > wrote: > > Stuart, > > > > There are few fixes straight away: > > > > - Have a predefined Paint object for your dots and use that instead > > of creating new Paint object each time you enter drawDots() > > - assign dots.size() to a local variable and keep testing that > > variable in the for loop, rather than calling size() method with each > > operation > > - don't detect collisions in your drawing code > > - worth testing whether Canvas.drawBitmap() (cached bitmap of a dot) > > could be quicker than Canvas.drawCircle(); first one only copies > > memory byte by byte, while second does some computation too > > - and finally as String said: profile it > > > > Then you have to think: > > - how complex is your collision detection? it has to be optimised and > > you should not be iterating through all dots testing for potential > > collision with PacMan; if you think about it, your dots don't move and > > PacMan moves incrementally; there are few clever strategies that you > > could use; in general collision detection optimisation strategies are > > not trivial, but I am sure you could find something that would work > > well for you and would not be too hard to implement; > > > > have a look here as an entry to possible ideas: > http://en.wikipedia.org/wiki/Collision_detection > > > > > > > > > > > > > > > > > > > > On Tue, May 24, 2011 at 12:57 PM, neuromit <stuart.lay...@gmail.com> > wrote: > > > I'm writing a pacman style game that has a lot of things to be drawn, > > > namely, the dots that pacman likes to eat. > > > > > I'm finding that the drawing of the dots is greatly hampering the > > > performance of the game. If I turn them off my app consumes much fewer > > > cpu cycles than if they are left on. > > > > > here is the code i'm using to draw the dots: > http://paste2.org/p/1432169 > > > > > void drawDots(Canvas c) { > > > if (!this.drawDots) > > > return; > > > > > Paint p = new Paint(mPaint); > > > p.setColor(this.dotColor); > > > for (int i=0; i<dots.size(); i++) > > > { > > > Dot d = dots.get(i); > > > if (this.pacdroid.detectCollision(d)) > > > d.disableDot(); > > > if (d.enabled) > > > c.drawCircle(d.x, d.y,d.r, p); > > > //dots.get(i).draw(c, p); > > > } > > > } > > > > > -- > > > You received this message because you are subscribed to the Google > > > Groups "Android Developers" group. > > > To post to this group, send email to > android-developers@googlegroups.com > > > To unsubscribe from this group, send email to > > > android-developers+unsubscr...@googlegroups.com > > > For more options, visit this group at > > >http://groups.google.com/group/android-developers?hl=en > > > > -- > > Daniel Drozdzewski > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- ~ Jeremiah:9:23-24 Android 2D MMORPG: http://solrpg.com/, http://www.youtube.com/user/revoltingx -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en