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

2018/12/2

generalization

destroyer9909 destroyer9909

2018/12/2

#
if(Greenfoot.isKeyDown("d"))
        {
            turn++;
            if(turn % 3 == 0)
            {
                move(7);
                setImage(rightImages[turnI % rightImages.length]);
                turnI++;
            }
        }
        else if(Greenfoot.isKeyDown("a"))
        {
            turn++;
            if(turn % 3 == 0)
            {
                move(-7);
                setImage(leftImages[turnI % leftImages.length]);
                turnI++;
            }
        }
Is there any way to put
            turn++;
            if(turn % 3 == 0)
            {
                move(7);
                setImage(rightImages[turnI % rightImages.length]);
                turnI++;
            }
and
            turn++;
            if(turn % 3 == 0)
            {
                move(-7);
                setImage(leftImages[turnI % leftImages.length]);
                turnI++;
            }
  
into one method?
danpost danpost

2018/12/2

#
destroyer9909 wrote...
Is there any way to put << Codes Omitted >> into one method?
Yes; but you would need to pass a value to the method to indicate which set of images to use (and which way to move). Your current code is biased toward moving right. The following will get an unbiased moving direction and call your new method:
int dir = 0;
if (Greenfoot.isKeyDown("a")) dir--;
if (Greenfoot.isKeyDown("d")) dir++;
if (dir != 0) animateMove(dir);
The new method would be:
private void animateMove(int dir)
{
    turn++;
    if (turn % 3 != 0 return;
    move(7*dir);
    int[] animation = dir == -1 ? leftImages : rightImages;
    setImage(animation[turnI % animation.length]);
    turnI++;
}
As such, however, you may just call:
move();
from the act method and put the codes together in that method:
public void move()
{
    int dir = 0;
    if (Greenfoot.isKeyDown("a")) dir--;
    if (Greenfoot.isKeyDown("d")) dir++;
    if (dir == 0 || ++turn % 3 == 0) return;
    move(7*dir);
    int[] animation = dir == -1 ? leftImages : rightImages;
    setImage(animation[turnI++ % animation.length]);
}
destroyer9909 destroyer9909

2018/12/2

#
danpost wrote...
destroyer9909 wrote...
Is there any way to put << Codes Omitted >> into one method?
Yes; but you would need to pass a value to the method to indicate which set of images to use (and which way to move). Your current code is biased toward moving right. The following will get an unbiased moving direction and call your new method:
int dir = 0;
if (Greenfoot.isKeyDown("a")) dir--;
if (Greenfoot.isKeyDown("d")) dir++;
if (dir != 0) animateMove(dir);
The new method would be:
private void animateMove(int dir)
{
    turn++;
    if (turn % 3 != 0 return;
    move(7*dir);
    int[] animation = dir == -1 ? leftImages : rightImages;
    setImage(animation[turnI % animation.length]);
    turnI++;
}
As such, however, you may just call:
move();
from the act method and put the codes together in that method:
public void move()
{
    int dir = 0;
    if (Greenfoot.isKeyDown("a")) dir--;
    if (Greenfoot.isKeyDown("d")) dir++;
    if (dir == 0 || ++turn % 3 == 0) return;
    move(7*dir);
    int[] animation = dir == -1 ? leftImages : rightImages;
    setImage(animation[turnI++ % animation.length]);
}
So I have a question what does this line of code mean?
int[] animation = dir == -1 ? leftImages : rightImages;
danpost danpost

2018/12/2

#
E gads -- sorry. That should be:
GreenfootImage[] animation = dir == -1 ? leftImages : rightImages;
It grabs a reference to the correct set of images to use (left or right) depending on the direction moved.
destroyer9909 destroyer9909

2018/12/2

#
ah thank you so much!
You need to login to post a reply.