last day (15 days later) » 

6:30 PM
-1
Q: Calling variables in private method Java Libgdx

user3165683Ok so i have a a method: private void method(){ Rectangle sprite = new Rectangle(); } And then making references to sprite.x etc in: public void render(float delta) { sprite.x } but on sprite.x i get the error: chicken cannot be resolved to a variable How can i call the sprite recta...

 
What class is method in and what class is render in?
Also, where is chicken coming from?
 
@Andrew_CS both in same OptionScreen class, chicken is the actual name for the sprite
 
Still not sure what you mean with chicken. I've updated my answer. Are you trying to call render in the main method of the OptionScreen class?
 
@Andrew_CS sprite takes the place of chicken in my game
 
You need to show the code that contains chicken if neither answer here has solved your issue.
 
6:30 PM
@Andrew_CS diffXchick = (float) (farmerX - chicken.x);
 
Show me the method that you are using that line in
 
7:04 PM
public void render(float delta) {
also look st the edit, thank u very much for your help
*at
 
You need to edit your question, don't do things like "ownCreate method:" . You need to show the entire method with the return type, method name, etc.
It's also unclear where your spawnChicken method ends
And fixing indentation in your question posts makes it so people like me can read it - since we don't know how your code is supposed to work
Where do you initialize the chicken variable?
The chicken variable that you are referencing in the spawnChicken method
 
My methods are setup as so:
public class OptionScreen implements Screen{
public OptionScreen(final Game1 gam){
private void spawnChicken() {
 
ok stop
 
public void render(float delta) {
 
What I'm saying is
in your question post
instead of
ownCreate method:



Array<Rectangle> chickens;
chickens = new Array<Rectangle>();
spawnChicken();
You should just put
public void ownCreate(){}
with the code inside
 
7:11 PM
Ok I see
 
you almost did it right for spawnChicken
except you never closed off the method
Can you just copy and paste your code into paste bin and give me a link so I can just look at everything?
 
Paste Bin? where do i fond this
 
You can put your code in there
And give me the link
And I can view your code
It will automatically expire after a while
Basically free web page for a little bit
 
is it worth becoming a member and making it private?
 
I wouldn't worry about it
No one is going to steal your code when it's broken
 
7:18 PM
haha, also sorry if its horrible as you may have guessed I am very new to Java and programming as a whole
 
no worries, just give me a sec to look through it
OK
Array<Rectangle> chickens;
Change Array to ArrayList
chickens = new Array<Rectangle>();
Change Array to ArrayList
 
OK
Still get an error on diffYchick = (float) (farmerY - chicken.y);
NullPointerException
 
Yeah I'm getting there
 
Ok Sorry
 
np
Where do you call render
 
7:30 PM
i dont :s
should i can i not just let it run through it?
 
What is render supposed to do - give me a real basic 2 sentence explanation
 
Primarily repeatedly draw assets to the screen to correct ratio size
 
This line
diffYchick = (float) (farmerY - chicken.y);
What chicken do you want chicken to be? Because every chicken is in the ArrayList chickens
So it doesn't know what chicken is
Are you supposed to call render for every chicken? Or is 1 render call supposed to take care of all of the chickens?
 
I want them all to do the same thing, follow the farmer and rotate to look at him as they do so
I believe 1 render would be more efficient wouldn't it?
 
So inside of render, somewhere you will have to do either a for loop or iterate over the chickens ArrayList, then you could say chicken = iter.next();
After you did that you would be able to call things like chicken.x
 
7:38 PM
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

}
does that not work
 
exactly
you see how you told the compiler that chicken is a Rectangle
 
Yes, change to arraylist?
 
and then you assigned a value to the chicken variable with iter.next()
well
you don't have to if it works with Array like that
I've never seen that before - but it might be part of the things you're importing from that website
 
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
}
I still get the error: java.lang.NullPointerException
 
On the same line?
Put your new code into past bin again
 
7:44 PM
yes: 152
 
Just curious what is the size of chickens?
nm
 
you aren't even compiling so that doesn't matter yet
Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

}
chicken only exists inside of that while loop
Do you know what scope is?
 
No?
 
scope basically defines where the object lives
ArrayList<Rectangle> chickens;
 
7:48 PM
OK
 
That ArrayList is an instance variable - it is declared inside of your class - outside of your methods
That means you can access it anywhere in the class that you want
 
Yes
 
Rectangle chicken = new Rectangle();
in spawnChicken
 
chicken's scope is that method - you declare the variable in the method, so that means you can access it anywhere inside of that method, but not outside
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

}
That while loop is in render
the scope is only for that while loop since it's declared inside
that means it doesn't exist outside of the loop
that's why you get an error when trying to use it outside of that while loop in the render method
make sense?
 
7:51 PM
Yes so can i get the variable from the private spawnChicken()? or how should i do it?
As i believe
Rectangle chicken = new Rectangle();
 
What you should do
is in your render method
 
has to be kept in the spawnChicken() to keep the old chicekn each time a new one comes on screen
 
Don't mess with spawnChicken
 
in render
keep that loop
all of the rest of the code in render
you need to figure out what needs to happen on each chicken
and move that inside of the loop
 
7:55 PM
Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
if(farmerX < (chicken.x - 85/2)) chicken.x -= 2.5;
if(farmerX > (chicken.x - 85/2))chicken.x += 2.5;
if (farmerY < (chicken.y - 66/2))chicken.y -= 2.5;
if(farmerY > (chicken.y - 66/2))chicken.y += 2.5;

float diffY;
float diffX;
diffY = (float) (touch.y - farmerY);
diffX = (float) (touch.x - farmerX);

float angleDegrees;

angleDegrees = (float) Math.toDegrees(Math.atan2(diffY, diffX));


}
Like that?
 
render(){
Make iterator

Loop over chickens{
Rectangle chicken = iter.next();

Do what you have to do that needs to be done on every chicken
For instance - update the position of the current chicken, rotation, etc.
}

Do whatever else needs to be done in the render method, that doesn't need to be done for each chicken
Such as move the farmer, etc.
}
 
Does that make sense?
 
Yes
 
Why don't you try that out and let me know how it goes
 
7:58 PM
OK thank u
 
You're welcome
 
pls look at line 136
for(Rectangle chicken: chickens){
game.batch.draw(chickenImage, (float)chickens.x, (float)chickens.y, (float)42.5, (float)33, (float)85, (float)66, (float)1, (float)1, (float)angleDegreeschick);
 
What about it
Hold on
 
On: Rectangle chiceken: chickens
Duplicate local variable chicken
ok
 
You already defined a variable chicken in your while loop
That for loop is in the scope of the while loop
so you can't re-use names
 
8:06 PM
ok changed to: game.batch.draw(chickenImage, (float)chicken.x, (float)chicken.y, (float)42.5, (float)33, (float)85, (float)66, (float)1, (float)1, (float)angleDegreeschick);
no errors but no chickens appear on screen?
But i think they are there as i still get the overlap message
 
so you got rid of that for loop?
 
yes
 
good I was going to tell you that next - had to run out for a minute
 
So the image not appearing is a whole seperate issue that you should look into, if you can't figure it out you should create a new question on SO.
I'm going to update my answer to your question post and you should mark it as accepted so other ppl don't try to keep answering your question
 
8:15 PM
will do
 
If you can you should update the question post to reflect what was the issue here
Do you know how to mark answers as accepted?
 
Yes
 
OK
Well glad I was able to help, good luck with the rest of your game.
 
Thank u for all your help should i model the question more around rendering properly and variables around loops?
 
Let me edit it
 
8:22 PM
Ok go for it :)
Thanks again
 
NP, you can look at how I edited the question if you want. Might help when you have to ask your next question.
 
Will do, as a side note do u mind if i ask how long it took u to become competatnt in java>
?
 
I'm a Computer Science student in college
so
I had a really good understanding of Java after 2 Java courses
So a year
I have to go, talk to you later
good luck
 
9:16 PM
Thanks, and good luck in the future. P.s. i am also interested in studying computer science in an American uni/college if you get the time have you got any advice?
 

last day (15 days later) »