尝试获取在屏幕上绘制的每个随机圆的x,y坐标 [英] Trying to get the x, y coordinate of each random circle that is drawn on the screen

查看:140
本文介绍了尝试获取在屏幕上绘制的每个随机圆的x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您制作的游戏会在屏幕上创建随机圆圈。随机创建的圆的值为红色或绿色。我的问题是,我想要能够不仅确定用户点击其中一个圈子,但确定他们最终点击什么圈子(红色或绿色)。这里是我的代码下面。我的主要问题是试图找到要绘制的圆的x和y

Hi im making a game that will create random circles on the screen. The circles created randomly will have the value red or green. My question is that i would like to be able to not only determine when a user clicks on one of the circles but determine what circle they have ultimately clicked on (red or green). Here is my code below. My main problem is trying to find the x and y of the circle that will be drawn

public class DrawingView extends View {



    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
    int randomHeight = (random.nextInt((int) Math.abs((getHeight()-radius/2 + radius/2f))));


    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);

        }
    };



    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(updateCircle);
    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // your other stuff here

        canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
    }



    @Override
    public boolean onTouchEvent (MotionEvent event) {

        double r = Math.sqrt(((randomWidth^2)+(randomHeight^2)));
        int maxX = (int) (((randomWidth)*(randomWidth)) + r);
        int minX = (int) ((((randomWidth)*(randomWidth))) - r);
        int maxY = (int) (((randomHeight)*(randomHeight)) + r);
        int minY = (int) ((((randomHeight)*(randomHeight))) - r);

        public int xCoordinateOfRedColor(){
            if(redColor == lastColor){
                if(randomWidth > maxX && < minX){
                    event.getX();
                }
            };
        }
        public int yCoordinateOfRedColor(){
            if(redColor == lastColor){
                if(randomHeight > maxY && < minY){
                    event.getX();
                }
            };
        }
        if(redColor == lastColor){
            if(randomHeight > maxY && < minY){
                event.getY();
            }
        };
        if(greenColor == lastColor){

        };
        if(greenColor == lastColor){

        };

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN : 
                randomWidth = (int) event.getX();
                randomHeight = (int) event.getY();

                invalidate(); 
                break;
            case MotionEvent.ACTION_POINTER_UP :


                break;
        }

        return true;

    }


}

我不知道如果im关闭还是真的很远。

I dont know if im close or really far away.

推荐答案

有很多方法来解决这种问题。你可以驱动自己坚果只是想列出他们所有。最强大的之一是在创建时为每个形状分配唯一的颜色。使用该颜色找到形状。换句话说...

There are many ways to solve this kind of problem. You can drive yourself nuts just trying to list them all. One of the most powerful is to assign each shape a unique color when it is created. Use that color to find the shape. In other words...

当他们点击这个:

取样颜色:

>

将这些唯一颜色的hashmap保存到形状对象引用。让所有的形状在一个隐藏的位图上绘制他们独特的颜色,你从来没有在屏幕上。当用户单击时,在隐藏的位图上查找该x的颜色。通过hashmap运行那个颜色,你有一个对他们点击的形状的引用。现在你不需要担心重叠,甚至使用不同的形状,而不是圆。这不仅给你的形状的x和y,但你知道关于那个形状的一切。

Keep a hashmap from these unique colors to the shapes object reference. Have all the shapes paint their unique color on a hidden bitmap that you never put on the screen. When the user clicks, look up the color at that x y on the hidden bitmap. Run that color thru the hashmap and you have a reference to the shape they clicked on. Now you don't need to worry about overlaps or even using different shapes than circles. This gives you not only the x and y of the shape but everything you know about that shape.

当然,为了这个工作,你必须为形状自己做类。我使独特的颜色非常不同为说明的目的。在现实中,你可以只有一个int,你增加。这可以通过构造函数增加,或者更好地有一个工厂方法注入它。

Of course for this to work you have to make your own class for the shapes. I made the unique colors very distinct for illustration purposes. In reality you could just have an int that you increment. This could either be incremented by the constructor or better yet have a factory method inject it.

这足够强大,即使在渲染3D时也能正常工作。只保留可见和隐藏的位图同步。原来是很容易,因为你只是重复使用相同的渲染代码。循环每个形状,并要求它绘制自己,用它的独特的颜色,在一个位图你给它。只要没有会改变颜色(阴影,照明,光线跟踪)的附加效果,就可以工作。

This is powerful enough that it works even when rendering 3D. Just keep the visible and hidden bitmaps in sync. That turns out to be easy since you just reuse the same rendering code. Loop each shape and ask it to paint itself, with it's unique color, on a bitmap you hand to it. Works as long as there are no additional effects that would change the color (shading, lighting, ray tracing). Make sure to turn those off.

顺便说一句,我不这么认为:

By the way, I don't think this:

if(randomHeight > maxY && < minY){

做你认为它做的。当我想测试两个值之间的东西时,我使用:

does what you think it does. When I want to test that something is between two values I use:

if(minY <= randomHeight && randomHeight <= maxY){

,因为它提醒我数学不等式3≤x≤15

because it reminds me of mathematical inequalities like 3 ≤ x ≤ 15.

这篇关于尝试获取在屏幕上绘制的每个随机圆的x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆