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);
}
Related
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
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]);
}
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;
}
In order to make a copy of an array that holds a copy of the values instead of by reference I do the following:
int[][][] copy = {{{0}},{{0}},{{0,0}},{{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}},{{0}},{{0,0}}};
System.arraycopy(spelerSpel, 0, copy, 0, spelerSpel.length);
then change a value IN THE COPY:
copy[SPELER_NUMMER][0][0] = baanSpelerNummer;
which results at that moment in the ORIGINAL (spelerSpel) array holding the same changed value, e.g.:
{{{4}},{{0}},{{0,0}},{{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}},{{0}},{{0,0}}};
as the copy. I also tried .clone(), same result. What am I doing wrong?
By the way I target Android API 8, which doe not support Arrays.copyOf().
System.arraycopy() does not support deep copies, but it does pretty well in terms of performance for simple arrays.
You can use it with some additional loops to create your own multi-dimensional arraycopy3d():
public int[][][] arraycopy3d(int[][][] array) {
int[][][] copy = new int[array.length][][];
for (int i = 0; i < array.length; i++) {
copy[i] = new int[array[i].length][];
for (int j = 0; j < array[i].length; j++) {
copy[i][j] = new int[array[i][j].length];
System.arraycopy(array[i][j], 0, copy[i][j], 0, array[i][j].length);
}
}
return copy;
}
how am I going to move the value of a char array to the same char array? Here's a code:
Assuming ctr_r1=1 ,
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
letters[ctr_x] = letters[ctr_x];
}
sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);
append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.
what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).
The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.
I tried doing this:
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
lettersr1[ctr_x] = letters[ctr_x];
}
sb.append(lettersr1);
but my app crashes when i do that. Any help please. Thanks!
I don't understand what you're trying to do, but I can comment on your code:
letters[ctr_x] = letters[ctr_x];
This is a noop: it sets an array element value to the value it already has.
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
lettersr1[ctr_x] = letters[ctr_x];
This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:
char[] lettersr1 = new char[someLength];
Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.
EDIT:
I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:
char[] eucharist = "EUCHARIST".toCharArray();
char[] harist = new char[6];
System.arraycopy(eucharist, 3, harist, 0, 6);
String haristAsString = new String(harist);
System.out.println(haristAsString);
// or
char[] harist2 = new char[6];
for (int i = 0; i < 6; i++) {
harist2[i] = eucharist[i + 3];
}
String harist2AsString = new String(harist2);
System.out.println(harist2AsString);
// or
String harist3AsString = "EUCHARIST".substring(3);
char[] harist3 = harist3AsString.toCharArray();
System.out.println(harist3AsString);
May be so:
String str = "EUCHARIST";
str = str.substring(3);
and after toCharArray() or smth another