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

2020/5/13

Left & Right facing images

1
2
genju genju

2020/5/13

#
Im trying to make a soldier unit that has animation, which sort of works but when running the animation, for every other act, the actor goes invisible and also it wont change direction and flip even though i have code for it to do so Superclass
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soldier here.
 * 
 * @author (Prayan Jegathees) 
 * @version (0.1)
 */
public abstract class Soldier extends Actor
{
    /**
     * Private variables used for all soldier subclasses
     *      stats needed to determine the overall stats 
     *      of the character
     *      
     * LIFE is a boolean meant to change & detect the actors "death" 
     * TEAM is a string meant to choose which alliance the actor belongs to 
     */
    protected int health;
    protected int speed;
    private int atkPower;
    private int atkRate;
    
    private boolean life;
    protected String team;
          
    protected boolean withEnemy = false;
    
    protected double vX;
    protected double vY;
    protected int direction;
    
    protected SimpleTimer timer = new SimpleTimer();
    /**
     * Constructor for all Soldiers
     */
    public Soldier(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        health = hp;
        speed = spd;
        atkPower = aPwr;
        atkRate = aRt;
        alliance = team;
        life = true;
        
        if (alliance == "Red")
        {
            direction = -1;
        }
        else if (alliance == "Blue")
        {
            direction = 1;
        }
    }
    
    /**
     * Attack - To deal damage to an enemy soldier unit
     */
    public abstract void attack();
        
    /**
     * takeDamage - When soldier subunits are hit by projectiles and lose health
     *              (amount taken varies based on the type of projectile)
     */
    public void takeDamage()
    {
        /*
        if (isTouching(PROJECTILE_ARROW.class))
        {
            health -= 2;
        }
        if (isTouching(PROJECTILE_FIREBL.class))
        {
            health -= 5;
        }
        */
        if (health == 0)
        {
            life = false;
        }
    }
    
    public void die()
    {
        if (!life)
        {
            move(0);
            getImage().setTransparency(50);
            setRotation(90);
        }
    }
    
    public void remove()
    {
        getWorld().removeObject(this);
    }
}
Subclass
* Write a description of class Knight here.
 * 
 * @author (Prayan Jegathees) 
 * @version (0.1)
 */
public class Minotaur extends Soldier
{
    private int imgNum = 1;
    private boolean animationComplete = false;
    GreenfootImage [] walkImages = new GreenfootImage[5];
    GreenfootImage [] atkImages = new GreenfootImage[4];
    
    public Minotaur(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        super(hp, spd, aPwr, aRt, alliance);
        setImg();
    }
    
    /**
     * Act - do whatever the Squire wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        update();
    }    
    
    public void attack()
    {
        
    }
    
    public void setImg()
    {
        if (!withEnemy) 
        {
            for(int i = 1; i < 5; i++)
            {
                walkImages[i] = new GreenfootImage("minoWalk" + i + ".png");
            }
            setImage(walkImages[imgNum]);
            
            if(team == "Red")
            {
                turn(180);
                getImage().mirrorVertically();
            }
        }
        else
        {
            for(int i = 1; i < 4; i++)
            {
                atkImages[i] = new GreenfootImage("minoAtk" + i + ".png");
            }            
            setImage(atkImages[imgNum]);
            
            if(team == "Red")
            {
                turn(180);
                getImage().mirrorVertically();
            }
        }
    }  
    
    public void animate()
    {
        if (!withEnemy) 
        {
            imgNum = (imgNum + 1) % walkImages.length;
            setImage( walkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
        else
        {
            imgNum = (imgNum + 1) % atkImages.length;
            getImage().scale(500,500);
            setImage( atkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
    }
    
    
    public void checkForEnemies()
    {
        if(isTouching(Soldier.class))
        {
            withEnemy = true;
        }
    }
    
    public void checkGround()
    {
        if(animationComplete)
        {
            remove();
        }
    }
    
    public void update()
    {
        if(direction >= 0)
        {
            setLocation(getX() + speed, getY());
            animate();
        }
        else
        {
            setLocation(getX() - speed, getY());
            animate();
        }        
    }    
}
danpost danpost

2020/5/13

#
You are comparing a lot of string variables to literal strings using '==', which compares the objects,, not their character sequences. Use the following format:
if ("literal".equals(var))
to compare the character sequences.
genju genju

2020/5/13

#
danpost wrote...
You are comparing a lot of string variables to literal strings using '==', which compares the objects,, not their character sequences. Use the following format:
if ("literal".equals(var))
to compare the character sequences.
are you referring to the alliance and team if statement lines? and for the variable it would be the team name/colour so wouldn't that be a string anyways..im confused
genju genju

2020/5/13

#
also i was able to mirror the image files and add it into the code but when i try animations it shows the very first image when i add it into the world and then it dissapears completely when i run it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Knight here.
 * 
 * @author (Prayan Jegathees) 
 * @version (0.1)
 */
public class Minotaur extends Soldier
{
    private int imgNum = 1;
    private boolean animationComplete = false;
    GreenfootImage [] walkImages = new GreenfootImage[5];
    GreenfootImage [] atkImages = new GreenfootImage[4];
    GreenfootImage [] walkImagesLeft = new GreenfootImage[5];
    GreenfootImage [] atkImagesLeft = new GreenfootImage[4];
    
    public Minotaur(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        super(hp, spd, aPwr, aRt, alliance);
        setImg();
    }
    
    /**
     * Act - do whatever the Squire wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        update();
    }    
    
    public void attack()
    {
        
    }
    
    public void setImg()
    {
        if (!withEnemy && team == "Blue") 
        {
            for(int i = 1; i < 5; i++)
            {
                walkImages[i] = new GreenfootImage("minoWalk" + i + ".png");
            }
            setImage(walkImages[imgNum]); 
        }
        else
        {
            for(int i = 1; i < 4; i++)
            {
                atkImages[i] = new GreenfootImage("minoAtk" + i + ".png");
            }            
            setImage(atkImages[imgNum]);
        }
        
        if (!withEnemy && team == "Red") 
        {
            for(int i = 1; i < 5; i++)
            {
                walkImagesLeft[i] = new GreenfootImage("minoWalk" + i + "-Left.png");
            }
            setImage(walkImagesLeft[imgNum]); 
        }
        else
        {
            for(int i = 1; i < 4; i++)
            {
                atkImagesLeft[i] = new GreenfootImage("minoAtk" + i + "-Left.png");
            }            
            setImage(atkImagesLeft[imgNum]);
        }
    }  
    
    public void animate()
    {
        if (!withEnemy) 
        {
            imgNum = (imgNum + 1) % walkImages.length;
            setImage( walkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
        else
        {
            imgNum = (imgNum + 1) % atkImages.length;
            getImage().scale(500,500);
            setImage( atkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
    }
    
    
    public void checkForEnemies()
    {
        if(isTouching(Soldier.class))
        {
            withEnemy = true;
        }
    }
    
    public void checkGround()
    {
        if(animationComplete)
        {
            remove();
        }
    }
    
    public void update()
    {
        if(direction >= 0)
        {
            setLocation(getX() + speed, getY());
            animate();
        }
        else
        {
            setLocation(getX() - speed, getY());
            animate();
        }        
    }    
}
danpost danpost

2020/5/13

#
genju wrote...
are you referring to the alliance and team if statement lines? and for the variable it would be the team name/colour so wouldn't that be a string anyways..im confused
Yes, that is what I am referring to; and, yes, they will be string objects, but you do not want to compare the objects -- you want to compare their character sequences. That is why you need to use equals -- not " == ". Also, when using equals, it is always best to put the literal (the one between quote marks) first. That way you can avoid any NullPointerException throws. So:
if ("Red".equals(team))
or
if ("Blue".equals(alliance))
genju genju

2020/5/13

#
ok i changed my lines to use what you said and still when i run the code, the actor disappears and doesnt actually animate through all the images set. is there a problem with my animation or set image code?
danpost danpost

2020/5/13

#
genju wrote...
ok i changed my lines to use what you said and still when i run the code, the actor disappears and doesnt actually animate through all the images set. is there a problem with my animation or set image code?
I do not know. I will look closer now that you fixed that much.
danpost danpost

2020/5/13

#
In Minotaur class lines 42 and 50 should start with:
for (int i=0; ...
with i starting at zero.
genju genju

2020/5/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Knight here.
 * 
 * @author (Prayan Jegathees) 
 * @version (0.1)
 */
public class Minotaur extends Soldier
{
    private int imgNum = 1;
    private boolean animationComplete = false;
    GreenfootImage [] walkImages = new GreenfootImage[5];
    GreenfootImage [] atkImages = new GreenfootImage[4];
    GreenfootImage [] walkImagesLeft = new GreenfootImage[5];
    GreenfootImage [] atkImagesLeft = new GreenfootImage[4];
    
    public Minotaur(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        super(hp, spd, aPwr, aRt, alliance);
        setImg();
    }
    
    /**
     * Act - do whatever the Squire wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        update();
    }    
    
    public void attack()
    {
        
    }
    
    public void setImg()
    {
        if (!withEnemy &&  ("Blue".equals(team))) 
        {
            for(int i = 0; i < 4; i++)
            {
                walkImages[i] = new GreenfootImage("minoWalk" + i + ".png");
            }
            setImage(walkImages[imgNum]); 
        }
        else
        {
            for(int i = 0; i < 3; i++)
            {
                atkImages[i] = new GreenfootImage("minoAtk" + i + ".png");
            }            
            setImage(atkImages[imgNum]);
        }
        
        if (!withEnemy && ("Red".equals(team))) 
        {
            for(int i = 0; i < 4; i++)
            {
                walkImagesLeft[i] = new GreenfootImage("minoWalk" + i + "-Left.png");
            }
            setImage(walkImagesLeft[imgNum]); 
        }
        else
        {
            for(int i = 0; i < 3; i++)
            {
                atkImagesLeft[i] = new GreenfootImage("minoAtk" + i + "-Left.png");
            }            
            setImage(atkImagesLeft[imgNum]);
        }
    }  
    
    public void animate()
    {
        if (!withEnemy) 
        {
            imgNum = (imgNum + 1) % walkImages.length;
            setImage( walkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
        else
        {
            imgNum = (imgNum + 1) % atkImages.length;
            getImage().scale(500,500);
            setImage( atkImages[imgNum]);
            if(imgNum >= 5)
            {
                animationComplete = true;
            }
        }
    }
    
    
    public void checkForEnemies()
    {
        if(isTouching(Soldier.class))
        {
            withEnemy = true;
        }
    }
    
    public void checkGround()
    {
        if(animationComplete)
        {
            remove();
        }
    }
    
    public void update()
    {
        if(direction >= 0)
        {
            setLocation(getX() + speed, getY());
            animate();
        }
        else
        {
            setLocation(getX() - speed, getY());
            animate();
        }        
    }    
}
i corrected it and still the same problem persists, also when i added the:
Minotaur mino = new Minotaur(10, 1, 10, 10, "Blue");
in the constructor and
addObject(mino, 200, 200);
it still only creates left facing images regardless of the team i set it to.. it should always face left for red and right for blue but the image starts on an attack image and faces left even though theres code specifically so it faces the right direction when spawned in and whether its in contact with an enemy or not
genju genju

2020/5/14

#
if it helps i can post my superclass that the minotaur is a subclass of if needed
danpost danpost

2020/5/14

#
Line 43 in your Soldier class is wrong. You should at least Inspect your actor before coming here for help. When you notice that team is still set to null, that should point you toward the problem line.
genju genju

2020/5/14

#
i tried changing it to .equals instead of = along with the if statements in soldier but it actually created a null pointer exception in the terminal once i tried running the code again. I wrote the code i know how and looked through multiple times.
public Soldier(int hp, int spd, int aPwr, int aRt, String alliance)
    {
        health = hp;
        speed = spd;
        atkPower = aPwr;
        atkRate = aRt;
        alliance.equals(team);
        life = true;
        
        if (alliance.equals("Red"))
        {
            direction = -1;
        }
        else if (alliance.equals("Blue"))
        {
            direction = 1;
        }
    }
genju genju

2020/5/14

#
i also feel that theres an issue with my animate method because its only animating right facing images but i dont know how i'd differentiate the left facing images from the right in this case
genju genju

2020/5/14

#
so i thought i found a solution to the issue i saw in the animate method but once i changed it there was another null pointer exception that greenfoot pointed out
public void animate()
    {
        if (team.equals("Blue"))
        {
            if (!withEnemy) 
            {
                imgNum = (imgNum + 1) % walkImages.length;
                setImage(walkImages[imgNum]);
                if(imgNum >= 5)
                {
                    animationComplete = true;
                }
            }
            else
            {
                imgNum = (imgNum + 1) % atkImages.length;
                getImage().scale(500,500);
                setImage( atkImages[imgNum]);
                if(imgNum >= 5)
                {
                    animationComplete = true;
                }
            }
        }
        
        if (team.equals("Red"))
        {
            if (!withEnemy) 
            {
                imgNum = (imgNum + 1) % walkImagesLeft.length;
                setImage(walkImagesLeft[imgNum]);
                if(imgNum >= 5)
                {
                    animationComplete = true;
                }
            }
            else
            {
                imgNum = (imgNum + 1) % atkImagesLeft.length;
                getImage().scale(500,500);
                setImage( atkImagesLeft[imgNum]);
                if(imgNum >= 5)
                {
                    animationComplete = true;
                }
            }
        }
    }
genju genju

2020/5/14

#
oh wait line 43 was reversed as well, it should have been team equals alliiance thats what you were talking about that makes sense, sorry for the spam Edit: so now its animating again, but the the very first problem arises again where it goes invisible every other act. other than that i believe it works now so thanks!
There are more replies on the next page.
1
2