简体   繁体   English

Android / Java不兼容类型

[英]Android/Java incompatible types

Here's the line I'm getting the error on: 这是我遇到错误的那一行:

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

Here's the full code block: 这是完整的代码块:

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

        }

    }

The error I'm receiving is telling me I have an incompatible types: Possible lossy conversion from float to int 我收到的错误告诉我我有一个不兼容的类型:可能从float到int的有损转换

Thanks in advance for the help, this is my first time posting to stackoverflow, I hope I formatted this correctly if you have any other questions let me know. 在此先感谢您的帮助,这是我第一次发布至stackoverflow,如果您有其他问题要告诉我,我希望我可以正确设置格式。 :) :)

Based on the error message, your touchPos.x is a float . 根据错误消息,您的touchPos.xfloat The compiler is telling you that when you try to put a floating-point value into an integer type, you will lose the fractional part (and if the number is really big, it might not fit), and you have to explicitly okay the conversion with a cast: 编译器告诉您,当您尝试将浮点值放入整数类型时,您将丢失小数部分(如果该数字确实很大,则可能不合适),并且您必须明确地同意转换与演员:

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

Note also that you almost certainly meant the above version with parentheses; 还要注意,您几乎可以肯定上述版本用括号括起来; standard order of operations applies in Java. 标准操作顺序适用于Java。 If you didn't, make your code clearer by just subtracting 32 instead. 如果您没有这样做,则只需减去32即可使代码更清晰。

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

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