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

2021/12/22

i am trying to make my player fire a laser when spacebar is pressed but i cant add my laser object through my Rocket class

roonie01 roonie01

2021/12/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 
public class Rocket extends Actor
{
    GreenfootImage rocket = new GreenfootImage("shipNoFire.png ");
    GreenfootImage ship = new GreenfootImage("ship.png ");
    int x;
    int y;
    /**
     * Act - do whatever the rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        x = this.getX();
        y = this.getY(); 
        // initialize rocket class
         
       movements();
         
 
    }
    public void movements()
    {
        if(Greenfoot.isKeyDown("w")&& y <= 1200)
        {
             
               //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX(),getY()-3);
         
        }
        if(Greenfoot.isKeyDown("s")&& y >= 0)
        {
               //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX(),getY()+3);
        }
        if(Greenfoot.isKeyDown("a")&& x <= 1600)
        {
               //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX()-3,getY());
        }
        if(Greenfoot.isKeyDown("d")&& x >= 0)
        {
               //says to check for key up and if condition is true move up 3 pixel's 
            setLocation(getX()+3,getY());
        }
    }
    public void Bullets()
    {
        if(Greenfoot.isKeyDown("space"))
          {
              addObject(new Laser(),getX()+5,getY());              
          }
    }
    }
     
    
danpost danpost

2021/12/22

#
The addObject method is a World instance method -- meaning only an instance of type World can execute it. Use:
getWorld().addObject(new Laser(), getX()+5, getY());
roonie01 roonie01

2021/12/22

#
ok that works but why does the laser make my enemy class stay at top of screen i was trying to have laser delete after touching the enemy this is what i have for code in laser class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Laser here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Laser extends Rocket
{
    /**
     * Act - do whatever the Laser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int counter; 
    public void act()
    {
        // Add your action code here.
        counter++;

        // use counter to add a delay 
        if (counter>=5)
        {
            //tells the laser class to wait until counter is equal to 5 
            setLocation (getX(),getY()-1);

        }
        else if(this.isAtEdge()) 
        {
           //checks if at edge and removes laser if is true 
            getWorld().removeObject(this);   
        }
        else if (this.isTouching(enemy1.class))
        {
            //same as edge detection just for enemy class
            getWorld().removeObject(this);   
        }
    }
}


danpost danpost

2021/12/22

#
roonie01 wrote...
ok that works but why does the laser make my enemy class stay at top of screen i was trying to have laser delete after touching the enemy
You only gave Rocket and Laser codes here. Need enemy as well.
You need to login to post a reply.