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

2016/12/4

problem with array need help

1
2
jrod159 jrod159

2016/12/4

#
Tritos Tritos

2016/12/4

#
So what is your problem?
jrod159 jrod159

2016/12/4

#
for (int x = 0; x < enemys.length; x++)
        {
            for (int y = 0; y < enemys[x].length; y++)
            {
               enemys[x][y] = new Invaders("Marine.png");
               addObject(enemys[x][y], 50+35*y, 100*(x+1));
            }
            
        
        }
i keep getting an error under = new Invaders("Marine.png"); the error says "constructor Invaders in class Invaders cannot be applied to given types; required; no arguments found: java.lang.string reason: actual and formal argument lists differ in length" and i have no idea why i am getting this error
Tritos Tritos

2016/12/4

#
Well, the constructor in your Invaders class has no parameters, but you give it a parameter. To be exact you give it a String ("Marine.png"). Simply delete the inside of the bracket
for (int x = 0; x < enemys.length; x++)
        {
            for (int y = 0; y < enemys[x].length; y++)
            {
               enemys[x][y] = new Invaders();
               addObject(enemys[x][y], 50+35*y, 100*(x+1));
            }
             
         
        }
If you want to make the Invaders class look like the "Marine.png" you can just right click on the class and click "Set image". Then select "Marine.png".
jrod159 jrod159

2016/12/4

#
well was i trying to make it so that in my world 1 row images could be "Marine.png" while the row below that would be a Different image but still part of the invaders class
Tritos Tritos

2016/12/4

#
Well you could write a new constructor for you Invaders class. This constructor should intake a String and then set the Image to that String.
public Invaders(String b)
    {
        setImage(b);
    }
something like this should work with your first code.
Super_Hippo Super_Hippo

2016/12/4

#
You have to change the constructor (or add a new one) of the Invaders (should be singular) class then so it knows that the parameter you give should be used as its image.
public Invaders(String image)
{
    setImage(image);
}
Edit: Too slow :P
Tritos Tritos

2016/12/4

#
:D
jrod159 jrod159

2016/12/4

#
ok but when i do that i get the same error underneath = new Invaders(); as i did in my original code
Tritos Tritos

2016/12/4

#
Did you add the constructor we sent to the Invaders class? or to the class where you run the loop? It has to be in the Invaders class
jrod159 jrod159

2016/12/4

#
public Invaders(String "Marine.png")
    {
        setImage("Marine.png");
    }
ya i put that into the Invaders class
Tritos Tritos

2016/12/4

#
replace "Marine.png" with somthing different like b or image as suggested above
Tritos Tritos

2016/12/4

#
(leave "Marine.png" in the other class of course)
jrod159 jrod159

2016/12/4

#
ok i did that but that weird error is still underneath " = new Invaders();" and thank you so much for all your help man
Tritos Tritos

2016/12/4

#
You have to use your fitst version with
enemys[x][y] = new Invaders("Marines.png");
There are more replies on the next page.
1
2