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

2017/3/19

I need help on how to make my pistol shoot one bullet, it shoots like a machine gun right now!

Zappib Zappib

2017/3/19

#
I was looking at some tutorials by mrligocki on youtube and he shows us how to make shoot bullets but he never showed us how to fix it so when we click LMB (left mouse button) it only shoots one bulletThis is the video I watched to get this far into getting my character to shoot
1
2
3
4
5
6
7
8
public void shoot()
{
    if(Greenfoot.getMouseInfo() !=null)
    {
        if(Greenfoot.getMouseInfo().getButton() == 1)
        getWorld().addObject(new Bullet(jeromeRotation), getX(), getY());
    }
}
This is the code he tolled us to write down. I need help!!! DAN POST HELP ME!!!
Nosson1459 Nosson1459

2017/3/19

#
The method getButton will return the number of the pressed button while you're moving the mouse, once you stop moving it will go back to zero (this doesn't necessarily have anything to do with your code, but it's the reason that I couldn't use getButton to help you). With your code if you're holding down on the left mouse button (and moving it a teeny bit) it will shoot a bullet. If you want to shoot one bullet when the mouse button is pressed, then have your shoot method be:
1
2
3
4
public void shoot()
{
    if (Greenfoot.mousePressed(null)) getWorld().addObject(new Bullet(jeromeRotation), getX(), getY());
}
I don't know if this will help you since I'm not DAN POST.
danpost danpost

2017/3/19

#
If you do not care about which button is clicked, then the code Nosson1459 supplied should suffice. If it must be the LMB, then use this:
1
2
3
4
5
6
7
public void shoot()
{
    if (Greenfoot.mousePressed(null) && Greenfoot.getMouseInfo().getButton() == 1)
    {
        getWorld().addObject(new Bullet(jeromeRotation), getX(), getY());
    }
}
Zappib Zappib

2017/3/20

#
danpost you are a legend, I love you mah man. Now I will get A+++ on my ict because of you. I want to meet in real life and shake your hand my good sir. Also thank you for fixing my problem I love you to Nosson if it was not for you replying dan would not have come here and suggested how to fix my problem so I can have my LMB to shoot, i will stop have a fantastic you two <3
You need to login to post a reply.