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

2017/3/8

need help with shooting

FutureCoder FutureCoder

2017/3/8

#
i have a character, and when i press the mouse, a bullet is added into the world and this is the code. if(turn == 0) { turnTowards(m.getX(), m.getY()); turn = turn +1; } if(turn > 1) { move(12); } this makes the bullet turn towards the mouse, then move forward. But something weird keeps happening is that the bullet doesn't completely turn towards the mouse, it's off by a lot, and when i press underneath the character, it moves really slowly. can someone please help me i have no idea why it's doing this.
Super_Hippo Super_Hippo

2017/3/8

#
Well, this can't really be the whole code. If 'turn' starts with 0, it will be set to 1 and will never be greater than 1. However, if you want to shoot the bullet to the direction where the mouse is right now and not following it (I guess you try to do it with the 'turn' variable), you should set its rotation right after you add it to the world and then just move straight forward. No need for the turn field then.
FutureCoder FutureCoder

2017/3/8

#
i tried, but it does the same thing, what code should i use to get it to work
Super_Hippo Super_Hippo

2017/3/8

#
Add the bullet like this:
MouseInfo m = getMouseInfo();
Actor b = new Bullet();
getWorld().addObject(b, getX(), getY());
b.turnTowards(m.getX(), m.getY());
And then, in the bullet's act method, do not turn anymore.
FutureCoder FutureCoder

2017/3/9

#
thanks a lot, this really helps
You need to login to post a reply.