I am trying to get one off my int
statements to take a float
statement because in my game, the sprite uses a float
to get the coords of where it is at. However the tilemap uses int
to get the location of the tile. Why cant you use an int
and float
in the same statement and if its an obvious answer how do you go about changing it without having to use casts in other methods?
Asked
Active
Viewed 496 times
-10

DrComputerScience
- 17
- 5
-
4A cast ***IS*** the method to change one numeric type to another. Why do you feel the need to avoid a cast? – Jim Garrison Feb 14 '17 at 17:18
-
@JimGarrison Because if i add a cast to my other objects, it would change the whole structure of the game. Or do you mean adding casts to the methods themselves? I tried doing so but it didnt work :/ – DrComputerScience Feb 14 '17 at 17:19
-
1I'm not sure you understand casts correctly or which of them you mean but you can cast right where you need it, e.g. `int fromTileX = tmap.getTileWidth((int)fromX);`. This wouldn't change the structure of the game at all. – Thomas Feb 14 '17 at 17:21
-
`tmap.getTileWidth(fromX);` <- why are you passing fromX into that function? Its name suggest that it is a getter....so in other words a method that gets a value. Having a parameter that you pass there seems very wierd. **Edit** Or is tmap a class by any chance and this is a static method call? – OH GOD SPIDERS Feb 14 '17 at 17:21
-
1@911DidBush you can have getters that take a parameter although the method name might be misleading. I assume it's meant to take the position as a parameter, locate the corresponding tile column and return the column's starting position. – Thomas Feb 14 '17 at 17:23
-
@Thomas I have tried this!! It just keeps saying this method is not applicable for the int and i dont know why :/ – DrComputerScience Feb 14 '17 at 17:24
-
@911DidBush The variable name `tmap` suggests that the parameter it takes is a key and it returns (i.e., "gets") an associated value for that key. – daiscog Feb 14 '17 at 17:24
-
3You might want to show the method signature and how you call it then. – Thomas Feb 14 '17 at 17:24
-
@911DidBush tmap is a variable in a method in a tilemap class, i am passing the value fromX because i want the tile to get the value of where the sprite is – DrComputerScience Feb 14 '17 at 17:25
-
What isn't applicable? Does the method expect an `int` parameter? Or does it return a `float` value? Either way, a cast will remove the compile error (although I'm not sure it will give you what you need as a result - as dsh's answer points out, you'll lose the fractional part of the float). – daiscog Feb 14 '17 at 17:26
-
@megaflop i have added more code, no it does not expect it. This is why i am stuck – DrComputerScience Feb 14 '17 at 17:29
-
Wow, it turns out @911DidBush was right all along - the `getTileWidth` method doesn't take a parameter. So why are you trying to pass it one? – daiscog Feb 14 '17 at 17:29
-
@Thomas I have added more code to see if it helps – DrComputerScience Feb 14 '17 at 17:29
-
1Please do not swear. In Java, you can only pass in parameters which the method has declared it expects. The `getTileWidth()` method has not been declared to accept any parameters. Look at the code for the method - all it does is return the value of the `tileWidth` member. It doesn't perform any calculations. – daiscog Feb 14 '17 at 17:35
-
3Why did you delete all the code? That way your question is quite hard to understand for future readers. Besides that, it should be obvious (at least from compiler errors) that you can't pass anything to a method that doesn't accept parameters. You couldn't even pass an `int`, i.e. the question isn't actually related to that problem. – Thomas Feb 15 '17 at 09:10
1 Answers
3
You can't assign a float
value to an int
variable because some of the information will be lost. For the same reason, you can't pass a float
value to a method whose argument is declared as an int
. (your question is rather unclear due to the incorrect use of terminology, but I think you are trying to do the latter)
You can tell the compiler that you really do want to lose the information (the fractional part of the float) by using a cast operation. That is the how, and the why.

dsh
- 12,037
- 3
- 33
- 51