AndEngine - can't iterate through array items inside handler calls - android

I am having a problem working inside a handler call. The problem is something as follows:
Sprite sprite = new Sprite[spriteArrayLength];
IUpdateHandler mm[i] = new ........// you know what
for(int i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = sprite[i].getX();//problem rises here
};
sprite[i].registerUpdateHandler(mm[i]);
}
Every time it shows an error saying i reaches out of array bound. That means the handler call executes after for loop ends and so i already crosses its limit. How can I do something like above in a proper way?
Edit:
Sorry for my previous mistake. The First line of the code will be:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new ........// you know what
not this:
Sprite sprite = new Sprite[spriteArrayLength];
IUpdateHandler mm[i] = new ........// you know what
I just thought these line are not very important to mention that's why made the mistake. But still the problem remains same.
Edit-2:
I get "array out of bound" type run time error. Let's say, array size is 6. So, last element is 5. But inside UpdateHandler "i" begins with 6 and throws an error. And I tried making "i" final, even made it global by declaring as a class field. I am trying to write short code examples here because it contains many codes. Better version is as follows:
public int i;//global declaration
//inside some method:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new IUpdateHandler[spriteArrayLength];
for(i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = sprite[i].getX();//problem rises here
};
sprite[i].registerUpdateHandler(mm[i]);
}

Based on the code in your Edit-2, my suggestion is:
Sprite sprite[] = new Sprite[spriteArrayLength];
IUpdateHandler mm[] = new IUpdateHandler[spriteArrayLength];
for(i = 0; i < spriteArrayLength; i++){
//many other actions
mm[i] = new IUpdateHandler() {
private final int id = i; /* add this line */
// call Log.i() here is compiling error
#Override
public void onUpdate(float pSecondElapsed) {
//do somthing with sprite array items
/* And access id, instead of i in below code.
Here, I assume this statement is located
within onUpdate() or reset().
*/
float anyVar = sprite[id].getX();
}
#Override
public void reset() {
}
};
sprite[i].registerUpdateHandler(mm[i]);
}

You are using the index variable i in 2 different contexts.
In your code you do this before the loop using i as an array index:
IUpdateHandler mm[i] = new ........// you know what
then inside the loop, you are changing the value of i and referencing mm[i] and sprite[i].
I assume that you haven't correctly initialized the mm array to the correct size (spriteArrayLength)

The problem is that your update handler holds a reference to variable i but isn't evaluated until after the loop finishes. Instead of holding a reference to i, give it a reference to the specific sprite, and for readability make it final to avoid any doubt that it could change:
for(i = 0; i < spriteArrayLength; i++){
final Sprite currentSprite = sprite[i];
//many other actions
mm[i] = new IUpdateHandler() {
//do somthing with sprite array items
float anyVar = currentSprite.getX();
};
currentSprite.registerUpdateHandler(mm[i]);
}

Related

ArrayList<HashMap<String,String>> Overwrite the items in list [duplicate]

I'm adding three different objects to an ArrayList, but the list contains three copies of the last object I added.
For example:
for (Foo f : list) {
System.out.println(f.getValue());
}
Expected:
0
1
2
Actual:
2
2
2
What mistake have I made?
Note: this is designed to be a canonical Q&A for the numerous similar issues that arise on this site.
This problem has two typical causes:
Static fields used by the objects you stored in the list
Accidentally adding the same object to the list
Static Fields
If the objects in your list store data in static fields, each object in your list will appear to be the same because they hold the same values. Consider the class below:
public class Foo {
private static int value;
// ^^^^^^------------ - Here's the problem!
public Foo(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
In that example, there is only one int value which is shared between all instances of Foo because it is declared static. (See "Understanding Class Members" tutorial.)
If you add multiple Foo objects to a list using the code below, each instance will return 3 from a call to getValue():
for (int i = 0; i < 4; i++) {
list.add(new Foo(i));
}
The solution is simple - don't use the static keywords for fields in your class unless you actually want the values shared between every instance of that class.
Adding the Same Object
If you add a temporary variable to a list, you must create a new instance of the object you are adding, each time you loop. Consider the following erroneous code snippet:
List<Foo> list = new ArrayList<Foo>();
Foo tmp = new Foo();
for (int i = 0; i < 3; i++) {
tmp.setValue(i);
list.add(tmp);
}
Here, the tmp object was constructed outside the loop. As a result, the same object instance is being added to the list three times. The instance will hold the value 2, because that was the value passed during the last call to setValue().
To fix this, just move the object construction inside the loop:
List<Foo> list = new ArrayList<Foo>();
for (int i = 0; i < 3; i++) {
Foo tmp = new Foo(); // <-- fresh instance!
tmp.setValue(i);
list.add(tmp);
}
Your problem is with the type static which requires a new initialization every time a loop is iterated. If you are in a loop it is better to keep the concrete initialization inside the loop.
List<Object> objects = new ArrayList<>();
for (int i = 0; i < length_you_want; i++) {
SomeStaticClass myStaticObject = new SomeStaticClass();
myStaticObject.tag = i;
// Do stuff with myStaticObject
objects.add(myStaticClass);
}
Instead of:
List<Object> objects = new ArrayList<>();
SomeStaticClass myStaticObject = new SomeStaticClass();
for (int i = 0; i < length; i++) {
myStaticObject.tag = i;
// Do stuff with myStaticObject
objects.add(myStaticClass);
// This will duplicate the last item "length" times
}
Here tag is a variable in SomeStaticClass to check the validity of the above snippet; you can have some other implementation based on your use case.
Had the same trouble with the calendar instance.
Wrong code:
Calendar myCalendar = Calendar.getInstance();
for (int days = 0; days < daysPerWeek; days++) {
myCalendar.add(Calendar.DAY_OF_YEAR, 1);
// In the next line lies the error
Calendar newCal = myCalendar;
calendarList.add(newCal);
}
You have to create a NEW object of the calendar, which can be done with calendar.clone();
Calendar myCalendar = Calendar.getInstance();
for (int days = 0; days < daysPerWeek; days++) {
myCalendar.add(Calendar.DAY_OF_YEAR, 1);
// RIGHT WAY
Calendar newCal = (Calendar) myCalendar.clone();
calendarList.add(newCal);
}
Every time you add an object to an ArrayList, make sure you add a new object and not already used object. What is happening is that when you add the same 1 copy of object, that same object is added to different positions in an ArrayList. And when you make change to one, because the same copy is added over and over again, all the copies get affected.
For example,
Say you have an ArrayList like this:
ArrayList<Card> list = new ArrayList<Card>();
Card c = new Card();
Now if you add this Card c to list, it will be added no problem. It will be saved at location 0. But, when you save the same Card c in the list, it will be saved at location 1. So remember that you added same 1 object to two different locations in a list. Now if you make a change that Card object c, the objects in a list at location 0 and 1 will also reflect that change, because they are the same object.
One solution would be to make a constructor in Card class, that accepts another Card object. Then in that constructor, you can set the properties like this:
public Card(Card c){
this.property1 = c.getProperty1();
this.property2 = c.getProperty2();
... //add all the properties that you have in this class Card this way
}
And lets say you have the same 1 copy of Card, so at the time of adding a new object, you can do this:
list.add(new Card(nameOfTheCardObjectThatYouWantADifferentCopyOf));
It can also consequence of using the same reference instead of using a new one.
List<Foo> list = new ArrayList<Foo>();
setdata();
......
public void setdata(int i) {
Foo temp = new Foo();
tmp.setValue(i);
list.add(tmp);
}
Instead of:
List<Foo> list = new ArrayList<Foo>();
Foo temp = new Foo();
setdata();
......
public void setdata(int i) {
tmp.setValue(i);
list.add(tmp);
}

Libgdx - TextureRegion.flip()

I have a problem with TextureRegion. When I fliped TextureAtlas, my character will move only in right direction (where TextureAtlas was flipped), and won't go back to left direction. Does anybody have an idea how to solve this problem?
Thank you.
Code is below:
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("texture/textures.pack"));
TextureRegion[] walkLeftFrame = new TextureRegion[5];
for(int i = 0 ; i<5; i++){
walkLeftFrame[i] = atlas.findRegion("bob-0"+(i+2));
}
walkLeftAnimation = new Animation(RUNNING_FRAME_DURATION , walkLeftFrame);
TextureRegion[] walkRightFrame = new TextureRegion[5];
for(int i=0; i<5; i++) {
walkRightFrame[i] = atlas.findRegion("bob-0" + (i + 2));
walkRightFrame[i].flip(true, false);
}
walkRightAnimation = new Animation(RUNNING_FRAME_DURATION, walkRightFrame);
if(bob.isFacingLeft())
bobFrame = walkLeftAnimation.getKeyFrame(bob.getStateTime(), true);
else
bobFrame = walkRightAnimation.getKeyFrame(bob.getStateTime(), true);
spriteBatch.draw(bobFrame, bob.getPosition().x * ppuX , bob.getPosition().y * ppuY , Bob.SIZE *ppuX, Bob.SIZE * ppuY);
That is the typical call by reference problem.
call by value
In call by value, a copy of actual arguments is passed to formal
arguments of the called function and any change made to the formal
arguments in the called function have no effect on the values of
actual arguments in the calling function.
call by reference
In call by reference, the location (address) of
actual arguments is passed to formal arguments of the called function.
This means by accessing the addresses of actual arguments we can alter
them within from the called function.
That means you are accessing the same TextureRegions in both arrays. That's why all your Regions are flipped.
Something like this should work:
TextureRegion[] walkRightFrame = new TextureRegion[5];
for(int i=0; i<5; i++) {
walkRightFrame[i] = new TextureRegion(atlas.findRegion("bob-0" + (i + 2)));
walkRightFrame[i].flip(true, false);
}

Cursor is looping but repeats only the first value

I created a loop that will get the data from my cursor, however I noticed that even though it is looping(with the correct count) it only shows the first value.
int vv = 0;
if ((CR3.moveToFirst()) || CR3.getCount() !=0){
while (CR3.isAfterLast() == false) {
vendoName[vv] = CR3.getString(0);
vendoEsch[vv] = CR3.getString(1);
vendoAsch[vv] = CR3.getString(2);
vendoTag[vv] = CR3.getString(3);
vv++;
CR3.moveToNext();
}}
and when I call all my data( I only need the first three records)
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
SearchResults2 sr2 = new SearchResults2();
for(int j = 0;j < 3;j++)
{
sr2.setName(vendoName[j]);
sr2.setEsch(vendoEsch[j]);
sr2.setAsch(vendoAsch[j]);
sr2.setTag(vendoTag[j]);
results2.add(sr2);
}
I am putting inside a listview, when I check, it is showing always the first data.
This is an example I used as a reference to my code(It's almost the same except that I used an array to put my data from)
http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
Am I doing something wrong which is why it is only getting the first piece of data?
Is it not easier to do something like this (if you don't need more than 3 results):
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
CR3.moveToFirst();
for (i = 0; i < 3; i++) {
SearchResults2 sr2 = new SearchResults2();
sr2.setName(CR3.getString(0));
sr2.setEsch(CR3.getString(1));
sr2.setAsch(CR3.getString(2));
sr2.setTag(CR3.getString(3));
results2.add(sr2);
CR3.moveToNext();
}
I think that maybe the cursor doesn't iterate properly through your results in your while-loop and that's why you become one and the same result for the three items

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

Array Being Overwritten with Last Index in Loop

I'm working on code that takes two arrays with strings (the strings are just sentences) and allocates them to classes which are held in another array (The Sentence class array shown below in the code).
So here's my problem. When popList() is called, the for loop runs through twice and works fine, putting the first index of addStrings and addTranslation into the first class in the array. However, when the loop indexes up and runs temp.sentence = addStrings[1] again, it OVERRIDES the first class's .sentence also. Then when temp.translations = addTranslations[1] runs again it OVERRIDES the first class's .translation.
So by the end of the loop, all of the arrays are filled with the same thing: the last index of addStrings and addTranslation. Every time it loops it overwrites all the indices before it with the index it's supposed to be putting in.
Anyone know what the problem is here? Thanks!
public class Sentence {
public String sentence;
public String translation;
Sentence() {
sentence = " ";
translation = " ";
}
}
private void popStrings() {
addStrings[0] = "我是你的朋友。"; addTranslations[0] = "I am your friend.";
addStrings[1] = "你可以帮助我吗?"; addTranslations[1] = "Could you help me?";
addStrings[2] = "我不想吃啊!"; addTranslations[2] = "I don't want to eat!";
}
//Fill Sentence array with string and translation arrays
private void popList() {
int i = 0;
Sentence temp = new Sentence();
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
}
You need to create new Sentence() inside the loop:
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
Sentence temp = new Sentence();
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
Otherwise you set sentence and translation continuously in the same object.

Categories

Resources