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

2013/5/3

An animation function ?

1
2
3
Solringolt Solringolt

2013/5/6

#
the third option! animate in different ways only during the time it intersects the objects.
danpost danpost

2013/5/7

#
Then, you will need to set your 'frames' array with the proper images when an intersection is detected. Also, you will not need the 'continuousAnim' field or 'setContinuousAnim' method. Substitute for '!continuousAnim' in the 'if' condition a detection check to determine when the intersecting of the objects ends. Oh, and get rid of the 'GreenfootImage frames' parameter from the 'animate' method declaration statement.
Solringolt Solringolt

2013/5/7

#
And I have to make another function for the basic continuous animation?
danpost danpost

2013/5/7

#
So, now you say that there will always be some animation going: a standard animation, plus various 'hit' animations that will be implemented while it intersects other objects.
Solringolt Solringolt

2013/5/7

#
Yes.. I'm sorry I wasn't very precise maybe... My English is not as good as I would like.
danpost danpost

2013/5/7

#
Then you would need something like this:
// with these instance fields
GreenfootImage[] frames = new GreenfootImage[8];
GreenfootImage[][] allFrames = new GreenfootImage[4][8];
final int CONSTANT = 4;
int imgFrame = 0;
int alienContact = 0;

// in the constructor
for(int j=0; j<4; j++) for (int k=0;k<frames.length;k++) allFrames[j][k]=new GreenfootImage("alien"+(j+1)+"Frame"+(k+1)+".png");
frames = allFrames[alienContact];

// add this method (called from 'act')
public void animate()
{
    Alien alien = (Alien) getOneIntersectingObject(Alien.class);
    int alienNum = 0;
    if (alien != null)
    {
        if (alien instanceof AlienOne) alienNum = 1;
        if (alien instanceof AlienTwo) alienNum = 2;
        if (alien instanceof AlienThree) alienNum = 3;
    }
    if (alienNum != alienContact)
    {
        // take damage here
        alienContact = alienNum;
        frames = allFrames[alienContact];
        imgFrame = CONSTANT;
    }
    if (imgFrame % 4 == 0) setImage(frames[imgFrame/CONSTANT]);
    imgFrame++;
    if (imgFrame == CONSTANT*frames.length) imgFrame = 0;
}
Solringolt Solringolt

2013/5/7

#
I got a java.lang.NullPointerException after some seconds playing (animation seems to work) the enemies flitters and then i got the error. Can you explain me line 9 and 15-29?
danpost danpost

2013/5/7

#
The key words in my last post were 'something like'. Line 9 will fill in the GreenfootImage 2-D array with the images needed for (first index 0) standard animation frames, (index 1) first actor intersecting animation frames, (index 2) second actor intersecting animation frames, etc. The array is declared on line 3. Line 15 gets any alien actor that intersects this enemy, if any, and stores it in a local variable ('alien'). This would be the actual code you would use if all your alien classes are under a super-class called 'Alien'. The idea is just to get a reference to any alien object that intersects this enemy. At this point, we need to determine which type of alien was returned, if any. Lines 16 through 22 assigns a value to a local variable ('alienNum') depending on the type of alien. You may need to accomplish this in some other way (it would mainly depend on how your classes are set up). Next, lines 23 through 29, we do what needs to be done if there has been a change in the intersecting alien object (from none to some alien, from some alien to another, or from some alien to none). If there was a change, then we change the array used for animation to the appropriate images, reset the imgFrame counter and save the last encountered alien number in the instance variable declared on line 6 ('alienContact'). This is what was used to determine if the currently intersecting object type is the same as the previous one. The value of this field (or alien number) is also used as the index when getting the newly active GreenfootImages from the 2-D array of images.
danpost danpost

2013/5/7

#
I guess the filenames I created for the 2-D array do not correctly reflect what the images are of. Better might have been: code]"enemy"+j+"_"+(k+1)+".png" which would make: 'enemy0_1.png' the first standard animation frame 'enemy0_2.png' ' the second standard animation frame through to: 'enemy0_8.png' ' the last standard animation frame then: 'enemy1_1.png' ' the first alienOne type contact animation frame to 'enemy1_8.png' ' the last alienOne type contact animation frame 'enemy2_1.png' ' the first alienTwo type contact animation frame etc.
Solringolt Solringolt

2013/5/7

#
Ok nice I understand way better now. My class Enemy has it standart animation and when a shot hit them then they do a little animation. So in your case it is the enemy who hit's the shot no? I changed my code like this
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[] frames = new GreenfootImage[8];  
    GreenfootImage[][] allFrames = new GreenfootImage[4][8];  
    final int CONSTANT = 4;  
    int imgFrame = 0;  
    int enemyContact = 0;  
    
    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<1; j++) for (int k=0;k<frames.length;k++) allFrames[j][k]=new GreenfootImage("enemy"+(j+1)+"touch"+(k)+".png");
        frames = allFrames[enemyContact];  
    }
    public void act() 
    {
       checkType();
       animate();
    }
    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)
        {
            
        }
        if (typeOfEnemy == 4)
        {
            
        }
        if (typeOfEnemy == 5)
        {
            
        }
        if (typeOfEnemy == 6)
        {
            
        }
    }
    public void hit(int damage) 
    {
        lifeLeft = lifeLeft - damage;
        if (typeOfEnemy == 1)
        {
            
        }
        if(lifeLeft <= 0)
        {
           ((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
           dropBonus();
           getWorld().removeObject(this);
        }
    }
    public void animate()  
    {  
        int enemyNum = 0;
        Enemy enemy = (Enemy) getOneIntersectingObject (Enemy.class);  
          
        if (enemy != null)  
        {  
            if (typeOfEnemy == 1) enemyNum = 1;  
            if (typeOfEnemy == 2) enemyNum = 2;  
            if (typeOfEnemy == 3) enemyNum = 3;  
        } 
        if (enemyNum != enemyContact)  
        {  
            enemyContact = enemyNum;  
            frames = allFrames[enemyContact];  
            imgFrame = CONSTANT;  
        } 
        if (imgFrame % 4 == 0) setImage(frames[imgFrame/CONSTANT]);  
        imgFrame++;  
        if (imgFrame == CONSTANT*frames.length) imgFrame = 0;  
    }
}
But the first part of the animate function I changed don't work. Do I have to change the getOneIntersectingObject() to my shot class?
Solringolt Solringolt

2013/5/7

#
Ha ok I just realized something. I won't be able to have a specific standart animation for each enemy because I take the type of enemy as a parameter. So every enemy types will have the same standart animation. I didn't intended so much difficulties for animation.. XD
danpost danpost

2013/5/7

#
What type of objects are you going to have intersect with the enemy that will cause it to animate differently?
danpost danpost

2013/5/7

#
Try the following for line 91:
int enemyNum = typeOfEnemy;
Solringolt Solringolt

2013/5/7

#
The hero can shoot. If a bullet intersects an enemy then the enemy should make a "damaged" animation. the bullet is added from the hero class. I changed the line, now i get the error at the start
java.lang.NullPointerException at Actions.colisionCheck(Actions.java:70) at Enemy.checkType(Enemy.java:46) at Enemy.act(Enemy.java:36)
that's my action class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Actions here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Actions extends Actor
{
    /**
     * Act - do whatever the Actions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int fixTimerValue = 1 ;
    private int reloadTime = 100;
    public int rotation;
    private String Image;
    
    public void act() 
    {
        
    }
    public void canShootNormal(String shotImage)
    {
        Image = shotImage;
        
        getWorld().addObject(new NormalShot(180,Image),getX(),getY());
        Greenfoot.playSound("tir.wav");
        reloadTime = 100;
       
    }
    public void randomMove()
    {
        
        rotation += Greenfoot.getRandomNumber(30)+356;
        setRotation(rotation);
        move(-3);
       
        setRotation(0);   
    
    }
    public void dropBonus()
    {
         if (Greenfoot.getRandomNumber(100)  <= 2)
         {
                 if (Greenfoot.getRandomNumber(2)  == 1)
                 {
                     getWorld().addObject(new Shield(),getX(),getY());
                 }
                 else
                 {
                     getWorld().addObject(new TripleShot(),getX(),getY());
                 }
         }  
    
    }
    public void backToStart()
    {
        if(getX() <= 5) setLocation(1195,Greenfoot.getRandomNumber(800)+20);
    }
    public void colisionCheck()
    {
        
        PlayUniverse playUniverse = (PlayUniverse) getWorld();
        Hero player = playUniverse.getplayer();
        
        Actor hero = getOneIntersectingObject(Hero.class);
        
        if (hero != null  && player.Shield == false)
        {
            ((LivesCounter)getWorld().getObjects(LivesCounter.class).get(0)).add(-1);
            player.checkBonus("tripleShotOff");
            getWorld().removeObject(this);

        }
        else if (hero != null && player.Shield == true)
        {
            player.checkBonus("shieldOff");
            getWorld().removeObject(this);
        }
    } 
}
The colisionCheck() seems to be the reason of the error.
danpost danpost

2013/5/7

#
I see no reason for a NullPointerException on that line (line 70 of the Actions class), unless you are programmatically calling 'act' on the object in question; or, and this is more likely, you changed the code between getting the error message and posting.
There are more replies on the next page.
1
2
3