2015-11-02 102 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