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

2013/5/29

Simplify my animation code

Solringolt Solringolt

2013/5/29

#
I have a code for my animation that works but it is really heavy and I think there are some useless part in it... I have to goals with my animation: one constant animation for each enemy type and a little touchAnimation when a bullet hit's them. http://www.greenfoot.org/scenarios/7845 Here you can see my project and how they animates. The code I use now s this one:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actions
{
    private String imageUsed;
    private int typeOfEnemy;
    private int lifeLeft;
    private int pointWin;
    
    GreenfootImage[] framesEnemy1 = new GreenfootImage[8];  
    GreenfootImage[][] allFramesEnemy1 = new GreenfootImage[2][8];
    
    GreenfootImage[] framesEnemy2 = new GreenfootImage[8];  
    GreenfootImage[][] allFramesEnemy2 = new GreenfootImage[2][8];
    
    final int CONSTANT = 4;  
    int imgFrame1 = 0;  
    int imgFrame2 = 0;  
    int enemy1Contact = 1;  
    int enemy1Num;
    int enemy2Contact = 1;  
    int enemy2Num;
    private int reloadTime = 1;
    private int fixTimerValue = 50;
    public Enemy(String enemyImage,int enemyType, int life, int pointEarned)
    {
        imageUsed = enemyImage;
        typeOfEnemy = enemyType;
        lifeLeft = life;
        pointWin = pointEarned;
        setImage(imageUsed);

        for(int j=0; j<2; j++) for (int k=0;k<framesEnemy1.length;k++) allFramesEnemy1[j][k]=new GreenfootImage("enemy1"+j+"touch"+(k+1)+".png");  
        framesEnemy1 = allFramesEnemy1[enemy1Contact];
        
        for(int l=0; l<2; l++) for (int m=0;m<framesEnemy2.length;m++) allFramesEnemy2[l][m]=new GreenfootImage("enemy2"+l+"touch"+(m+1)+".png");  
        framesEnemy1 = allFramesEnemy1[enemy2Contact]; 
    }
    public void act() 
    {
       checkType();
       if (getWorld() != null) 
       {
           if (typeOfEnemy == 1) animateEnemy1();
           if (typeOfEnemy == 2) animateEnemy2();
       }
    }
    public void checkType()
    {
        if (typeOfEnemy == 1)
        {
            randomMove();
            move(-3);
            backToStart();
            colisionCheck();
        }
        if (typeOfEnemy == 2)
        {
            randomMove();
            //colisionCheck();
            if (Greenfoot.getRandomNumber(5000)  <= 10)
            {
                 canShootNormal("enemyShot1.png");
            }
            
        }
        if (typeOfEnemy == 3)
        {
            chargeHero();
            if (!chargingHero)verticalMove();
            backToStart();
            colisionCheck();
        }
        if (typeOfEnemy == 4)
        {
            
        }
        if (typeOfEnemy == 5)
        {
            
        }
        if (typeOfEnemy == 6)
        {
            
        }
    }
    public void hit(int damage) 
    {
        lifeLeft = lifeLeft - damage;
        
        hitAnimationEnemy();
        
        
        if(lifeLeft <= 0)
        {
           ((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
           dropBonusRandom();
           getWorld().removeObject(this);
        }
    }
    public void hitAnimationEnemy()
    {
        //pour ennemi 1
        if (typeOfEnemy == 1 && lifeLeft == 7)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 6)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 5)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 4)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 3)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 2)
        {
            setImage("enemy11touch1.png");
        }
        if (typeOfEnemy == 1 && lifeLeft == 1)
        {
            setImage("enemy11touch1.png");
        }
        //pour ennemi 2
        if (typeOfEnemy == 2 && lifeLeft == 8)
        {
            setImage("enemy21touch2.png");
            
        }
        if (typeOfEnemy == 2 && lifeLeft == 7)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 6)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 5)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 4)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 3)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 2)
        {
            setImage("enemy21touch2.png");
        }
        if (typeOfEnemy == 2 && lifeLeft == 1)
        {
            setImage("enemy21touch2.png");
        }
    }
    public void animateEnemy1()  
    {  
            enemy1Num = typeOfEnemy-1;
            if (enemy1Num != enemy1Contact)  
            {  
                enemy1Contact = enemy1Num;  
                framesEnemy1 = allFramesEnemy1[enemy1Contact];  
                imgFrame1 = CONSTANT;  
            } 
            if (imgFrame1 % 4 == 0) setImage(framesEnemy1[imgFrame1/CONSTANT]);  
            imgFrame1++;  
            if (imgFrame1 == CONSTANT*framesEnemy1.length) imgFrame1 = 0;  
    }
    public void animateEnemy2()  
    {  
            enemy2Num = typeOfEnemy-2;
            if (enemy2Num != enemy2Contact)  
            {  
                enemy2Contact = enemy2Num;  
                framesEnemy2 = allFramesEnemy2[enemy2Contact];  
                imgFrame2 = CONSTANT;  
            } 
            if (imgFrame2 % 4 == 0) setImage(framesEnemy2[imgFrame2/CONSTANT]);  
            imgFrame2++;  
            if (imgFrame2 == CONSTANT*framesEnemy2.length) imgFrame2 = 0;  
    }
}
How can I have the same result without so much code?
danpost danpost

2013/5/29

#
You could replace lines 110 through 173 with:
setImage("enemy"+typeOfEnemy+"1touch"+typeOfEnemy+".png");
You need to login to post a reply.