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

2021/2/4

Setting multiple images for an Actor

XApple15 XApple15

2021/2/4

#
Hi ! How do I set up multiple images to be the Image of an Actor one by one ?
danpost danpost

2021/2/4

#
XApple15 wrote...
Hi ! How do I set up multiple images to be the Image of an Actor one by one ?
Easiest is to put the images, in order of use, in an array and add an index counter for it. You could use my Animation Support Class to either look over or make use of the class itself.
XApple15 XApple15

2021/2/4

#
Thank you! But i did not understand what should I do, I tried a While in my Actor in which i used setImage( " ... ") to display every frame, but did not work
XApple15 XApple15

2021/2/4

#
I have found something , and it`s your code :
import greenfoot.*; 
 
public class Lyn extends Actor
{
    GreenfootImage[] images = new GreenfootImage[26];
    int imageNumber;
 
    public Lyn()
    {
        for( int i=0; i<images.length; i++ ) images[i] = new GreenfootImage( "" + i + ".png" );
        setImage( images[imageNumber] );
    }
 
    public void act()
    {
        animation();
    }
 
    public void animation()
    {
        imageNumber = ( imageNumber + 1 ) % images.length;
        setImage( images[imageNumber] );
    }
}
How could i transform it to show images in order then in reverse?
danpost danpost

2021/2/4

#
XApple15 wrote...
How could i transform it to show images in order then in reverse?
Add a int direction field:
int direction = 1;
Then, you can have:
public void animation()
{
    imageNumber = imageNumber+direction;
    if (imageNumber % (imaages.length-1) == 0) direction = -direction;
    setImage( images[imageNumber] );
}
XApple15 XApple15

2021/2/4

#
Your code works , but it`s not optimised. When i run the code, images are getting changed, but the players are moving veeeryyy slow. ( i`m using this code on an object , not on the player ) . Any idea to solve this?
danpost danpost

2021/2/4

#
XApple15 wrote...
Your code works , but it`s not optimised. When i run the code, images are getting changed, but the players are moving veeeryyy slow. ( i`m using this code on an object , not on the player ) . Any idea to solve this?
This code should have little to no effect on player speed. Show player's class code.
XApple15 XApple15

2021/2/5

#
This is Player actor class
import greenfoot.*;  

public class Player1 extends Players
{
    public void act() 
    {
        desc();
        if(Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY()-3);
            if(hitObject())
            {
                setLocation(getX(), getY()+3);
            }
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getX()-3, getY());
            if(hitObject())
            {
            setLocation(getX()+3, getY());
            }
        }
        if(Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + 3);
            if(hitObject())
            {
                setLocation(getX(), getY()-3);
            }
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + 3, getY());
            if(hitObject())
            {
            setLocation(getX() - 3, getY());
            }
        }
    } 
    private void desc()
    {
        GreenfootImage Player1= new GreenfootImage("Players/ppl3.png");
        setImage(Player1);
        changeSize( 1.5 , 1.5 );
        ImageSet();
        
    }

}
danpost danpost

2021/2/5

#
Line 43 is setting a new, but the same, image to the actor every frame using file operations. That is def not optimal.
XApple15 XApple15

2021/2/5

#
danpost wrote...
Line 43 is setting a new, but the same, image to the actor every frame using file operations. That is def not optimal.
Line 43 on Player class? There’s only “ } “ on line 43
danpost danpost

2021/2/5

#
XApple15 wrote...
Line 43 on Player class? There’s only “ } “ on line 43
Are you sure? Check again.
You need to login to post a reply.