creating a random circle that can be clicked on like a button

后端 未结 2 1447
一个人的身影
一个人的身影 2021-01-27 08:02

so here is my predicament i cannot seem to find this anywhere on the internet. My question is simple. I am creating a game app for android. The game will generate random circles

相关标签:
2条回答
  • 2021-01-27 08:36

    from what i've understood you are trying to make circles that you are drawing randomly clickable by the user, well this is pretty easy actually first you have to save the random position of the new circle in an integer variable then make a condition that will be controlled through an onToutchEvent

    @Override
        public boolean onTouchEvent (MotionEvent event) {
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN : 
                    xPos = event.getX();
                    yPos = event.getY();
                    invalidate(); 
                    break;
            }
    
            return true;
    
        }
    
    0 讨论(0)
  • 2021-01-27 08:59

    Conceptually, you do not have a difficult problem. You are drawing a circle on a screen. You want to know if the user "touched" it.

    You need to capture the touch event (i.e. "onTouchEvent") and then get the X,Y coordinates of the event. It looks like you know where the circle is on the screen, which has a center and radius. You just need to determine if the touch event occurred within the circle.

    The only odd thing here is that usually when an object is "touched" it processes the touch event. In your case, you have a drawing inside the container that is an indicator to the user of what will "process" the event. And you have to do the calculation to determine if the event was successful.

    This is a high-level explanation because you have asked a fairly high level question. You need to post more code and ask more specific questions to get a lower level answer at this point.

    EDIT:

    You've modified your code to include your onTouchEvent - that tells you where the user touched the screen. You need to do the math in that function to determine if the touch event occurred "inside" the circle.

    Since you have the location and radius of the circle, you should be able to use algebra to determine the border (like you know the center, so you can see if the touch event occurred with an X,Y position that is inside the radius)

    First, think in quadrants. Is the touch event X value greater than or less than the center X. And then the same with the Y axis. Then determine if the touch event occurred inside the radius by calculating the greatest/least X value and Y value in that quadrant.

    That should help ... sorry it wasn't sooner.

    EDIT 2:

    Radius is the distance from the center - so, if you know the center, then you can determine the radius. To determine if the X.Y of the touch event is "inside" the radius, then calculate the Y value from the X, and the X value from the Y...

    In other words, you should know that r = sqrt(x^2 + y^2) right? So, if the center of the circle is at (XX, YY) then remember that MAX(X value) = XX + r and MIN(X value) = XX - r (accounting for the edge of the screen).

    So, if Ye (Y value of the event) is greater than YY-r but less than YY+r, then it could be within the radius of the circle. You can check the X values to see if they are also within the circle...

    Is that enough guidance? I'm trying to help you understand - but this is a very complex (yet "basic") concept. If you can get this, then a lot of other things will become more intuitive..

    0 讨论(0)
提交回复
热议问题