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

2014/9/23

need help in adding objects to world using arraylist

lowens0 lowens0

2014/9/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import java.util.Random; 
/**
 * Write a description of class Bg here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bg extends World
{
ArrayList dynamicArray = new ArrayList(6);

    public Bg()
    {        
        
        super(600, 400, 1); 
       
        for(int i =0;i <6;i++){
             int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
            Actor[] actor ={new blue(), new green(),new orange(),new red(), new violet(), new yellow()};
            int ColorNumber = Greenfoot.getRandomNumber(6);
            addObject(actor[ColorNumber],x, y);
             
        System.out.println(actor[ColorNumber]); 
       
  
        
    }
            
           if(!getObjects(blue.class).isEmpty()){
              int color= getObjects(blue.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new blue());
                System.out.println("blue is not null");}
            }
           if(!getObjects(green.class).isEmpty()){
              int color= getObjects(green.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new green());
                System.out.println("green is not null");}
            }
            if(!getObjects(orange.class).isEmpty()){
              int color= getObjects(orange.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new orange());
                System.out.println("orange is not null");}
            }
            if(!getObjects(red.class).isEmpty()){
              int color= getObjects(red.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new red());
                System.out.println("red is not null");}
            }
            if(!getObjects(violet.class).isEmpty()){
              int color= getObjects(violet.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new violet());
                System.out.println("violet is not null");}
            }
            if(!getObjects(yellow.class).isEmpty()){
              int color= getObjects(yellow.class).size();
               for(int i = 0; i<color;i++){
                   dynamicArray.add(new yellow());
                System.out.println("yellow is not null");}
            }
            Random generator = new Random();  
           String a= dynamicArray.get(generator.nextInt(dynamicArray.size())).toString();
            addObject(a,50,50);
          System.out.println(a);
           
        }
    }

i got this error:
erdelf erdelf

2014/9/23

#
well, in line 69 change the variable a to an Actor reference and remove the to string at the end
Actor a= (Actor)dynamicArray.get(generator.nextInt(dynamicArray.size()));
lowens0 lowens0

2014/9/23

#
thanks it worked! xD
You need to login to post a reply.