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

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
1
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.