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

2016/11/11

Greenfoot is reading setImage() but is not setting the image?

SloanTheSloth SloanTheSloth

2016/11/11

#
So, I am trying to make one of my actors change images when the mouse is hovering over it (whether or not it is moving). This is our code: if (mouse != null) { int mx = mouse.getX(); int my = mouse.getY(); if (mx > 978 && mx < 1042 && my > 151 && my < 251) { setImage(image2); } else { setImage(image1); } Those numbers are the x and y values of where the mouse is, we did this because mouseMoved() only checks if it is currently moving. Onto the problem, so, we tried it, and it doesnt work. Here is what happens in the debugger: It reads setImage to Image2, but it doesn't do it. at the top, image 2 is an instance variable: private GreenfootImage image2 = new GreenfootImage("OPENTrashcan.png"); Why wouldn't it switch to it?
danpost danpost

2016/11/11

#
It looks like your instruction pointer is at the 'setImage' line (where it has not executed yet). However, there is a very easy way to accomplish what you are trying:
if (getImage() != image2 && Greenfoot.mouseMoved(this)) setImage(image2);
if (getImage() != image1 && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) setImage(image1);
You need to login to post a reply.