不确定的下降图像数量...在SurfaceView上显示帮助!

时间:2011-03-27 17:13:01

标签: android bitmap imageview surfaceview

我想我在这里找到了编码器块。我正在编写一个游戏,其中多个图像将从屏幕顶部落下,您必须在底部捕获它们。将有不确定数量的图像,因此您不知道在开始时间将有多少图像。对于我的生活,我无法弄清楚实现这一点的方法。我理解逻辑,但在通过Android的类运输时遇到了麻烦。我试图创建自己的“开花”对象,但每次我尝试编写与其中的位图有关的任何内容时,它都会变得怪异。也许我只是写错了,我不知道。所有这些都是在SurfaceView上执行的。有没有人对如何做到这一点有任何想法?这是我到目前为止的代码:

    public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
    Context mContext;


    Bitmap box = 
        (BitmapFactory.decodeResource
                (getResources(), R.drawable.box));

    Bitmap blossom = 
        (BitmapFactory.decodeResource
                (getResources(), R.drawable.blossom));

    private BoardThread thread;
    private float box_x = 140;
    private float box_y = 378;
    private float blossom_x = 0;
    private float blossom_y = 0;
    private float boxWidth = box.getWidth();
    private float boxHeight = box.getHeight();
    private float blossomWidth = blossom.getWidth();
    private float blossomHeight = blossom.getHeight();
    private Random generator = new Random();



    boolean mode = false;

    RectF boxRect = new RectF(box_x,box_y, box_x + boxWidth, box_y + boxHeight);
    //ImageView box_view;

    public BoardView(Context context){
        super(context);

        //surfaceHolder provides canvas that we draw on
        getHolder().addCallback(this);

        // controls drawings
        thread = new BoardThread(getHolder(),this);

        //draws the blossom at a random starting point
        blossom_x = generator.nextInt(300);


        //box_view = (ImageView)findViewById(R.id.box_view);
         //box_view.setVisibility(ImageView.VISIBLE);
         //TranslateAnimation anim = new TranslateAnimation(0,0,300,0);
         //anim.setDuration(500);
         //box_view.startAnimation(anim);

        //intercepts touch events
        setFocusable(true);

    }


    @Override

    public void onDraw(Canvas canvas){
        canvas.drawColor(Color.WHITE);  


        //draw box and set start location
        canvas.drawBitmap(box, box_x - (boxWidth/2), 
                box_y - (boxHeight/2), null);

            canvas.drawBitmap(blossom, blossom_x,
                blossom_y = blossom_y+3 , null);

        //collision detection, currently not working after 
            //implementing random draw
            if(blossom_y + blossomHeight == box_y)
        {
            canvas.drawBitmap(blossom, 40,100, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){

        if(event.getAction() == MotionEvent.ACTION_DOWN){
            if(boxRect.contains(event.getX(),event.getY())){
                mode = true;
            }
        }

        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            if(boxRect.contains(event.getX(),event.getY())){
                mode = true;
            }
            if(mode == true){
                box_x = (int)event.getX();
                boxRect.set(box_x,box_y, box_x + boxWidth, box_y + boxHeight);
            }

        }

        if(event.getAction() == MotionEvent.ACTION_UP){
            mode = false;
        }

        invalidate();

        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, 
            int format, int width, int height ){

}

    @Override
    public void surfaceCreated(SurfaceHolder holder){
        thread.startRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
        thread.startRunning(false);
        thread.stop();
    }
}

我目前正在使用位图绘制落花。我开始为花卉对象创建一个类,到目前为止看起来像这个

    public class Blossom{
    private Bitmap blossom;

    public Blossom()
    {
        Bitmap blossom = 
            (BitmapFactory.decodeResource
                    (getResources(), R.drawable.blossom));
    }
    public void setImage(Bitmap bitmap)
    {
        //sets the image for the blossom;
        Bitmap blossom = bitmap;
    }
}

它一直给我一个错误,说是对于Blossom类,未定义getResources。当我尝试编写一个名为setImage的方法,并在BoardView类中进行Image的设置时,它根本不会让我:(它看起来像这样

    public class Blossom{
    private Bitmap blossom;

    public void setImage(Bitmap bitmap)
    {
        //sets the image for the blossom;
        Bitmap blossom = bitmap;
    }
}

有人能看到这里出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

由于您的课程延伸SurfaceView,您应该getContext().getResources()

修改 - 如果您想在其他课程中使用getResources,那么您还需要传入Context。您可能只需传入Bitmap而不是尝试从该数据类中获取它。