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

2015/3/9

How can I have an actor do something different on the same click?

decadence18 decadence18

2015/3/9

#
If I have an actor and I double click it, it expands. I want to be able to double click it again, and it shrink back to the normal size. Is there any possible way of doing this?
danpost danpost

2015/3/9

#
decadence18 wrote...
If I have an actor and I double click it, it expands. I want to be able to double click it again, and it shrink back to the normal size. Is there any possible way of doing this?
You can use basic arithmetic operations to toggle between two values. For example:
1
2
3
4
5
6
7
8
9
// using these int fields
int ten = 10; // larger size of a dimension
int five = 5; // smaller size of the same dimension
int toggleValue = five; // current dimension size
// the following will toggle the 'toggleValue' field between the two values, 'ten' and 'five':
int sumValue = ten+five;
toggleValue = sumValue-toggleValue;
// or simply
toggleValue = 15-toggleValue;
So which ever value it was, it will now be the other. Repeating the operation will return the field to its previous value. Just apply this method to the widths and heights of the two sizes.
decadence18 decadence18

2015/3/9

#
danpost wrote...
You can use basic arithmetic operations to toggle between two values. For example:
1
2
3
4
5
6
7
8
9
// using these int fields
int ten = 10; // larger size of a dimension
int five = 5; // smaller size of the same dimension
int toggleValue = five; // current dimension size
// the following will toggle the 'toggleValue' field between the two values, 'ten' and 'five':
int sumValue = ten+five;
toggleValue = sumValue-toggleValue;
// or simply
toggleValue = 15-toggleValue;
So which ever value it was, it will now be the other. Repeating the operation will return the field to its previous value. Just apply this method to the widths and heights of the two sizes.
What I'm actually attempting to do is change between two images on a double click; The larger image has more data on it than the smaller one. My current code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse.getClickCount() == 2)
            {
                int ten = 10; // larger size of a dimension
                int five = 5; // smaller size of the same dimension
                int toggleValue = five; // current dimension size
                int sumValue = ten+five;
                toggleValue = sumValue-toggleValue;
                if (toggleValue == 10)
                {
                    GreenfootImage image = new GreenfootImage("German SdKfz 234_1 Edit.png");
                    setImage(image);
                }
                else if (toggleValue == 5)
                {
                    GreenfootImage image2 = new GreenfootImage("German SdKfz 234_1 Less.png");
                    image2.scale(75, 75);
                    setImage(image2);
                }
            }
        }
(All of that is in the Act method) However, when I add that code in the class, it still only works for the first double-click. Am i possibly using your code the wrong way? I know it's a bit different than what I may have initially lead you to think; I apologize.
danpost danpost

2015/3/9

#
In my code-post above, lines 2 through 4 are supposed to be fields in the class (declared outside of any method, but still within the class brackets). Then, line 6 was only given to exemplify the following procedure for the toggling of the numbers. Throwing it all together like you did will not work because line 8 in you code will always set the value of the field to the same initial number for line 10 to deal with. By moving your lines 6 through 9 outside the method, they are only set once, when the object of the class is created. Since you only have two states and you do not really need to save any particular int values, we will use zero and one, which will be saved by using one field, I will call it 'imageNumber':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// the field (outside any method, but inside the class)
private int imageNumber;
// your if block above, revised
if (Greenfoot.mouseClicked(this))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse.getClickCount() == 2)
    {
        imageNumber = 1-imageNumber; // toggle value between zero and one
        String suffix = "Edit"; // assume zero value
        if (imageNumber == 1) suffix = "Less"; // adjust if one value
        setImage(new GreenfootImage("German SdKfz 234_1 "+suffix+".png")); // set image
        if (imageNumber == 1) getImage().scale(75, 75); // scale if one value
    }
}
Lines 10 and 11 could be written combined as:
1
String suffix = imageNumber == 0 ? "Edit" : "Less";
which sets the value of 'suffix' depending on the value of 'imageNumber'.
decadence18 decadence18

2015/3/11

#
danpost wrote...
In my code-post above, lines 2 through 4 are supposed to be fields in the class (declared outside of any method, but still within the class brackets). Then, line 6 was only given to exemplify the following procedure for the toggling of the numbers. Throwing it all together like you did will not work because line 8 in you code will always set the value of the field to the same initial number for line 10 to deal with. By moving your lines 6 through 9 outside the method, they are only set once, when the object of the class is created. Since you only have two states and you do not really need to save any particular int values, we will use zero and one, which will be saved by using one field, I will call it 'imageNumber'...
Thank you, I tried using it in the Act method, falsely assuming it was permanently saving the value. The new code works as intended.
You need to login to post a reply.