Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序随机颜色和计时器_Java_Android_Random_Colors_Timer - Fatal编程技术网

Java Android应用程序随机颜色和计时器

Java Android应用程序随机颜色和计时器,java,android,random,colors,timer,Java,Android,Random,Colors,Timer,我有一个应用程序,它有一个正方形,每1.5秒在屏幕上移动一次,每次你点击它,你就会得到一分。我试着让每点击一次方块,颜色就变成随机的颜色。另外,我想在“设置”下有一个选项,用于更硬的模式,其中正方形每0.7秒移动一次 以下是我的画法: protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLUE); Paint dotPaint = new Paint(); canvas.drawRect(dotX,

我有一个应用程序,它有一个正方形,每1.5秒在屏幕上移动一次,每次你点击它,你就会得到一分。我试着让每点击一次方块,颜色就变成随机的颜色。另外,我想在“设置”下有一个选项,用于更硬的模式,其中正方形每0.7秒移动一次

以下是我的画法:

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

public boolean onTouch(View v, MotionEvent event) {
if (detectHit( (int)event.getX(), (int)event.getY() )) {
    score++;
    invalidate();
}
        return false;
我不太确定如何在每次点击时改变正方形的颜色

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);
}

好的,只是一个建议,将DetectThits()方法从(int…,int…)更改为(MotionEvent…)可以缩短调用

对于随机颜色,您必须通过编程方式生成一种。 这里有一个
Color.rgb(int-red,int-green,int-blue)
方法,您可能需要使用它

现在我们要做的是,生成随机整数来生成一种颜色,如下所示

Random rnd = new Random();
Color rndCol = Color.rgb(rnd.nextInt(254), rnd.nextInt(254), rnd.nextInt(254));
现在,您将有ti应用该颜色。 首先,它不鼓励在onDraw期间分配对象,因此将绘制的分配移动到onCreate/onResume,并使其成为过程中的一个字段。然后你必须像这样调整你的方法

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
}
在将速度从1.5更改为0.7的过程中,将计时设置为类中的一个字段,并在某个点上减少该值以减少计时器


由于您没有显示当前如何每1.5秒移动一次正方形,我无法为您调整代码。

因此,我想我已经回答了您的大部分问题。但你仍然需要处理移动速度加快的问题。我假设计时器正在运行某个任务,强制ui每1.5秒重新绘制一次。所以把它改成.700应该可以做一些技巧了,我现在理解了随机颜色,只是它说dotPaint不能被解析,下面是我如何每1.5秒移动一次正方形。定时器=新定时器();任务=新的DotsMasherItemTask(画布);时间表(任务,0,1500);是的,它无法解决,因为我猜你没有阅读文本:)你必须在你的类中将dotPaint设置为一个字段,否则你无法访问它这应该会告诉你如何改变时间,哈哈,是的,我在发表评论后就知道了。