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

2016/3/31

Question regarding the "asteroids" game

madeirense madeirense

2016/3/31

#
Hi :) So, regarding the "Asteroids" game I'm trying to make two things happen but I can't figure it out. The first thing is that I want my bullets to go where my "mouse cursor" is WHEN I press the "Left Mouse Button". Secondly, I already changed the code so that the movement of the Spaceship is done by pressing "WASD" instead of using just 1 key to accelerate. The thing is that the Spaceship "instantly" turns to that direction and I want it to "smoothly" turn up/down/left/right. What I'm trying to accomplish is very similar to this game (Link below), notice how the Spaceship moves and shoots, they're 2 independent things.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private void checkKeys()
    {
        //for going down
        if(Greenfoot.isKeyDown("S")) {
            setRotation(90);
            ignite(Greenfoot.isKeyDown("S"));
        }
        //for going up
        if(Greenfoot.isKeyDown("W")) {
            setRotation(270);
            ignite(Greenfoot.isKeyDown("W"));
        }
        //for going left
        if(Greenfoot.isKeyDown("A")) {
            setRotation(180);
            ignite(Greenfoot.isKeyDown("A"));
        }
        //for going right
        if(Greenfoot.isKeyDown("D")) {
            setRotation(0);
            ignite(Greenfoot.isKeyDown("D"));
        }
fejfo fejfo

2016/3/31

#
for the bullets you could use
1
2
3
if(Greenfoot.getMouseInfo().getButton() == 1) {
    //go to the mouse
}
of course you should check if the mouse is on the screen first for the spaceship you shouldn't use wasd the way you do if you want it to turn smoothly else your gonna end up with it facing an other direction then it is going you should use a/q to turn left and d to turn right z/w to go forwards and s to go back I don't know what ignite does but beware that your passing a booling as argument not the key and that it can happen more then once per act
madeirense madeirense

2016/4/1

#
Hmm, that code that you've showed doesn't seem to work was I wanted. What I have now is this:
1
2
3
4
5
6
7
8
9
10
if(Greenfoot.mouseClicked(null))
        {
            (new GreenfootSound("shoot.wav")).play();          
            getWorld().addObject(new Shot(), getX(), getY());
             
            MouseInfo mouse = Greenfoot.getMouseInfo();
            this.turnTowards(mouse.getX(), mouse.getY());
             
            shotKeyDown = true;
        }
The thing is that I need the bullets to continuously fire as I have the mouse pressed down. Secondly I don't want the spaceship to turn to where my mouse is pointing, I want to be able to Move and Fire at the same time.
Super_Hippo Super_Hippo

2016/4/1

#
Instead of the 'mouseClicked' method, you could use the 'mouseDragged' method. I thought the 'shotKeyDown' variable would be there to prevent continuous shots. Actually I don't believe that you really want them to fire continuously like that. You will probably need a timer to delay to shots.
madeirense wrote...
Secondly I don't want the spaceship to turn to where my mouse is pointing
Remove lines 6 and 7 then. I wonder what the 'ignite' method is because you always pass 'true' to it.
You need to login to post a reply.