简体   繁体   中英

Java Android App Random Color and Timer

I have an app that has a square that moves around the screen every 1.5 seconds and every time you click it you get a point. I'm trying to make it where every you click the square the color changes to a random color. Also, I want to have an option under settings for a harder mode where the square moves every .7 seconds.

Here is my draw method:

protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
Paint dotPaint = new Paint();       
    canvas.drawRect(dotX, dotY, dotX + 60, dotY + 60, dotPaint);
dotPaint.setColor(Color.WHITE);
dotPaint.setTextSize(60); 
    canvas.drawText("Score: " + score, 20, 60, dotPaint);                                           

and here is my onTouch method:

public boolean onTouch(View v, MotionEvent event) {
if (detectHit( (int)event.getX(), (int)event.getY() )) {
    score++;
    invalidate();
}
        return false;

I'm not too sure how to go about making the color of the square change every click.

Also, here is my menu items:

public boolean onOptionsItemSelected(MenuItem item) {
    // handle menu item selection
    switch (item.getItemId()){
        case R.id.item1:
            newGame();
            return true;
        case R.id.item2:
            quit();
            return true;
        case R.id.item3:
            harder();
            return true;
        default: 
            return super.onOptionsItemSelected(item);

and my harder method:
public void harder(){
    timer.schedule(task, 0, 700);
}

Well just a suggestion, change your detectHits() Method from (int ... , int ...) to (MotionEvent ...) shortens the call.

For a Random color you will habe to generate one programmatically. There is this Color.rgb(int red, int green, int blue) Method, which you might want to use.

So now what you do is, you generate Random ints to generate a Color, like this

Random rnd = new Random();
Color rndCol = Color.rgb(rnd.nextInt(254), rnd.nextInt(254), rnd.nextInt(254));

now you will have ti apply that color. First of all its discourage to allocate objects during onDraw, so move that allocation of Paint to onCreate/onResume and make it a field in the process. Then you will have to adapt your onTouchMethod like this.

public boolean onTouch(View v, MotionEvent event) {
if (detectHit(event)) {
    changeColor();
    score++;
    invalidate();
}
return false;

private void changeColor(){
   Random rnd = new Random();
   Color rndCol = Color.rgb(rnd.nextInt(254), rnd.nextInt(254), rnd.nextInt(254));
   dotPaint.setColor(rndCol); // this should be a field by now and accessible from within this method
}

in the matter of changing the speed from 1.5 to 0.7 make the timing a field in the class, and at a certain point reduce the this value to reduce the timer.

As you didnt show how you are currently moving the square every 1.5 seconds i cant adapt the code for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM