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

2011/8/16

OnmouseOver... again.

1
2
juanchi_kiuxus juanchi_kiuxus

2011/8/16

#
Hi there, I'm having problems trying to simulate the "OnmouseOver" event. I'd like when I put my mouse over an actor that the image change. I have tried a couple of things: 1º In the Actor part: public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); Actor act = mouse.getActor(); if (act.equals(this) == true){ System.out.println("I'm over my actor"); } } But I get java.lang.NullPointerException 2º In the Actor part: if (Greenfoot.mouseMoved(this) == true){ setImage("boton_gestion_registros_resaltado.png"); }else{ setImage("boton_gestion_registros.png"); } But I don't have the correct effect (just change the image if i`m moving the mouse all the time over the actor...). Any idea?? I¡m learning and it's hard at the first... :-) Thanks, Juan.
danpost danpost

2011/8/16

#
In your first attempt, try 'this.equals(act)' instead of 'act.equals(this)'. I think (because you know there is a 'this') that will eliminate the NullPointerException (unless something else is triggering it). Also '== true' is not needed, as the 'equals(object)' method will return a boolean (true/false) value.
Busch2207 Busch2207

2011/8/17

#
Try this:
    private boolean onThis=false;

    public void act()
    {
        if(Greenfoot.mouseMoved(null))
            onThis = Greenfoot.mouseMoved(this);
        if(onThis)
            setImage("boton_gestion_registros_resaltado.png"); 
        else
            setImage("boton_gestion_registros.png"); 
    }
juanchi_kiuxus juanchi_kiuxus

2011/8/17

#
Thanks for the reply danpost,. I realise that the NullPointerException is caused for that line: public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); Actor act = mouse.getActor(); --> Exception if (this.equals(act)){ System.out.println("Estoy encima de record"); } } There are any way to correct this mistake? Thanks, Juan.
juanchi_kiuxus juanchi_kiuxus

2011/8/17

#
Thanks for the reply Busch2207, your code works perfectly!! Regards, Juan.
danpost danpost

2011/8/21

#
@Busch2207 -- Yes, the code works perfectly, as written above. I have a slightly different problem -- I have the good code in one Actor class working fine, but I also have the same code in another Actor class that does not seem to be working at all. The only difference is, though it is in the act() method, it IS within a 'while (true)' loop. Would that prevent any new mouse movement (or action, which I did not check on) to be detected? In other words, after the first time through the loop, does it from there on return 'null' on 'Greenfoot.mouseMoved(this);'? And whether that be the case or not, what code would I use within a 'while (true)' loop to track mouse action?
mjrb4 mjrb4

2011/8/22

#
In short you wouldn't use a while(true) at all - you'd just put the if statement directly in the body of the act() method. The act method of each actor in the world (and the world itself) is called over and over again, the speed depending on the speed slider. However, there isn't any mechanism in place to yank execution from one act method if it takes to long to execute, neither do the act methods run in separate threads concurrently (very deliberately!) If you stick a while(true) in the act method and that corresponding actor is in the world, then it'll just block forever as soon as it hits the loop.
danpost danpost

2011/8/22

#
This object, when entered into the world, is to be interacted with immediately; that is, all other action will be suspended until either 'ENTER' or 'ESCAPE' is pressed or the object is clicked. Then.it will be removed from the world and a 'return' statement will be executed immediately.
danpost danpost

2011/8/23

#
I just wanted to know if there was code to cover this situation. I do know of a way to do what I want without this precise code (i.e. create an transparent object to cover the world that has no act() method code, and put my object over it; therefore anything that is currently happening will continue to happen, but cannot be interacted with until my object is dealt with and the world cover is removed. Doing this would eliminate the need for the 'while (true)' loop.).
mjrb4 mjrb4

2011/8/23

#
Aisde from the method you describe, I think you'd have to implement that functionality separately in each of the other actors. You could create an interface, say Pausable, and have all your actors implement it in some way, so that when a "pause()" method is called nothing takes place in the act method (this could be done by an unpause() method or a boolean flag set on the first method.) This would allow you to get all the actors in the world, cast them all to Pausable and then do what you needed to do. Even in the situation you describe above the while(true) won't work, because everything in the scenario is done on the same thread, including the updates to the mouse methods (ie determining whether the mouse was moved or not.) If you block the thread from exiting a certain act method as you would with a while(true) then you'll never be able to update any mouse conditions to break out of the loop.
danpost danpost

2011/8/23

#
mjrb4 wrote...
everything in the scenario is done on the same thread, including the updates to the mouse methods
Thank you mjrb4, that answers my question about code within the loop. As far as creating a Pausable interface, I think it much easier to cover the world with a non-acting transparent object or just make my object itself cover the whole world. Thanks again.
mjrb4 mjrb4

2011/8/24

#
As far as creating a Pausable interface, I think it much easier to cover the world with a non-acting transparent object or just make my object itself cover the whole world. Thanks again. As far as I can see they should achieve the same result. From a technical perspective I personally think the Pausable interface is a bit neater, and it'll give you more control over exactly what parts of the behaviour you want to pause (whereas covering it with a transparent image will just prevent interaction.) At the end of the day though, just covering the world is a simple solution and if it fits what you need to do there's nothing wrong with it!
danpost danpost

2011/8/24

#
It fits. Thanks mjrb4.
PStigerID PStigerID

2012/5/4

#
I have one question here - I tried using this code, but it isnt working. I am getting a nullPointer.
    private boolean onThis=false;  

    public f()
    {
        if(level.l6 == 0)
            setImage("6l.png");
        if(level.l6 == 1)
            setImage("6.png ");
    }

    public void act() 
    {
        if(level.l6 == 0)
        {
            if(Greenfoot.mouseMoved(null))  
                onThis = Greenfoot.mouseMoved(this);  
            if(onThis)
                getWorld().addObject(new m("level 5"),Greenfoot.getMouseInfo().getX(),Greenfoot.getMouseInfo().getY());

        }
        if(level.l6 == 1)
        {
            if(Greenfoot.mouseClicked(this)) 
                Greenfoot.setWorld(new L6());
        }
    }  
Actor m:
    public m(String a)
    {
        setImage(new GreenfootImage("You must play " + a + " first!" , 15, Color.WHITE, Color.BLACK));
    }
Also: I want it to go away as soon as it leaves this. Thx PStiger
Busch2207 Busch2207

2012/5/4

#
Your mistake is in line 18. Greenfoot.getMouseInfo() can although return null. and you can't call a method of a null type... to avoid it, try to save the x and y locations in two different variables. e.g.:
    private int i_MouseX,i_MouseY;

    public void act()
    {
        MouseInfo MI_ = Greenfoot.getMouseInfo();
        if(MI_!=null)
        {
            i_MouseX=MI_.getX();
            i_MouseY=MI_.getY();
        }
    }
There are more replies on the next page.
1
2