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

2016/8/12

aim at mouse

christ christ

2016/8/12

#
I am trying to make the actor face the mouse pointer by turnTowards mouse pointer to aim when the mouse pointer is moved. However the actor only rotates and points at the mouse pointer when the mouse pointer is moving, if the mouse pointer stops moving then the actor returns to its default position, I am basically trying remake diep.io, if you've heard of it, pls reply as soon as possible. Here is the code sofare: import greenfoot.*; public class tank extends Actor { public void act() { setRotation(90); if (Greenfoot.isKeyDown("w")){ move(-2); } if (Greenfoot.isKeyDown("s")){ move(2); } setRotation(0); if (Greenfoot.isKeyDown("d")){ move(2); } if (Greenfoot.isKeyDown("a")){ move(-2); } if(Greenfoot.mouseMoved(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); turnTowards(mouse.getX(),mouse.getY()); } } }
danpost danpost

2016/8/12

#
Do this:
1
2
3
4
5
// first line in act method
int angle = getRotation();
 
// line before 'if(Greenfoot.mouseMoved(null))'
setRotation(angle);
christ christ

2016/8/12

#
thank you so much it worked :-) ive got the shooting and the movement all correct now I need to figure out how to make the background scroll as the character moves and I have no clue, could you help me in any way?
danpost danpost

2016/8/12

#
christ wrote...
I need to figure out how to make the background scroll as the character moves and I have no clue, could you help me in any way?
See this discussion.
You need to login to post a reply.