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

2024/2/20

Shooting

Kookaburra737 Kookaburra737

2024/2/20

#
Hey! I'm having some trouble with getting my character too shoot can someone help me? The Actors im using is Player_2 and Shots, the world they are going in is called Spike_Out... plz help... my error is in the "addObject"
    public void Shoot()
    {
        if(Greenfoot.isKeyDown("space")) 
        {  
           Player_2 shooter1 = new Player_2(); 
           int X = shooter1.getX(); 
           int Y = shooter1.getY(); 
           Shots shot = new Shots(); 
           shot.setRotation(shooter1.getRotation());
           addObject(shot, X, Y); 
        }
    }
BrodyCoe BrodyCoe

2024/2/21

#
If this code is run in a class other than World, you will need to add the getWorld() method immediately before the addObject() method. In your case, this would be public void Shoot() { if (Greenfoot.isKeyDown("space")) { Player_2 shooter1 = new Player_2(); int X = shooter1.getX(); int Y = shooter1.getY(); Shots shot = new Shots(); shot.setRotation(shooter1.getRotation()); getWorld().addObject(shot, X, Y); } }
danpost danpost

2024/2/21

#
Kookaburra737 wrote...
public void Shoot()
{
    if(Greenfoot.isKeyDown("space")) 
    {  
       Player_2 shooter1 = new Player_2(); 
       int X = shooter1.getX(); 
       int Y = shooter1.getY(); 
       Shots shot = new Shots(); 
       shot.setRotation(shooter1.getRotation());
       addObject(shot, X, Y); 
    }
}
Line 5 creates a new Player_2 object. Lines 6 and 7 will both fail as this newly created object is not yet in any world. However, I am sure that this newly created object is not the instance of the class you wish to have shoot. That would be the instance already in the world. This method should be in your Player_2 class and called from its act method. The instance should be referred to as this (which is implicitly understood when no other object is given). The method should be as follows:
public void Shoot()
{
    if(Greenfoot.isKeyDown("space"))
    {
        Shots shot = new Shots();
        this.getWorld().addObject(shot, this.getX(), this.getY());
        shot.setRotation(this.getRotation());
    }
}
or (being implicit):
public void Shoot()
{
    if(Greenfoot.isKeyDown("space"))
    {
        Shots shot = new Shots();
        getWorld().addObject(shot, getX(), getY());
        shot.setRotation(getRotation());
    }
}
You may find that this still does not quite work as you might like as multiple shots will be fired in quick repetition. That is because the isKeyDown function returns true as long as the space bar is held down. To perform the action only once each time the key is initially pressed, use the following:
private boolean spaceKeyDown;

public void Shoot()
{
    if (spaceKeyDown != Greenfoot.isKeyDown("space"))
    {
        spaceKeyDown = ! spaceKeyDown;
        if (spaceKeyDown)
        {
            Shots shot = new Shots();
            getWorld().addObject(shot, getX(), getY());
            shot.setRotation(getRotation());
        }
    }
}
Line 5 is only true when the space key changes state, either from up to down or from down to up. Line 7 updates the tracking field to the new state and line 8 is true only if that state change was from being up to being down (at the instant the key goes down).
Kookaburra737 Kookaburra737

2024/2/22

#
Thank you so much, it works perfectly now, but how would I delete the bullets touching the worlds border?
danpost danpost

2024/2/22

#
Kookaburra737 wrote...
Thank you so much, it works perfectly now, but how would I delete the bullets touching the worlds border?
Use code like the following in the act method of the projectile's class:
if (getX() == 0 || getX() == getWorld().getWidth()-1) getWorld().removeObject(this);
You can modify the code to include vertical limits.
You need to login to post a reply.