简体   繁体   English

Java Android App随机颜色和计时器

[英]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. 我有一个带有正方形的应用程序,该正方形每1.5秒在屏幕上移动一次,每次单击它都会得到一个积分。 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. 另外,我想在设置下选择一个更硬的模式,其中正方形每0.7秒移动一次。

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: 这是我的onTouch方法:

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. 好吧,只是一个建议,将您的detectHits()方法从(int ...,int ...)更改为(MotionEvent ...)可以缩短通话时间。

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. 您可能想使用此Color.rgb(int red, int green, int blue)方法。

So now what you do is, you generate Random ints to generate a Color, like this 所以现在您要做的是,生成Random int以生成Color,就像这样

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. 首先,它不鼓励在onDraw期间分配对象,因此将Paint的分配移动到onCreate / onResume并将其作为过程中的一个字段。 Then you will have to adapt your onTouchMethod like this. 然后,您将必须像这样调整您的onTouchMethod。

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. 在将速度从1.5更改为0.7的过程中,将计时作为类中的一个字段,并在某个点减小此值以减少计时器。

As you didnt show how you are currently moving the square every 1.5 seconds i cant adapt the code for you. 正如您没有显示您当前如何每1.5秒移动一次方块,我无法为您修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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