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

2016/6/17

Casting int to double

gmacias gmacias

2016/6/17

#
Why doesn't this work? It works with 1.5 but not 0.5. Why?
1
2
3
4
5
6
7
8
9
10
11
12
public class Ani extends Actor
{
    private double go = 0.5;
    /**
     * Act - do whatever the ani wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       move((int)go);
    }   
}
davmac davmac

2016/6/17

#
You're casting a double to an int (integer). An integer must be a whole number, and the conversion rounds down. So 0.5 rounds down to 0, and doesn't move at all. 1.5 rounds down to 1, and moves 1 cell.
gmacias gmacias

2016/6/17

#
how do i make it move 0.5 pixels? is that even possible?
davmac davmac

2016/6/17

#
A pixel is the smallest visible unit on your screen, so no, it's not possible. If you want to move more slowly, that is possible. The SmoothMover class will do it for you (even simulating movement by less than whole pixels, by tracking position with double variables). Edit -> Import Class -> SmoothMover.
gmacias gmacias

2016/6/17

#
now I understand! Thank you.
You need to login to post a reply.