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

2017/2/20

Projectile problem

joshuadswa joshuadswa

2017/2/20

#
Hello :) I have some problem again here :D so i want to make and actor shoot the projectile where he pointing or where the gun is pointed (i use my mouse here) but in my code here, the bullet follow my mouse xD since its getX() and getY() here my code (Projectile class) public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); turnTowards(mouse.getX(),mouse.getY()); move(5); } i dont know the method to make, after the mouse is clicked the object will move to the direction where the mouse located and still go forth .-. thx all
Super_Hippo Super_Hippo

2017/2/20

#
The bullet should just move straight. When creating the bullet, change the rotation of it. So it could look like this:
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null && Greenfoot.mousePressed(null))
{
    Actor p = new Projectile();
    getWorld().addObject(p, getX(), getY());
    p.turnTowards(mouse.getX(), mouse.getY());
}
Nosson1459 Nosson1459

2017/2/21

#
joshuadswa wrote...
public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); turnTowards(mouse.getX(), mouse.getY()); move(5); }
If before typing this bit of code you would press on the word "code" on the bottom of the text box and type the code into the frame that pops up, it would make it easier to read as code, like in the above post by Super_Hippo. Need help with embedding? click here! Posting code? read this!
joshuadswa joshuadswa

2017/2/21

#
Thx a lot :D ^^ you guys are the best :D
You need to login to post a reply.