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

2015/8/24

Problem with mathematical operations...

Vellyxenya Vellyxenya

2015/8/24

#
Hi, I started programming with Greenfoot some weeks ago and constantly have some difficulties, until now, i succeeded in resolving most of them, but now... I'm completely blocked... so I explain: I'm trying to add an Object at the coordinates (x and y). The point is that my x is given by an addition of two numbers (two doubles I think) and Greenfoot is unable to compile (it says: incompatible types: possible lossy conversion from double to int) Here's the part of the code that goes wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void laser()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int xMouse = mouse.getX();
        int yMouse = mouse.getY();
        double xThis = this.getExactX();
        double yThis = this.getExactY();
        double dx = xMouse-xThis;
        double dy = yMouse-yThis;
        double length = Math.sqrt(dx*dx+dy*dy);
        double xLength = dx/length;
        double yLength = dy/length;
         
        if(Greenfoot.mousePressed(null))
        {
            for(int i=0; i<length; i++)
            {
             Weapon1 weapon1 = new Weapon1();
             getWorld().addObject(weapon1, this.getX()+[b]i*xLength[/b], this.getExactY()+[b]i*yLength[/b]);
            }
        }
}
the part that is in bold is what's marked in red by Greenfoot Can someone help me with this please? :) PS: Sorry for my bad english, it's not my native language^^
danpost danpost

2015/8/25

#
The incompatible types can be removed by casting to the required type for the parameter. Since the x- and y-coordinates for where the actor is to be added into the world use 'int' types: (int)(this.getExactX()+(double)i*xLength) and (int)(this.getExactY()+(double)i*yLength) should be used.
Vellyxenya Vellyxenya

2015/8/27

#
Thank you very much :D
You need to login to post a reply.