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

2012/7/29

insert health bar in boss.class but error

Andriyanto Andriyanto

2012/7/29

#
i put void counter() in act but error this is code boss.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Math;
import java.awt.Color;
/**
 * 
 */
public class boss2 extends musuh
{
////////////////////////////////////
boolean bDefeated = false;

static int totalparts = 30;
boss2part[] parts = new boss2part[totalparts];

boolean bPartsbeenset = false;

int goalx;
int goaly;
boolean bGoalset = false;

int bosshealth = 7;
float parthurt = -1;

//accelration
float xa=0;
float ya=0;

//////////

static GreenfootImage iCacingHeadNormal = new GreenfootImage("xx.gif");
static GreenfootImage iCacingHeadHurt = new GreenfootImage("xx.gif");

static GreenfootImage iCacingPartNormal = new GreenfootImage("xx.gif");
static GreenfootImage iCacingPartHurt = new GreenfootImage("xx.gif");

    public boss2()
    {
     bCanBeAttackedFromBelow = false;
      bKillOnFall = false;
     usualstun = 40;
     health = 7;
      maxrun = 3;
     
    }//constructor

     protected void addedToWorld(World world)
    {
        x = getX();
        y = getY();
    }//added

  ////////////////////////////////////
    public void act() 
    {
        myWorld mworld = (myWorld) getWorld();  
        if(!mworld.isPaused)  
        {
        if (bRemoved) {return;}
          setparts();
          partsanim();
         if (!bDefeated) {
             setgoal();
                if (stunned <= 0) {followgoal();}
                if (dmgdelay <= 0) {setImage(iCacingHeadNormal);}
                move();
             partsfollow();
             hasreachedgoal();
            }
             else
            {
               gravity();
                movement();
              
            }//endif
            
            
        delays();
        limits();
        setLocation((int) x, (int) y);
         
            
         if (bosshealth <= 0 && !bDefeated)
         {
            bDefeated = true;
            bMeleeAttack = false;
            bCanBeAttackedAtAll = false;
            bKillOnFall = true;
              partsfall();
            getWorld().addObject(new kunci(), 320, 240);   
        }//defeat
        
        if (health < 0) { removeme();}
        counter();
        }
        
    }//act  
 /////////////////
      public void removeme()
    {
        bRemoved = true;
        
        for (int i = 0; i < totalparts;i++)
        {
            
           getWorld().addObject(new efek(), parts[i].getX(),parts[i].getY());
            getWorld().removeObject(parts[i]);
        }//next i
        
        getWorld().addObject(new efek(),getX(),getY());
        getWorld().removeObject(this);
    }//killthis
    
    //////////
      public void damageStun(int dmg)
     {
       if (dmgdelay <= 0) {
    //  ys = 3;   
     // dmgdelay = 10;
      stunned = usualstun;
        }
     }//damstunoverload
    
   /////////////////////////////////////////////
    private void move()
    {
      
        if (stunned <= 0) {
        xs += xa;
        if (xs> maxrun) {xs = maxrun;}
        if (xs<-maxrun) {xs = -maxrun;}
        
        ys += ya;
        if (ys > maxrun) {ys = maxrun;}
        if (ys < -maxrun) {ys = -maxrun;}
    } else {
    if (ys < 6) {ys+= 0.4;}
         }
        
         if (y > 440 && ys > 0 && dmgdelay <=0) 
         {
             ys = -16; 
            stunned = 40;
            dmgdelay = 50;
            Greenfoot.playSound("kick.wav");
            setImage(iCacingHeadHurt);
            health = 7;
            bosshealth--;
            parthurt = 0;
        }
         
        x += xs;
        y += ys;
    }//move

    private void followgoal()
    {
        int x = goalx - getX();
        int y = goaly - getY();
        float length =(float) Math.sqrt((x*x)+(y*y));

        xa = x / length;
        ya = y / length;
    }//

    private void hasreachedgoal()
    {
        if (getX() > goalx+12 || getX() < goalx-12) {return;}
        
        if (getY() > goaly+12 || getY() < goaly-12) {return;}
        
        bGoalset = false;
    }//reachedgoal
    
    private void setgoal()
    {
        if (bGoalset) {return;}
        bGoalset = true;
        goalx = goalx + Greenfoot.getRandomNumber(380)+130;
        if (goalx > 600) {goalx = 70;}
        goaly = Greenfoot.getRandomNumber(200)+110;
    }//setgoal
    
    private void partsanim()
    {
        if (parthurt < 0) {return;}
        parthurt++;
        if (parthurt > 20) {
             for (int i = 0; i < totalparts;i++)
        {
            parts[i].setImage(iCacingPartNormal);
        }//nexti
            parthurt = -1;}
            else{
      
            for (int i = 0; i < totalparts;i++)
        {
             if (Greenfoot.getRandomNumber(8) == 1) {
                 parts[i].setImage(iCacingPartNormal);
                }else
                {
                 parts[i].setImage(iCacingPartHurt);}//endif
        }//nexti
    }//else
    }//partsanim
    
    private void partsfall()
    {
        for (int i = 0; i < totalparts;i++)
        {
           // parts[i].setLocation(parts[i].getX()+parts[i].cX,getY());
            parts[i].bActive = false; //minor bugfix  -- turn off being elevator after boss2 defeated
            parts[i].bUnconnected = true;
            parts[i].ys = (Greenfoot.getRandomNumber(10)+4)*-1;
            parts[i].xs = Greenfoot.getRandomNumber(10)-Greenfoot.getRandomNumber(10);
        }//next i
    }//void

    private void partsfollow()
    {
        int x = 10;
        int y = 10;
        x =(int) (parts[0].getX()+ (getX() - parts[0].getX())*0.2f);
        y =(int) (parts[0].getY()+ (getY() - parts[0].getY())*0.2f);
       
        y++;
         //         parts[0].setpx();
         //         parts[0].setLocation(x,y); 
         //         parts[0].setcx();
    parts[0].place(x,y);

       for (int i = 1; i < totalparts;i++)
       {
         x =(int) (parts[i].getX()+ (parts[i-1].getX() - parts[i].getX())*0.2f);
         y =(int) (parts[i].getY()+ (parts[i-1].getY() - parts[i].getY())*0.2f);
      
            y++;    
            //         parts[i].setLocation(x,y); 
            parts[i].place(x,y);
       }//next i
       
    }//endofvoid

    private void setparts()
    {
        if (bPartsbeenset) {return;}
        bPartsbeenset = true;
        
      myWorld mg = (myWorld) getWorld();
      

       //int tive = 0;
       
            for (int i = 0; i<totalparts;i++)
            {
                 parts[i] = new boss2part();
                 mg.addObject(parts[i],getX(),getY());
                    //                  parts[i].bActive = false;
                    //                  tive++;
                    //                  if (tive >2) {tive =0; parts[i].bActive = true;}
                 // parts[i].setImage(boss2part_image);
            }//next i
        
   }//setparts 
    public void counter()
    {   
        if(bosshealth==7 && mworld.getObjects(life7.class).isEmpty())
        {
            mworld.addObject(new life7(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life0.class));
        }

        if(bosshealth==6 && mworld.getObjects(life6.class).isEmpty())
        {
            mworld.addObject(new life6(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life7.class));
        }

        if(bosshealth==5 && mworld.getObjects(life5.class).isEmpty())
        {
            mworld.addObject(new life5(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life6.class));
        }

        if(bosshealth==4 && mworld.getObjects(life4.class).isEmpty())
        {
            mworld.addObject(new life4(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life5.class));
        }
        
        if(bosshealth==3 && mworld.getObjects(life3.class).isEmpty())
        {
            mworld.addObject(new life3(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life4.class));
        }
        
        if(bosshealth==2 && mworld.getObjects(life2.class).isEmpty())
        {
            mworld.addObject(new life2(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life3.class));
        }
        
        if(bosshealth==1 && mworld.getObjects(life1.class).isEmpty())
        {
            mworld.addObject(new life1(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life2.class));
        }
        
        if(bosshealth==0 && mworld.getObjects(life0.class).isEmpty())
        {
            mworld.addObject(new life0(), 583, 18);
            mworld.removeObjects(mworld.getObjects(life1.class));
        }
    }
so, where i must put counter();?? can help me??
danpost danpost

2012/7/29

#
public void counter()
{
    mworld.removeObjects(Life.class);
    Life life;
    switch (bosshealth)
    {
        case 0:  life = new Life0(); break;
        case 1:  life = new Life1(); break;
        case 2:  life = new Life2(); break;
        case 3:  life = new Life3(); break;
        case 4:  life = new Life4(); break;
        case 5:  life = new Life5(); break;
        case 6:  life = new Life6(); break;
        case 7:  life = new Life7(); break;
    }
    mworld.addObject(life, 583, 18);
}
The code above is equivalent to yours, but is much cleaner and easier to read. Without knowing you class heirarchy (what extends what), it is hard to say exactly what you need to do with it. This much I can say: this method should be called only IF, or when, damage is being dealt to the boss. The check for damage is done each act cycle. The code should probably be in the Boss class.
SPower SPower

2012/7/29

#
@andriyanto I want to help you, so a few little tips: -the names of classes should start with an Uppercase, so boss should be Boss. -Not very important, but I saw you writing this:
void counter()
When you're talking about a method, you don't have to say the return type or put the () when you have no parameters. But as I said, the second tip isn't super important, just a little helpful :) -another tip, you might want to checkout some courses from this collection: http://www.greenfoot.org/collections/321
Andriyanto Andriyanto

2012/7/30

#
danpost : ok i try to change my code with your code.. yes, only or when damege is being dealt to the boss.. thanks..
Andriyanto Andriyanto

2012/7/30

#
SPower : yes, but another class with name boss (not Uppercase) nothing error, and i put counter above in act() no syntax error. only in boss2.class i put counter(); there an error. thanks for your tips and i wil check your collection.
Andriyanto Andriyanto

2012/7/30

#
danpost : error because boss2.class have extend life7 until life0..
Andriyanto Andriyanto

2012/7/30

#
this is error in boss2.class with mymethod counter(); when i put in act(); java.lang.NullPointerException at boss2.Counter(boss2.java:266) at boss2.act(boss2.java:94) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
SPower SPower

2012/7/30

#
From this line: NullPointerException You can tell that you're using an object which is null. From this line: at boss2.act(boss2.java:266) You can tell that it happens at the 26th line in the class boss2. I think this is where it all happens:
mworld.addObject(new life7(), 583, 18);
because it can never access this code (which is in act):
myWorld mworld = (myWorld) getWorld();
From the method it's in. Simply copy this line into the counter method, and it should work (as I can see now). And about the uppercase for class names, there's no error when you don't do it, but you confuse other programmers.
Andriyanto Andriyanto

2012/7/30

#
yes i can.. i put if on method counter to act.. :D thanks All..
You need to login to post a reply.