Roshan123 wrote...
<< Code Omitted >>
Do u mean something like this???danpost wrote...
at end of constructor and at end of act method//Constructor protected void addedToWorld(World world) { world.showText(name, getX(), getY()-45); } public void act { getWorld().showText(null, getX(), getY()-45); //Rest of the code getWorld().showText(name, getX(), getY()-45); }
import greenfoot.*; import java.util.List; public class EnemyTank extends Actor { private int moveTimer = 120; private int condition; private boolean canMove = true; private int speed = 0; int time=0; short s[]={0,1,2}; private Integer lastDraw; public static short health=150; Sheild shield = new Sheild(); short cratetimr; static final int m=100; short r[]={680,120}; short r2[]={120,480}; public EnemyTank(boolean canmove, int speedInt) { getImage().scale(54, 54); canMove = canmove; speed = speedInt; } protected void addedToWorld(World w) { w.showText("mik"+time, getX(), getY()-45); int rand=Greenfoot.getRandomNumber(r.length); int rand2=Greenfoot.getRandomNumber(r2.length); int i=0; while(getOneIntersectingObject(null)!=null && i++<m) { setLocation(r[rand], r2[rand2]); } if(i>=m) { w.removeObject(this); return; } } public void act() { getWorld().showText(null, getX(), getY()-45); getImage().scale(55, 55); MyWorld world1 = (MyWorld) getWorld(); if(world1.counter.sec==2158 && health<111 || time==0 && health<101) health=150; if(world1.duo==1 && getWorld()!=null && getObjectsInRange(348,Crate.class).size()>0 && time<120) { Crate crate = getWorld().getObjects(Crate.class).get(0); turnTowards(crate.getX(), crate.getY()-40); move(2); }else time=121; if(time>120){ if(canMove) { moveTimer++; if(moveTimer >= 120) { condition = (Greenfoot.getRandomNumber(4)); moveTimer = 0; } checkTouching(); bumpTank(); }} if(world1.duo==1) { if(isTouching(Crate.class)) { int rand=Greenfoot.getRandomNumber(s.length-(lastDraw!=null ? 1:0)); if(lastDraw!=null && rand >= lastDraw)rand++; lastDraw=new Integer(rand); if(rand==0) getWorld().addObject(shield,getX(),getY() ); if(rand==1)getWorld().addObject(new HealthT(),400,300 ); if(rand==2)getWorld().addObject(new Score_inc_t(world1.counter),400,300); } } die(); getWorld().showText("mik", getX(), getY()-45); } public void checkTouching() { Stone block = (Stone) getOneIntersectingObject(Stone.class); int r = getRotation(); if(block == null) { moveAround(); } else if(block != null) { if(r == 0) { setLocation(getX() - speed, getY()); if(getY() < 300) { condition = 3; } else if(getY() >= 300) { condition = 2; } } else if(r == 90) { setLocation(getX(), getY() - speed); if(getX() < 500) { condition = 1; } else if(getX() >= 300) { condition = 0; } } else if(r == 180) { setLocation(getX() + speed, getY()); if(getY() < 300) { condition = 3; } else if(getY() >= 300) { condition = 2; } } else if(r == 270) { setLocation(getX(), getY() + speed); if(getX() < 500) { condition = 1; } else if(getX() >= 300) { condition = 0; } } } else if(block == null) { int x = getX(); int y = getY(); if(r == 0) { if(y < 300) { setLocation(getX() - speed, getY()); condition = 3; } else if(y >= 300) { setLocation(getX() - speed, getY()); condition = 2; } } else if(r == 90) { if(x < 500) { setLocation(getX(), getY() - speed); condition = 1; } else if(x >= 500) { setLocation(getX(), getY() - speed); condition = 0; } } else if(r == 180) { if(y < 300) { setLocation(getX() + speed, getY()); condition = 3; } else if(y >= 300) { setLocation(getX() + speed, getY()); condition = 2; } } else if(r == 270) { if(x < 500) { setLocation(getX(), getY() + speed); condition = 1; } else if(x >= 500) { setLocation(getX() - 2, getY() + speed); condition = 0; } } } } public void moveAround() { if(condition == 0 ) { setLocation(getX() - speed, getY()); setRotation(180); } else if(condition == 1) { setLocation(getX() + speed, getY()); setRotation(0); } else if(condition == 2) { setLocation(getX(), getY() - speed); setRotation(270); } else if(condition == 3) { setLocation(getX(), getY() + speed); setRotation(90); } } public void bumpTank() { int r = getRotation(); List<EnemyTank1> enemyTank1 = getNeighbours(69,true,EnemyTank1.class); List<Stone> stone = getNeighbours(44,true,Stone.class); MyWorld world1 = (MyWorld) getWorld(); List<Crate> crate = getNeighbours(63,true,Crate.class); if(isTouching(B.class) || isTouching(Tank.class) || enemyTank1.size() !=0 || stone.size() !=0 || crate.size() !=0 && world1.solo==1 || isTouching(Counter.class) || getX()<=25 || getX()>=getWorld().getWidth()-25 || getY()<=25 || getY()>=getWorld().getHeight()-25) { if(r == 0) { condition = 0; } else if(r == 90) { condition = 2; } else if(r == 180) { condition = 1; } else if(r == 270) { condition = 3; } } } public void die() { if(isTouching(Projectile1.class) && getWorld()!=null && getWorld().getObjects(Counter.class).size()>0) { Counter counter = getWorld().getObjects(Counter.class).get(0); counter.bscore++; } Actor Projectile1=getOneIntersectingObject(Projectile1.class); if(Projectile1 !=null) { health-=50; getWorld().removeObject(Projectile1); } List<Stone> stone = getNeighbours(40,true,Stone.class); List<B> b = getNeighbours(35,true,B.class); List<Tank> tank = getNeighbours(35,true,Tank.class); List<EnemyTank> enemyTank = getNeighbours(57,true,EnemyTank.class); List<Crate> crate = getNeighbours(52,true,Crate.class); MyWorld world1 = (MyWorld) getWorld(); if(health ==0 || crate.size() !=0 && world1.solo==1 || stone.size() !=0 || b.size() !=0 || tank.size() !=0 || enemyTank.size() !=0) { getWorld().removeObject(this); } } }