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

2017/6/18

error in "indeksTerakhir" please help me

Adhim Adhim

2017/6/18

#
private void kurangiNyawaKetikaKenaPeluru(){
       Actor pelor2 = getOneIntersectingObject(pelor2.class);
       if (pelor2 !=null){
          int indeksTerakhir = bg.getNyawaBar().size()-1;
          nyawa nyawaTerakhir = bg.getNyawaBar().get(indeksTerakhir);
          
          getWorld().removeObject(pelor2);
          getWorld().removeObject(nyawaTerakhir);
          bg.getNyawaBar().remove(1);
    }
    }
danpost danpost

2017/6/18

#
You did not say what kind of error you were getting. To eliminate one possibility, try inserting the following at line 5:
if (indeksTerakhir < 0) return;
If you are still getting an error, try inserting the following at line 4 to eliminate another possibility:
if (bg.getNyawaBar() == null) return;
Adhim Adhim

2017/6/19

#
the error incompatible types: java.lang.Object cannot be converted greenfoot
Adhim Adhim

2017/6/19

#
sorry for bad english :D
danpost danpost

2017/6/19

#
Adhim wrote...
the error incompatible types: java.lang.Object cannot be converted greenfoot
On what line? (and it appears the error message was not copied completely).
Adhim Adhim

2017/6/19

#
this actor class code (ps)
import greenfoot.*;

public class ps extends Actor
{ 
   public ps ()
        {
       GreenfootImage bata = getImage ();
        int tinggi=(int)bata.getHeight()/2;
        int lebar=(int)bata.getWidth()/2;
        bata.scale(lebar,tinggi);
    }
   private bg pl;
   public void addedToWorld(World bg){
      pl=(bg)bg;
     }
    /**
     * Act - do whatever the ps wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
  
   
   private void kurangiNyawaKetikaKenaPeluru(){
       Actor pelor2 = getOneIntersectingObject(pelor2.class);
       if (pelor2 !=null){
          int indeksTerakhir = bg.getNyawaBar().size()-1;    
          if(bg.getNyawaBar()==null)return;
          getWorld().removeObject(pelor2);
          
          bg.getNyawaBar().remove(1);
          }
    }

    public void act() 
   {
    //kurangiNyawaKetikaKenaPeluru();
     {
         // Menggerakan aktor ps menggunakan mouse
              if(Greenfoot.mouseMoved(null)){
       MouseInfo mouse = Greenfoot.getMouseInfo();
       setLocation(mouse.getX(), mouse.getY());
      }
      // Menembakkan peluru dengan mengklik kiri mouse
       if(Greenfoot.mouseClicked(null)) {
       pl.addObject(new pelor1(),getX()+getImage().getHeight(),getY());
       
      }
     }  
   }
}
Adhim Adhim

2017/6/19

#
this world class code(bg)
import greenfoot.*;
import java.awt.Color;
import java.util.ArrayList;
/**
 * Write a description of class bg here.
 * Adhim Afanda Suherman
 * @version 1.0.0
 */
public class bg extends World
{
    private final int NYAWA_MAX = 3;
    private static ArrayList nyawaBar = new ArrayList();
   
    Counter counter = new Counter("Skor: ");
    /**
     * Constructor for objects of class bg.
     * 
     */
    public bg()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(854, 480, 1); 
        setPaintOrder(skor.class, fire.class, awan.class, ps.class, ufo.class, Counter.class, pelor1.class, pelor2.class);
        addObject(new awan(),853,200);
        addObject(new ps(),20,220);
        addObject(new ufo(),853,320);
        addObject(counter,200,450);

        prepare();
    }


    public void act(){
        if(Greenfoot.getRandomNumber(1000)<4){
            addObject(new awan(),853,Greenfoot.getRandomNumber(500)-25);
        }
        if(Greenfoot.getRandomNumber(1000)<4){
            addObject(new ufo(),853,Greenfoot.getRandomNumber(500)-15);
        }
        if(Greenfoot.isKeyDown("space")){
            Greenfoot.stop();
        }
        GreenfootImage image = this.getBackground();
    }

    public void tambah(){
        counter.add(10);
    }
 
    /**
     * Called when game is up. Stop running and display score.
     */
    public void selesai() 
    {
        
        addObject(new skor(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
        
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        prepareNyawaBar(40, 450);
    }
    private void prepareNyawaBar(int offsetX, int offsetY){
        int spasiAntarNyawa = 10;
        for (int i = 0; i < NYAWA_MAX; i++){
            nyawa nyawa = new nyawa();
            nyawa.setLokasiX(offsetX+((nyawa.getImage().getWidth()+spasiAntarNyawa)*i));
            nyawa.setLokasiY(offsetY);
            addObject(nyawa, nyawa.getLokasiX(), nyawa.getLokasiY());
            nyawaBar.add(nyawa);}
        }
    public static ArrayList getNyawaBar() {
            return nyawaBar;
            }
}

Adhim Adhim

2017/6/19

#
there are not syntax error but the health(nyawa) not work
danpost danpost

2017/6/19

#
Adhim wrote...
private void kurangiNyawaKetikaKenaPeluru(){
       Actor pelor2 = getOneIntersectingObject(pelor2.class);
       if (pelor2 !=null){
          int indeksTerakhir = bg.getNyawaBar().size()-1;
          nyawa nyawaTerakhir = bg.getNyawaBar().get(indeksTerakhir);
          
          getWorld().removeObject(pelor2);
          getWorld().removeObject(nyawaTerakhir);
          bg.getNyawaBar().remove(1);
    }
    }
Change 'get' in line 5 to 'remove' and then remove line 9. Also, in your bg class, change line 12 to:
private static ArrayList<nyawa> nyawaBar;
and insert the following at line 28:
nyawaBar = new ArrayList<nyawa>();
Adhim Adhim

2017/6/19

#
change all "get" to remove in line 5 ?
Adhim Adhim

2017/6/19

#
there are an error in "nyawa nyawaTerakhir = bg.getNyawaBar().remoove(indeksTerakhir);" (indeksTerakhir); error is incompatible types: java.lang.Object cannot be converted to nyawa
private void kurangiNyawaKetikaKenaPeluru(){
       Actor pelor2 = getOneIntersectingObject(pelor2.class);
       if (pelor2 !=null){
          int indeksTerakhir = bg.getNyawaBar().size()- 1;
          nyawa nyawaTerakhir = bg.getNyawaBar().remove(indeksTerakhir);
          getWorld().removeObject(pelor2);
          getWorld().removeObject(nyawaTerakhir);
          
          
          }
    }
danpost danpost

2017/6/19

#
danpost wrote...
Also, in your bg class, change line 12 to:
private static ArrayList<nyawa> nyawaBar;
and insert the following at line 28:
nyawaBar = new ArrayList<nyawa>();
Did you make these changes also?
danpost danpost

2017/6/19

#
Line 78 may also need adjusting. Try the following there:
public<T> static ArrayList<T> getNyawaBar()
You need to login to post a reply.