This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alexkr
Alexkr wrote ...

2019/10/25

Incompatible types: possible lossy conversion from float to int

Alexkr Alexkr

2019/10/25

#
public class avoiderWorld extends World { //Sound and image variables: public GreenfootImage LevelUp; private GreenfootSound Megalovania; //Counter private Counter scoreBoard; //Enemy variables private int enemy1SpawnRate = 20; private int enemy2SpawnRate = 20; private int enemy1Speed = 1; private int enemy2Speed = 1; //Levels variables private int Level1 = 25; private int Level2 = 25; private int Level3 = 25; private float enemyIncreaseSpeedlevel1 = 0.25f; private float enemyIncreaseSpeedlevel2 = 0.25f; private float enemyIncreaseSpeedlevel3 = 0.25f; public avoiderWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1, false); Megalovania = new GreenfootSound("sounds/megalovania.wav"); Megalovania.playLoop(); prepare(); } private void prepare() { avatar avatar = new avatar(); addObject(avatar,304,359); avatar.setLocation(306,353); scoreBoard = new Counter("Score: "); addObject(scoreBoard, 70, 20); } public void act() { //randomly add enemies to the world if(Greenfoot.getRandomNumber(1500) < enemy1SpawnRate) { enemy1 e1 = new enemy1(); e1.setSpeed(enemy1Speed); addObject(e1, Greenfoot.getRandomNumber(getWidth()-20)+10, -30); //increase points for each enemy spawned scoreBoard.setValue(scoreBoard.getValue() + 1); } if(Greenfoot.getRandomNumber(1500) < enemy2SpawnRate) { enemy2 e2 = new enemy2(); e2.setSpeed(enemy2Speed); addObject(e2, Greenfoot.getRandomNumber(getWidth()-20)+10, -30); //increase points for each enemy scoreBoard.setValue(scoreBoard.getValue() +1); } increaseLevel(); } private void increaseLevel() { int score = scoreBoard.getValue(); if( score > Level1) { enemy1SpawnRate = enemy1SpawnRate +1; enemy1Speed = enemy1Speed + enemyIncreaseSpeedlevel1; Level1 += 100; //Use can you showText() method to create text for level counter } if(score > Level2) { enemy1SpawnRate = enemy1SpawnRate +2; enemy1Speed = enemy1Speed + enemyIncreaseSpeedlevel2; Level2 += 200; } if(score >Level3) { enemy1SpawnRate = enemy1SpawnRate +2; enemy1Speed = enemy1Speed + enemyIncreaseSpeedlevel3; Level3 += 300; } } public void endGame() { AvoiderWorldGameOverScreen go = new AvoiderWorldGameOverScreen(); Greenfoot.setWorld(go); Megalovania.stop(); }
Alexkr Alexkr

2019/10/25

#
The problem is that it gives me this error when I try to set the variable speed as float: Incompatible types: possible lossy conversion from float to int
Alexkr Alexkr

2019/10/25

#
this is the enemy class: public class enemy1 extends Actor { private int speed; /** * Act - do whatever the enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY() + speed); checkRemove(); turn(5); } public void setSpeed( int s) { speed = s; } private void checkRemove() { World w = getWorld(); if(getY() > w.getHeight() + 30) { w.removeObject(this); } }
danpost danpost

2019/10/25

#
Alexkr wrote...
<< Code Omitted >>The problem is that it gives me this error when I try to set the variable speed as float: Incompatible types: possible lossy conversion from float to int << Code Omitted >>
You cannot expect that your enemy speeds, stored in int fields to be increased by fractional amounts. For one (of several) example, you have the following 3 lines:
private int enemy1Speed = 1;
//
private float enemyIncreaseSpeedlevel1 = 0.25f;
// and
enemy1Speed = enemy1Speed + enemyIncreaseSpeedlevel1;
An int field cannot hold a fourth of a unit.
Alexkr Alexkr

2019/10/25

#
I changed it and it still gives me the same error.
danpost danpost

2019/10/25

#
Alexkr wrote...
I changed it and it still gives me the same error.
What all did you change?
Alexkr Alexkr

2019/10/25

#
This is my actual code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class avoiderWorld extends World { //Sound and image variables: public GreenfootImage LevelUp; private GreenfootSound Megalovania; //Counter private Counter scoreBoard; //Enemy variables private int enemy1SpawnRate = 20; private int enemy2SpawnRate = 20; private float enemy1Speed = 1.00f; private float enemy2Speed = 1.00f; private int enemyevadedscore = 1; private int enemy2evadedscore =1; //Levels variables private int Level1 = 25; private int Level2 = 25; private int Level3 = 25; private float enemy1IncreaseSpeedlevel1 = 0.25f; private float enemy1IncreaseSpeedlevel2 = 0.25f; private float enemy1IncreaseSpeedlevel3 = 0.25f; private float enemy1Speed1; private float enemy1Speed2; private float enemy1Speed3; public avoiderWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1, false); Megalovania = new GreenfootSound("sounds/megalovania.wav"); Megalovania.playLoop(); prepare(); } private void prepare() { avatar avatar = new avatar(); addObject(avatar,304,359); avatar.setLocation(306,353); scoreBoard = new Counter("Score: "); addObject(scoreBoard, 70, 20); } public void act() { //randomly add enemies to the world if(Greenfoot.getRandomNumber(1500) < enemy1SpawnRate) { enemy1 e1 = new enemy1(); e1.setSpeed(enemy1Speed); addObject(e1, Greenfoot.getRandomNumber(getWidth()-20)+10, -30); //increase points for each enemy spawned scoreBoard.setValue(scoreBoard.getValue() + enemyevadedscore); } if(Greenfoot.getRandomNumber(1500) < enemy2SpawnRate) { enemy2 e2 = new enemy2(); e2.setSpeed(enemy2Speed); addObject(e2, Greenfoot.getRandomNumber(getWidth()-20)+10, -30); //increase points for each enemy scoreBoard.setValue(scoreBoard.getValue() + enemy2evadedscore); } increaseLevel(); } private void increaseLevel() { int score = scoreBoard.getValue(); if( score > Level1) { enemy1SpawnRate = enemy1SpawnRate +1; enemy1Speed1 = enemy1Speed + enemy1IncreaseSpeedlevel1; Level1 += 100; //Use can you showText() method to create text for level counter } if(score > Level2) { enemy1SpawnRate = enemy1SpawnRate +2; enemy1Speed2 = enemy1Speed1 + enemy1IncreaseSpeedlevel2; Level2 += 200; } if(score >Level3) { enemy1SpawnRate = enemy1SpawnRate +2; enemy1Speed3 = enemy1Speed + enemy1IncreaseSpeedlevel3; Level3 += 300; } } public void endGame() { AvoiderWorldGameOverScreen go = new AvoiderWorldGameOverScreen(); Greenfoot.setWorld(go); Megalovania.stop(); } }
danpost danpost

2019/10/25

#
Alexkr wrote...
This is my actual code: << Code Omitted >>
If that is all you changed, then no wonder you still have issues. You are now (trying to) passing a float value to a method that has an int parameter (to set speed of enemies). Use e#.setSpeed((int)enemy#Speed); in act method.
You need to login to post a reply.