這裏是我得到的錯誤行:的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,我希望我爲如果您有任何其他問題讓我知道,請將其正確地對準。 :)
你有一個'float',你試圖分配給'int'。這是什麼讓你感到困惑? – chrylis
我只是困惑我將如何將浮點數轉換爲int以擺脫錯誤 – Derek
@Derek http://stackoverflow.com/questions/1295424/how-to-convert-float-to-int-with -java –