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

2016/3/8

Help me, my counter "n" wont increase...

Nathan2000 Nathan2000

2016/3/8

#
public class rocket extends Actor
{
    int n=0;
    /**
     * Act - do whatever the laser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(50);
       if(cekmusuh())
       {
           kenarocket();
           
       }
       info();
       nyampeujung();
    }
    
    public boolean cekmusuh()
    {
        Actor musuh = getOneObjectAtOffset(0,0,musuh.class);
        if(musuh!=null)return(true);else return(false);
    }
    
    public void kenarocket(){
            Actor musuh = getOneObjectAtOffset(0,0,musuh.class);
            if(musuh!=null){
                getWorld().removeObject(musuh);
                n++;
        }
    }
    
    public void nyampeujung()
    {
        if(this.isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    
    public void info()
    {
        labelhp lhp = new labelhp();
        lhp.setTulisan("n : "+n);
        getWorld().addObject(lhp,110,20);[quote]

public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    protected int hp = 500;
    private boolean mouseIsDown;
    private int counter,HIT,rockets;
    
    public Player()
    {
        rockets = 25;
        counter = 0;
        // HIT = 0;
        
    }
    
    public void act() 
    {
        if(hp<=0){
            Greenfoot.stop();
            lose();
        }
        tembakrocket();
        kenamusuh();
        kenaboss();
        // info();
        
    }
    
    public void kenamusuh()
    {
       Actor musuh = getOneObjectAtOffset(0,0,musuh.class);
       if(musuh!=null)
       {
           hp=hp-1;
       }
    }
    
    public void kenaboss()
    {
        Actor boss = getOneObjectAtOffset(0,0,boss.class);
        if(boss!=null)
        {
            hp=hp-3;
        }
    }
    
    // public void info()
    // {
        // labelhp lhp = new labelhp();
        // lhp.setTulisan("HP : "+hp);
        // getWorld().addObject(lhp,110,20);
    // }
    
    public void tembakrocket()
    {
        //mendapatkan koordinat kursor, diklik atau tidak, ditekan atau tidaknya klik kiri atau klik kanan
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mousePressed(null)){mouseIsDown = true;}
        else if (Greenfoot.mouseClicked(null)){mouseIsDown = false;}
        
        if(mouseIsDown){
            if(counter >= rockets){
                //mengeluarkan rocket sesuai mouse info
                rocket rc = new rocket();
                getWorld().addObject(rc, getX(), getY()-10);
                
                //player mengikuti koordinat dari kursor
                if(mouse!=null){rc.turnTowards(mouse.getX(), mouse.getY());}
                rc.move(0);
                counter = 0;
            }
        }
        counter++; //jeda rocket yang keluar
        // if(HIT>0){HIT--;}
    }
    
    public void lose()
    {
        getWorld().showText("========= YOU LOSE =========", 500, 500);
    }[/quote]
[code]
{
    /**
     * Act - do whatever the musuh wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    // private boolean klik=true;

    public class musuh extends Actor
    
    public void act() 
    {
       move();
       tembakdibase();
      
    }
    
    public void tembakdibase()
    {
       if(Greenfoot.mousePressed(this))
       {
           getWorld().removeObject(this);
       }
       
    }
    
    public void move(){
        musuh m = new musuh();
        int x = ((Player)getWorld().getObjects(Player.class).get(0)).getX();
        int y = ((Player)getWorld().getObjects(Player.class).get(0)).getY();
        turnTowards(x,y);
        move(3);
    }

}
}
Nathan2000 Nathan2000

2016/3/8

#
when the "rocket" touch the "musuh", the counter increase, but when I'm clicked again, the counter reset to 0
Super_Hippo Super_Hippo

2016/3/8

#
Each 'rocket' has its own 'n' variable. So whenever you create a new 'rocket', 'n' will be set to zero (line 3) and the new rocket prints its own 'n' into the world. One way to make it work is to track the hits in the Player's class code. And whenever the rocket hits an enemy, it increases the variable of the Player object.
You need to login to post a reply.