canvas - Android drawBitmap paint -
i trying show bullets when user touched screen
i making bullet here
public projectile(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); paint = new paint(); bulletbitmap = bitmapfactory.decoderesource(context.getresources(), r.drawable.bullet); } public interface projectilelistener { public void onprojectilechanged(float delta, float angle); } public void setprojectilelistener(projectilelistener l) { listener = l; } public void setprojectiledirection(int x, int y, int size){ pos = new rect(x, y, size, size); invalidate(); } protected void ondraw(canvas c) { c.drawbitmap(bulletbitmap, pos, pos, paint); super.ondraw(c); }
and call here
projectile p = new projectile(toweranimation.this); p.setprojectiledirection(x, y, 50); projectiles.add(p); canvas c = null; p.ondraw(c);
however getting errors on line
c.drawbitmap(bulletbitmap, pos, pos, paint);
did make wrong drawbitmap? thanks
in following code:
projectile p = new projectile(toweranimation.this); p.setprojectiledirection(x, y, 50); projectiles.add(p); canvas c = null; <------------------ here p.ondraw(c); <------------------ npe
you setting c
null
, passing ondraw()
. what's happening in ondraw()
:
protected void ondraw(canvas c) { null.drawbitmap(bulletbitmap, pos, pos, paint); <--------- npe super.ondraw(c); }
edit 1:
i'm not sure trying code. check class bulletsonscreen
. use it, need add view layout. example, if have linearlayout, can use addview()
method:
mylinearlayout.addview(new bulletsonscreen(this)); public class bulletsonscreen extends view { bitmap bullet; boolean touched; float xvalue, yvalue; public bulletsonscreen(context context) { super(context); setfocusable(true); bullet = bitmapfactory.decoderesource(context.getresources(), r.drawable.bullet); touched = false; } protected void ondraw(canvas canvas) { if (touched) { canvas.drawbitmap(bullet, xvalue, yvalue, null); touched = false; } } public boolean ontouchevent(motionevent event) { xvalue = event.getx(); yvalue = event.gety(); touched = true; invalidate(); }
Comments
Post a Comment