0

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

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. :)

Derek
  • 1
  • 1

1 Answers1

0

Based on the error message, your touchPos.x is a float. 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. If you didn't, make your code clearer by just subtracting 32 instead.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152