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

2014/1/29

incompatible types

Rudolf Rudolf

2014/1/29

#
Hello i´m new here and dont´t understand why i´ve got this error "incompatible types. Please help me :( Thats my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Start extends Actor { public boolean isSelected=false; public void act() { if (Greenfoot.mousePressed(this)){ MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse != null) { setLocation (mouse.getX(), mouse.getY()); } } } }
danpost danpost

2014/1/29

#
There does not appear to be anything wrong with the code as it is shown (other than the underline tags being thrown in there).
davmac davmac

2014/1/30

#
Rudolf, please use code tags, indent your code properly, and say which line you get the error on.
Rudolf Rudolf

2014/2/2

#
Thats my Code. I got the error in Line 10 (Greenfoot.getMouseInfo();
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 

public class Start extends Actor 
{ 
    public boolean isSelected=false; 
    public void act() 
    { 
        if (Greenfoot.mousePressed(this))
        { 
            MouseInfo mouse = Greenfoot.getMouseInfo(); 
            if(mouse != null) 
            { 
                setLocation (mouse.getX(), mouse.getY()); 
            } 
        } 
    } 
}
danpost danpost

2014/2/2

#
Again, I do not see anything wrong with the code. However, since you are detecting a mouse action with 'mousePressed', there should be no need to check if the MouseInfo object returned is not 'null'. It is never null when a mouse action is detected. Therefore, equivalent code would be:
import greenfoot.*;

public class Start extends Actor
{
    public boolean isSelected = false;

    public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
    }
}
Try a select-all/delete on your class code and replace it with the above by copy/paste operations and try again. If that does not help, try removing the class and adding it back in with the above code. Either way, post back with results.
Rudolf Rudolf

2014/2/2

#
I dont´t know how to build the code to drad&drop a copy of an Actor out of the selection area(on the World) to another place where I drag the Actor. That´s why I´m building this Code. Is this the wrong way?
Rudolf Rudolf

2014/2/2

#
Sorry, the same error. How could I put some screenshot´s in here?
danpost danpost

2014/2/3

#
Rudolf wrote...
I dont´t know how to build the code to drad&drop a copy of an Actor out of the selection area(on the World) to another place where I drag the Actor. That´s why I´m building this Code. Is this the wrong way?
Well, 'mousePressed' is only the first step. It only returns true at the moment the state of a mouse button changes from an unpressed state to a pressed state. It is not true at any other time. The next step is using 'mouseDragged' (to keep the actor at the mouse location) and, if needed, a combination of 'mouseClicked' (in case the mouse is not moved) and 'mouseDragEnded' (for when the mouse has moved). You may even get away with not even using the code you provided above as re-setting the location is only needed to be done during the mouse drag event. In other words, you can go ahead an use the code you have, but change 'mousePressed(this)' to 'mouseDragged(this)'.
davmac davmac

2014/2/3

#
Sorry, the same error.
Is it possible that you have a 'MouseInfo' class of your own in the scenario? If so, can you remove it? Or otherwise change the line to read:
            greenfoot.MouseInfo mouse = Greenfoot.getMouseInfo();
You need to login to post a reply.