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

2021/5/21

how making this array to be looping?

mariq_rasyid29 mariq_rasyid29

2021/5/21

#
 private  GreenfootImage MoveLeftImage= getImage();
    private String[] MoveLeftImages=
        {   "Move left 1.png","Move left 2.png", "Move left 3.png","Move left 1.png",
        };
    private int currentMoveLeftImage = 0;
im used the second code but
 public void ImageMoveleft()
    {
        currentMoveLeftImage++;
        if(currentMoveLeftImage++ >= MoveLeftImages.length)
            currentMoveLeftImage=0;

        GreenfootImage newImage = new GreenfootImage(MoveLeftImages[currentMoveLeftImage]);
        setImage(newImage);
        MoveLeftImage=getImage();
    }
show this in terminal
java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
	at Blizzard.ImageMoveleft(Blizzard.java:130)
	at Blizzard.moveLeft(Blizzard.java:95)
	at Blizzard.KeyboardCheck(Blizzard.java:116)
	at Blizzard.act(Blizzard.java:51)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
this is all Blizzard code
import greenfoot.*;
import java.util.List; 
/**
 * Write a description of class Player here.
 * 
 * Muhamad Ariq Rasyid 
 * 1.0, 5 Feb 521
 */
public class Blizzard extends Actor
{   private int Level=1;
    private int Health=100;
    public int Defense=5;
    public int Attack=10;
    public int Exp=0;
    GreenfootSound LvUp= new GreenfootSound("level_up (online-audio-converter.com).mp3");

    private GreenfootImage MetalWolfMoveLeft = getImage();
    private  String[] MetalWolfMoveLeftImages=
        { "Metalgarurumon move left 1.png","Metalgarurumon move left 2.png", "Metalgarurumon move left 3.png", "Metalgarurumon move left 1.png"
        };
    private int currentMetalWolfMoverLeftImage = 0;

    private GreenfootImage MetalWolfMoverRight=getImage();
    private  String[]MetalWolfMoverRightImages=
        { "Metalgarurumon move right 1.png","Metalgarurumon move right 2.png","Metalgarurumon move right 3.png","Metalgarurumon move right 1.png",       
        };
    private int currentMetalWolfMoverRightImage = 0;

    private  GreenfootImage HowlingImage=getImage();
    private String[]Howling=
        {"Sit Left.png","Howl.png","Sit Left.png",
        };
    private int currentHowlImage = 0;

    private  GreenfootImage MoveLeftImage= getImage();
    private String[] MoveLeftImages=
        {   "Move left 1.png","Move left 2.png", "Move left 3.png","Move left 1.png",
        };
    private int currentMoveLeftImage = 0;

    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        TimberWolf();
        KeyboardCheck();
        Howling();
        GameOver();
        showstatistic();
        if(Level==5)
        {MetalWolf();
        };

    } 

    public void showstatistic()
    {if (getWorld() instanceof Dugeons)
        {getWorld().showText("Health :" +Health, 50, 30);
            getWorld().showText("EXP :" +Exp ,130, 30);
            getWorld().showText("LV :" +Level, 180, 30);
        }
    }    

    public void GameOver()
    {if (Health<=0)
        {Greenfoot.setWorld(new Gameover());
            ((Dugeons)getWorld()).Back.stop();
        }
    }

    public void Howling()
    {if (getWorld() instanceof MoonlightClift) Howl();
    }

    public void Howl()
    {  GreenfootSound WolfHowls= new GreenfootSound( "wolfhowl.wav" );
        if(Greenfoot.isKeyDown("H")) WolfHowls.play();
    }

    public void moveUp()
    {setLocation (getX(),getY()-5);
    }

    public void moveDown()
    {setLocation (getX(),getY()+5);
    }

    public void moveLeft()
    {setLocation (getX()-5,getY());
        ImageMoveleft();
    }

    public void moveRight()
    {setLocation (getX()+5,getY());
    }

    public void KeyboardCheck()
    {if (Greenfoot.isKeyDown("s")) 
        {
            if (Mapschecks(0, +5))
                moveDown();
        }
        if (Greenfoot.isKeyDown("w"))
        { 
            if (Mapschecks(0, -5))
                moveUp();}
        if (Greenfoot.isKeyDown("a")) 
        {

            if (Mapschecks(-5, 0))
                moveLeft();
        }
        if (Greenfoot.isKeyDown("d")) 
        {if (Mapschecks(+5, 0))
                moveRight();}

    }

    public void ImageMoveleft()
    {
        currentMoveLeftImage++;
        if(currentMoveLeftImage++ >= MoveLeftImages.length)
            currentMoveLeftImage=0;

        GreenfootImage newImage = new GreenfootImage(MoveLeftImages[currentMoveLeftImage]);
        setImage(newImage);
        MoveLeftImage=getImage();
    }

    public boolean Mapschecks(int x, int y)
    {
        Actor Map= getOneObjectAtOffset(x, y, Map.class );
        return(Map==null);
    }

    public void MetalWolf()
    {if(Greenfoot.isKeyDown("m"))
        {setImage("metalgarurumon2.gif");
            this.Health+=200;
        }
    }

    public void TimberWolf()
    {if(Greenfoot.isKeyDown("t"))
        { setImage("Timberwolf.png");
            Health=this.Health; 
        }
    }
}
danpost danpost

2021/5/21

#
Remove the final comma ( "," ) after the last element in the declared MoveLeftImages array. Then try this:
 public void ImageMoveleft()
{
    currentMoveLeftImage = (currentMoveLeftImage+1)%MoveLeftImages.length;
    GreenfootImage newImage = new GreenfootImage(MoveLeftImages[currentMoveLeftImage]);
    setImage(newImage);
}
mariq_rasyid29 mariq_rasyid29

2021/5/21

#
solved thx sir
You need to login to post a reply.