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

2014/12/13

using a for loop with actors

hhoogerwerf hhoogerwerf

2014/12/13

#
Hello everybody, I am trying to get a Hook to interact with an container. So when I press spacebar it moves with the hook. But because they have the same name when spawned. When they cross (the containers) it switches to the oldest container. I tried to solve it like this:
if(1==1){
            for(int i = 0; i<100; i++) {
                ContainerOrange containerOrange[i] = new ContainerOrange();
                addObject(new containerOrange[i], 642+(i*10), 80);
            }
        }
I try to give every object that spawns to have a different name this way. When I dont change anything to this code, it says that it expects an ] after
ContainerOrange containerOrange[
When I remove it. Like this:
if(1==1){
            for(int i = 0; i<100; i++) {
                CraneHook containerOrange[] = new ContainerOrange();
                addObject(new containerOrange[i], 642+(i*10), 80);
            }
        }
It says incompatible types highlighting new ContainerOrange line Any help would be awesome. Here is the code of the hook, just in case it is usefull.
if (Greenfoot.isKeyDown("space")&& isTouching(ContainerOrange.class)) {  
          if(liftedContainer==true)
          {
            Actor containerorange = getOneIntersectingObject(ContainerOrange.class);
            containerorange[i].setLocation(getX(),getY());
            return;
          }
          else
          {
            if (Greenfoot.isKeyDown("space")&& isTouching(ContainerOrange.class)) {  
           
           liftedContainer=true;
           return;
          }
          else
          {
              if (Greenfoot.isKeyDown("space")&& isTouching(ContainerOrange.class) && liftedContainer==true) {  
           
                  liftedContainer=false;
           
               }
          }
       }
       
       
       
        
    }
Super_Hippo Super_Hippo

2014/12/13

#
You are trying to create an array and not a loop, right? Try to add "private ContainerOrange containerOrange = new ContainerOrange" in the beginning of the code.
hhoogerwerf hhoogerwerf

2014/12/13

#
I hope I am saying this right I am trying to fill an array using a loop. I think I don't need the loop oops. When I add this to the code it still gives the same error private ContainerOrange containerOrange = new ContainerOrange();
if(1==1){
            
          ContainerOrange containerOrange[containerNumber] = new ContainerOrange();
          addObject(new containerOrange[containerNumber], 642, 80);
           containerNumber++; 
        } 
I am now using this code but it still won't let me add anything in the array, do I need to make it first?
hhoogerwerf hhoogerwerf

2014/12/13

#
http://www.greenfoot.org/topics/5816 so I tried this but it still switched between actors. How can I fix this? A variable in the container? Or something? Will be back in an hour or so. Thanks for helping.
Super_Hippo Super_Hippo

2014/12/13

#
Ah, I should have used code tags for the line. Otherwise the brackets won't work.
// outside methods because I think you want to use those references later
private ContainerOrange containerOrange[] = new ContainerOrange[100];

//why compare 1 to 1 here? 1 will always be 1
for(int i = 0; i<100; i++)
{
    ContainerOrange containerOrange[i] = new ContainerOrange();
    addObject(new containerOrange[i], 642+i*10, 80);
}
danpost danpost

2014/12/13

#
If you already have the array declared with something like the following previously in the class:
private ContainerOrange[] contanerOrange = new ContainerOrange[100];
then line 3 in your last post should be:
containerOrange[containerNumber] = new ContainerOrange();
You are not declaring the array on that line. And line 4 should be:
addObject(containerOrange[containerNumber], 642, 80);
You will already have the object created. You do not want to create another one here (although it should error the way it was and not create another one). If you are using line 4 inside the loop, all created ContainerOrange object will be placed one on top of another. The location coordinates should be probably be varied by the index of the loop somehow.
hhoogerwerf hhoogerwerf

2014/12/13

#
@ danpost thank you that worked you made a typing error in the post though, so for everybody looking for this, here is the code that works: private int containerNumber=0; private ContainerOrange containerOrange = new ContainerOrange; Somewhere in the class.
if(1==1){
             
          
          containerOrange[containerNumber] = new ContainerOrange();
          addObject(containerOrange[containerNumber], 642, 80);
           containerNumber++; 
        }
Right? Don't want to post the wrong thing here.
danpost danpost

2014/12/13

#
hhoogerwerf wrote...
private int containerNumber = 0;
private ContainerOrange[] containerOrange = new ContainerOrange[100];

// Somewhere in the class.

if (1 == 1)
{
    containerOrange[containerNumber] = new ContainerOrange();
    addObject(containerOrange[containerNumber], 642, 80);
    containerNumber++; 
}
I reposted your fix to include 'code' tags around the first two lines, which were not showing the square brackets or their contents in the second line.
Super_Hippo Super_Hippo

2014/12/13

#
I still don't see the reason for the if statement in line 6.
hhoogerwerf hhoogerwerf

2014/12/13

#
I will change it after I finish my code, I still need to code that bit. So for now I use 1==1. Just to check if the code works.
You need to login to post a reply.