Generate a circle randomly on the screen and make it green or red

后端 未结 1 1341
太阳男子
太阳男子 2021-01-24 15:17

So ive been trying to make a game app that either displays a red button with text or a green button with text randomly on the android screen. If anyone can help me with this i w

相关标签:
1条回答
  • 2021-01-24 15:44

    You only need one Random instance.

    Declare private long lastUpdated = 0; and private int lastColor = Color.BLACK; outside of the onDraw.

    Update the bottom portion to:

    final float radius = 230f;
    if(System.currentTimeMillis() > lastUpdated + 1000){
        lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
        lastUpdated = System.currentTimeMillis();
    }
    paint.setColor(lastColor);
    canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
    

    This will draw a circle of either red or green at random location every second.

    You need the radius/2 because the coordinates are from the center of the circle.

    As for your second part of your question, also on a side note i want to slowly generate faster cool upside. You'd have to clarify what you mean.

    Edit: Provided a more complete (and correct) sample here: https://gist.github.com/mshi/8287fd3956c9a917440d

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