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

2020/6/16

Variabel from other class not effected

MichaelTan MichaelTan

2020/6/16

#
so i had idea that i have 10 ammo which every i press space will fire and the ammo will minus. but if the ammo hit the enemy i will gain 1 ammo. so my fire code is in Spaceship class
public class Spaceship extends Actor
{
    /**
     * Act - do whatever the Spaceship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int speed=5;
    private boolean firerate = true;
    
    public int ammo = 10;
    
    
    public Spaceship()
    {
        GreenfootImage image = getImage();
        
        int newHeight = (int)image.getHeight() / 4;
        int newWidth = (int)image.getWidth()/ 4;
        
        image.scale(newWidth, newHeight);

    }   
    
    public void act() 
    {
        // Add your action code here.
        spaceshipMove();
    }    
    
    public void spaceshipMove()
    {
        if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("A"))
        {
            setLocation(getX()-speed, getY());
        }
        
          if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("D") || Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+speed, getY());
        }
        
        
    }
    
    public void fireTheBullet()
    {
        if(ammo == 0)
        {
            getWorld().showText("GameOver", 500, 500);
            
            Greenfoot.stop();
        }
        if(Greenfoot.isKeyDown("space") && firerate == true)
        {
            getWorld().addObject(new Bullet_1(),getX(), getY() - 55);
            firerate = false;
            ammo--;
        }
        else if(!Greenfoot.isKeyDown("space") )firerate = true;
        
    }
    
    }
}
and in my enemy class i try to increase the ammo. here my enemy code
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int speed = 2;
    public boolean tambah = false;
    
    public void act() 
    {
        // Add your action code here.
        enemyMove();
    } 
    
    public void enemyMove()
    {
        setLocation(getX(), getY()+speed);
    }
    
    public void removeAtEdge()
    {
        //if(getY() == 699) getWorld().removeObject(this);
        
    }
    
        public void bulletHit()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        
        if(getY() == 699) getWorld().removeObject(this);
        
        else{
        if(bullet != null)
        {
            Spaceship addbullet = new Spaceship();
            
            getWorld().removeObject(bullet);
            addbullet.ammo++;
            getWorld().removeObject(this);
            tambah = true;
        }
        
    }
    }
}
can anyone help me so the ammo will increase after i hit the enemy. Sorry for bad english.Thankyou...
danpost danpost

2020/6/16

#
MichaelTan wrote...
in my enemy class i try to increase the ammo. here my enemy code << Code Omitted >>
Line 37 creates a new Spaceship instance. It has its full complement of bullets and has never been in any world and has never fired a shot. Increasing its ammo count (at line 40) does nothing for the ammo count of the Spaceship instance that has been in the world and has fired a shot. You need to acquire a reference to the instance of Spaceship that is in the world and adjust its ammo count.
MichaelTan MichaelTan

2020/6/17

#
Hello danpost. can you code it for me? i dont really understand about what you said because im new to this greenfoot. it would help me so much. btw thankyou for your reply :)
danpost danpost

2020/6/17

#
MichaelTan wrote...
can you code it for me? i dont really understand about what you said because im new to this greenfoot. it would help me so much. btw thankyou for your reply :)
To get a reference to the Spaceship instance already in the world, provided one is always in the world, use:
Spaceship addBullet = (Spaceship)getWorld().getObjects(Spaceship.class).get(0);
To avoid an error, if it is possible no ship is in the world, you will need to ensure the List object returned by getObjects is not empty before using get on it.
You need to login to post a reply.