2013-07-22 156 views
0

我试图表现出子弹,当用户触摸屏幕的Android drawBitmap油漆

我在写这里的子弹

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); 
    } 

,并在这里把它

Projectile p = new Projectile(TowerAnimation.this); 
         p.setProjectileDirection(x, y, 50); 
         projectiles.add(p); 
         Canvas c = null; 
         p.onDraw(c); 

但是我得到这条线上的错误

c.drawBitmap(bulletBitmap, pos, pos, paint); 

我是否对drawBitmap做任何错误? 感谢

+0

你会得到什么错误?您是否在以contxt为参数的构造函数中初始化'bulletBitmap'? – Blackbelt

+0

java.lang.NullPointerException 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.View.dispatchTouchEvent(View.java:5546) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTouchEvent(ViewGroup .java:1712) 07-22 16:03:23.231:E/AndroidRuntime(8347):\t at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957) 这里有一些其他 – NoobMe

+0

这是什么:“Canvas c = null;”?你传递null给onDraw方法,可能会得到NullPointerException。 –

回答

1

在下面的代码:

Projectile p = new Projectile(TowerAnimation.this); 
        p.setProjectileDirection(x, y, 50); 
        projectiles.add(p); 
        Canvas c = null; <------------------ here 
        p.onDraw(c);  <------------------ NPE 

要设置cnull并将它传递给onDraw()。这是正在发生的事情你onDraw()

protected void onDraw(Canvas c) { 
    null.drawBitmap(bulletBitmap, pos, pos, paint); <--------- NPE 
    super.onDraw(c); 
} 

编辑1:

我不知道你正在尝试用你的代码做。检查类BulletsOnScreen。要使用它,您需要将其作为视图添加到某个布局。例如,如果你有一个LinearLayout,你可以使用addView()方法:

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(); 
    } 
+0

我试过这个Canvas c = new Canvas(); 但没有运气我的形象仍然没有出现〜 – NoobMe

+0

@NoobMe检查编辑1上面。 – Vikram

+0

你有什么想法,为什么我不能让我的图像出现在屏幕上? :) – NoobMe