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

2019/5/14

Moving Actors Diagonally without turning and cycling through photos problem

NerdWithProblems NerdWithProblems

2019/5/14

#
I'm having problems with this code. So I know that greenfoot has restrictions. I'm wondering if it is possible for a player to go diagonally without rotating the image, because the code I'm using doesn't work. When I set the location "setLocation(x-1,y-1)" it only changes in the x-direction. Also, I need to cycle through images in a similar format of the game Robotron but the images only change on the initial "isKeyDown". I set each image as "Right1", "Right2" and etc. but it only changes to the first one and doesn't cycle through the rest. If you could help that would be great.
public void movement()
    {
        //direction movement
        int ay = getY();
        int ax = getX();
        int move = 5;
        int pictureChange = 9;
        int counter = 1;
        int counter2 = 1;
        int counter3 = 1;
        int counter4 = 1;
        int counter5 = 1;
        int counter6 = 1;
        int counter7 = 1;
        int counter8 = 1;
        int picLib = 1;
        String dirImage = "";
        if(Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("a"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax-1,ay-1);
                counter++;
                if(counter == 10)
                {
                    setImage(new GreenfootImage("Left1" + ".png"));
                }
                if(counter == 20)
                {
                    setImage(new GreenfootImage("Left2" + ".png"));
                }
                if(counter == 30)
                {
                    setImage(new GreenfootImage("Left3" + ".png"));
                }
                if(counter > 40)
                {
                    counter = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("s"))
        {
            for(int a = 0; a < 900; a++)
            {
                moving();
                //setLocation(ax-1,ay+1);
                counter2++;
                if(counter2 == 10)
                {
                    setImage(new GreenfootImage("Left1" + ".png"));
                }
                if(counter2 == 20)
                {
                    setImage(new GreenfootImage("Left2" + ".png"));
                }
                if(counter2 == 30)
                {
                    setImage(new GreenfootImage("Left3" + ".png"));
                }
                if(counter2 > 40)
                {
                    counter2 = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("d"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax+1,ay+1);
                counter3++;
                if(counter3 == 10)
                {
                    setImage(new GreenfootImage("Right1" + ".png"));
                }
                if(counter3 == 20)
                {
                    setImage(new GreenfootImage("Right2" + ".png"));
                }
                if(counter3 == 30)
                {
                    setImage(new GreenfootImage("Right3" + ".png"));
                }
                if(counter3 > 40)
                {
                    counter3 = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("d") && Greenfoot.isKeyDown("w"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax+1,ay-5);
                counter4++;
                if(counter4 == 10)
                {
                    setImage(new GreenfootImage("Right1" + ".png"));
                }
                if(counter4 == 20)
                {
                    setImage(new GreenfootImage("Right2" + ".png"));
                }
                if(counter4 == 30)
                {
                    setImage(new GreenfootImage("Right3" + ".png"));
                }
                if(counter4 > 40)
                {
                    counter4 = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("w"))
        {
            for(int a = 0; a < 4; a++)
            {
                setLocation(ax,ay-1);
                counter5++;
                if(counter5 == 1)
                {
                    setImage(new GreenfootImage("Back1" + ".png"));
                }
                if(counter5 == 2)
                {
                    setImage(new GreenfootImage("Back2" + ".png"));
                }
                if(counter5 == 3)
                {
                    setImage(new GreenfootImage("Back3" + ".png"));
                    counter5 = 1;
                }
                //if(counter5 > 40)
                //{
                //    counter5 = 1;
                //}
            }

        }
        if(Greenfoot.isKeyDown("a"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax-1,ay);
                counter6++;
                if(counter6 == 10)
                {
                    setImage(new GreenfootImage("Left1" + ".png"));
                }
                if(counter6 == 20)
                {
                    setImage(new GreenfootImage("Left2" + ".png"));
                }
                if(counter6 == 30)
                {
                    setImage(new GreenfootImage("Left3" + ".png"));
                }
                if(counter6 > 40)
                {
                    counter6 = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("s"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax,ay+1);
                counter7++;
                if(counter7 == 10)
                {
                    setImage(new GreenfootImage("Front1" + ".png"));
                }
                if(counter7 == 20)
                {
                    setImage(new GreenfootImage("Front2" + ".png"));
                }
                if(counter7 == 30)
                {
                    setImage(new GreenfootImage("Front3" + ".png"));
                }
                if(counter7 > 40)
                {
                    counter7 = 1;
                }
            }

        }
        if(Greenfoot.isKeyDown("d"))
        {
            for(int a = 0; a < 900; a++)
            {
                setLocation(ax+1,ay);
                counter8++;
                if(counter8 == 10)
                {
                    setImage(new GreenfootImage("Right1" + ".png"));
                }
                if(counter8 == 20)
                {
                    setImage(new GreenfootImage("Right2" + ".png"));
                }
                if(counter8 == 30)
                {
                    setImage(new GreenfootImage("Right3" + ".png"));
                }
                if(counter8 > 40)
                {
                    counter8 = 1;
                }
            }

        }
NerdWithProblems NerdWithProblems

2019/5/14

#
Sorry, I was experimenting on "isKeyDown("w")" so it is a bit different than the rest but still the same.
danpost danpost

2019/5/14

#
Remove your loops.
danpost danpost

2019/5/14

#
A lot of code for doing quite little. The following would probably have sufficed:
public void movement()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("a")) dx--;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("w")) dy--;
    if (Greenfoot.isKeyDown("s")) dy++;
    if (dx == 0 && dy == 0) return;
    counter = (counter+1)%30;
    String img = (dy == -1 ? "Back" :  "Front");
    img = (dx == 0 ? img : (dx == -1 ? "Left" : "Right"))+(1+counter/10)+".png";
    setImage(img);
    setLocation(getX()+dx, getY()+dy);
}
You need to login to post a reply.