2015-11-02 76 views
0

这里是我得到的错误行:的Android/Java的不兼容的类型

bucket.x = touchPos.x - 64/2;

下面是完整的代码块:

package com.badlogic.drop; 

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.audio.Music; 
import com.badlogic.gdx.audio.Sound; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.Vector3; 

import java.awt.Rectangle; 


public class Drop extends ApplicationAdapter { 
    private Texture dropImage; 
    private Texture bucketImage; 
    private Sound dropSound; 
    private Music rainMusic; 
    private OrthographicCamera camera; 
    private SpriteBatch batch; 
    private Rectangle bucket; 

    @Override 
    public void create() { 
     // load the images for the droplet and the bucket, 64x64 pixels each 
     dropImage = new Texture(Gdx.files.internal("droplet.png")); 
     bucketImage = new Texture(Gdx.files.internal("bucket.png")); 

     //instantiating the rectangle and specify its initial values 
     bucket = new Rectangle(); 
     bucket.x = 800/2 - 64/2; 
     bucket.y = 20; 
     bucket.width = 64; 
     bucket.height = 64; 

     // setting the camera to show an area of the game world that is 800x480 units wide 
     camera = new OrthographicCamera(); 
     camera.setToOrtho(false, 800, 480); 

     // creating the sprite batch 
     batch = new SpriteBatch(); 

     // load the drop sound effect and the rain background "music" 
     dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav")); 
     rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")); 

     // start the playback of the background music immediately 
     rainMusic.setLooping(true); 
     rainMusic.play(); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     // render the bucket 
     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     batch.draw(bucketImage, bucket.x, bucket.y); 
     batch.end(); 

     // telling the camera to make sure its updated 
     camera.update(); 

     // making the button move through touch 
     if(Gdx.input.isTouched()) { 
      Vector3 touchPos = new Vector3(); 
      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
      camera.unproject(touchPos); 
      bucket.x = touchPos.x - 64/2; 
     } 

     } 

    } 

我收到错误告诉我我有一个不兼容的类型:可能有损浮动转换为int

在此先感谢您的帮助,这是我第一次发布到stackoverflow,我希望我为如果您有任何其他问题让我知道,请将其正确地对准。 :)

+1

你有一个'float',你试图分配给'int'。这是什么让你感到困惑? – chrylis

+0

我只是困惑我将如何将浮点数转换为int以摆脱错误 – Derek

+0

@Derek http://stackoverflow.com/questions/1295424/how-to-convert-float-to-int-with -java –

回答

0

根据错误消息,您的touchPos.xfloat。编译器告诉你,当你试图将一个浮点值放入一个整数类型中时,你将失去小数部分(如果数字非常大,可能不适合),并且你必须明确地确定转换与演员阵容:

bucket.x = (int) (touchPos.x - 64)/2; 

请注意,你几乎肯定意味着上述版本与括号;标准操作顺序适用于Java。如果你没有,只需要减去32来代码就可以使你的代码更加清晰。

+0

感谢chrylis的帮助,如果我使用Math.random将它舍入到最接近的整数,那么它仍然可以工作吗? – Derek

+0

@Derek你的意思是'Math.round()'?当然,这会起作用,但要确保你不会意外推开屏幕的右侧/底部。 – chrylis

+0

oops是的,我的意思是Math.round()。好的,我也会记住这一点。 – Derek