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

2019/3/6

Need help setting image in act()

Game4Lols Game4Lols

2019/3/6

#
So I have an actor that has 11 different possible images/costumes, each for a different digit and an extra which is just a black square. If a value is equal to 4, the actor should switch to "num4.png," and look like a 4. This works, but only once. If the value changes to say 8 while the program is running, the actor stays as a 4. If I change the starting value from 4 to say 6, it will become a 6 when the program starts but will stay as a 6 even if the variable changes. I don't know how to fix this and it's been really annoying. Here's my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;
 
public class Digits extends Actor
{
    int digits = 27;
    int[] mA = {4560,0,0,1230,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0}; // Value 0 isn't shown ingame, nor is value 28.
    public void act()
    {
        for (int i = 0; i < 28; i++) //if mA[0] <= -1, WHOLE number is negitive. If mA[28] >= 1, Interger overflow.
        {
            if (mA[i] <= -1)
            {
                mA[i+1]--;
                mA[i] += 10;
            }
            if (mA[i] >= 10)
            {
                mA[i+1]++;
                mA[i] -= 10;
            }
        }            
    }
    public void adjustMoney(int m, int d) // m = money to add, d = what digit the money should be added to.
    {                
        mA[d] += m;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;
 
public class Digit1 extends Digits
{
    static int iam = 1;
    public void act()
    {
        if (mA[iam] > 9 || mA[iam] < 0 || iam > digits)
            setImage("num10.png");
        else
            setImage("num" + mA[iam] + ".png");
    }   
}
Possible images that digit1 can be are num0.png to num10.png. 10 is just a black square. The value is stored in an array, but digit1 will only look at the second value, hence why iam = 1; My code is mostly incomplete, so this might seem a bit weird, I dunno. But any help as to why Digit1 won't change appearance more than once would be nice. Also the idea of the array is that eventually, {4560,0,0,1230,0,0,0} etc will become {0,6,5,4,3,2,1}, but because the second value is a 0, Digit1 will switch to num0.png and when it's finished changing and the second array value is 6, digit1 is still using num0.png and not num6.png.
danpost danpost

2019/3/7

#
Looking over your codes, I have found several things of note. One, not relevant to your current issue is the order of the conditions in line 8 of your Digit1 class. The last condition should be checked first; otherwise, either one of the other conditions will throw an IndexOutOfBounsException. The next is line 5 in the Digit1 class. It is not clear what that variable is for or why it is a class value (declared static). Next, the content of the Digits class seems out of place. It does not appear to deal with the individual digits, but more for groups of digits. Any code in that class should describe a Digits object. In fact, each Digits object you create (from any subclass of Digits), will have its own array built for it. So, any change one digit might do to its array will not affect the arrays belonging to the other digits. Finally (and this is a big one to understand), unless you create a Digits object directly (one not being an instance of one of its subclasses), its act method will never execute if all subclasses have an act method. In other words, when a Digit1 object acts, the act method in the Digit1 class executes on that object because it overrides the act method in the Digits class. You only need one Digit class without it having any extensions; and, your World subclass should probably contain the array and act upon it. It may also use an array for your Digit objects, so it can tell them what to display. In fact, the Digit class could be a blank class (void of any fields or methods). Your world could set the images of these objects directly.
Game4Lols Game4Lols

2019/3/7

#
I changed the order of line 8, and I don't know why line 5 was static. I got rid of it but iam exists because I want to have digit2 and digit3 all the way to 27, hence the array with 27+ values. But regardless, I moved my digits code to my world subclass, and used ((IntOF)getWorld()).getValue(iam) to get the Array value. It worked PERFECTLY. Thanks so much :)
You need to login to post a reply.