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

2017/3/13

Little Shooter - the weapon not like a ball

wueschn wueschn

2017/3/13

#
I need a little help with my little shooter. Whenever I press the space key in the Defender Class, a new weapon (should look like a ball) starts in Y-direction.
public class Defender extends Actor
{
    public void act() 
    {
        this.navigating();
    }
    public void navigating()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            this.setLocation(this.getX() +5, this.getY());
        }
        if(Greenfoot.isKeyDown("left"))
        {
            this.setLocation(this.getX() - 5, this.getY());
            
        }
        if(Greenfoot.isKeyDown("space"))
        {
            this.getWorld().addObject(new Weapons(), this.getX(), this.getY() - 50);
        }
       
    }
}
The Weapons (the ball) behave like this
public Weapons()
    {
        GreenfootImage image = new GreenfootImage(20,20);
        Color color = new Color(255, 0,0);

        this.setImage(image);
        this.getImage().setColor(color);
        this.getImage().drawOval(0, 0, 20, 20);
        this.getImage().fillOval(0, 0, 20, 20);

    }

    public void act() 
    {
        this.setRotation(270);
        this.move(10);
        this.shooting();
        this.vanishing();
    } 

    public void shooting()
    {
        if(this.isTouching(Enemies.class))
        {
            Greenfoot.playSound("Explosion.wav");
            this.removeTouching(Enemies.class);
            this.getWorldOfType(MyWorld.class).getCounter().addScore();

        }
    }

    public void vanishing()
    {
        if(this.isAtEdge())
        {
            this.getWorldOfType(MyWorld.class).removeObject(this);
        }
    }
}
The problem: the weapon (ball) is not a single round ball, it is a row of several balls that are connected to each others. It does not look like a weapon. I know it is difficult to explain, but I hope you understand the problem. Thanks a lot
Nosson1459 Nosson1459

2017/3/13

#
It's adding a new ball for every act act cycle that the space bar is down. Try this (code from danpost):
// outside all methods
private boolean down;

// lines 18 - 21 in Defender class
if (down != Greenfoot.isKeyDown("space"))
{
    down = !down;
    if (!down)
    {
        getWorld().addObject(new Weapons(), getX(), getY());
    }
}
With the above code you won't be able to hold down on the space bar for shooting. If you want to be able to hold down just have less spawn at a time then:
// outside all methods
private int timer;

// in act
timer++;

//lines 18 - 21 in Defender class
if (Greenfoot.isKeyDown("space") && timer > 25) // I chose a random number for the timer you can use whatever number you think fits
{
    getWorld().addObject(new Weapons(), getX(), getY());
    timer = 0;
}
danpost danpost

2017/3/14

#
Nosson1459 wrote...
Try this (code from danpost):
// outside all methods
private boolean down;

// lines 18 - 21 in Defender class
if (down != Greenfoot.isKeyDown("space"))
{
    down = !down;
    if (!down)
    {
        getWorld().addObject(new Weapons(), getX(), getY());
    }
}
With this, a shot is fired every time you release the space key. I myself prefer the action to occur when the key is pressed, where line 8 would then be:
if (down)
Nosson1459 Nosson1459

2017/3/14

#
danpost wrote...
With this, a shot is fired every time you release the space key. I myself prefer the action to occur when the key is pressed, where line 8 would then be:
if (down)
I agree the action should happen with the press as opposed to the release, but that's the way that I think it was given to me (I don't know since it wasn't ME who asked that particular question, so I don't either know what it was asked to use for - mentioned other times that WE'RE two people).
danpost danpost

2017/3/14

#
Nosson1459 wrote...
I agree the action should happen with the press as opposed to the release, but that's the way that I think it was given to me (I don't know since it wasn't ME who asked that particular question, so I don't either know what it was asked to use for - mentioned other times that WE'RE two people).
It was you -- and you asked about the release of a key here.
wueschn wueschn

2017/3/14

#
@danpost @Nosson1459 Perfect solutions, thanks a lot!! So I guess the mistake in my solution was the fact that I couldn`t handle just one shot with just keypressed. For me (as a dummy) to understand better your lines I adapted now to ;-)
public class Defender extends Actor
{
    private boolean pressed = false;

    public void act() 
    {
        this.navigating();
    }

    public void navigating()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            this.setLocation(this.getX() +5, this.getY());
        }
        if(Greenfoot.isKeyDown("left"))
        {
            this.setLocation(this.getX() - 5, this.getY());

        }
        
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed)
            {
                getWorld().addObject(new Weapons(), getX(), (getY() - 41));
            }
        }

    }
}
It works perfect, thanks a lot!!!!!!!
danpost danpost

2017/3/14

#
wueschn wrote...
So I guess the mistake in my solution was the fact that I couldn`t handle just one shot with just keypressed.
Correct. The 'isKeyDown' method gives the current state and you are looking for the moment of change in the state which requires knowing the previous state to compare with the current state.
Nosson1459 Nosson1459

2017/3/14

#
danpost wrote...
It was you -- and you asked about the release of a key here.
What do you mean it was me? How can you know which of us it was who asked it (I care more about the grammer than my brother, so there's a difference - you can even see this between this discussion and the one you linked to)? I know for a fact that, that particular discussion was my brother not me whose the one in this discussion. Thanks for showing me where it was because I didn't know where it was since it WASN'T ME who asked that.
Nosson1459 (Yehuda) wrote...
(I don't know since it wasn't ME who asked that particular question, so I don't either know what it was asked to use for - mentioned other times that WE'RE two people).
The user Nosson1459 has two people who use it. (This can be seen in some scenarios and posted code by the @author tag.)
You need to login to post a reply.