the third option! animate in different ways only during the time it intersects the objects.
// 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; }
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; } }
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); } } }