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

2021/1/14

Showing text above a moving actor

1
2
3
4
Roshan123 Roshan123

2021/1/19

#
danpost wrote...
Move line 26 to before line 39.
Still now its not working properly (╯︵╰,)
danpost danpost

2021/1/19

#
Roshan123 wrote...
Still now its not working properly (╯︵╰,)
??? What is happening? What should? Show revised class codes.
Roshan123 Roshan123

2021/1/19

#
Roshan123 wrote...
But why it still doesn't remove the text(I carefully noticed that it works but not properly) To know what's going on the scenario, click Michael Kolling picture in left hand side and then click on solo
If u will click mik picture and then if u will click solo, the text will not be removed from the world
Roshan123 Roshan123

2021/1/19

#
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)
    {
     
     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;
     }
w.showText("mik"+time, getX(), getY()-45);
    }
    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();
    }   
    
    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();
getWorld().showText("mik", getX(), getY()-45);
       if(health ==0 || crate.size() !=0  && world1.solo==1 || stone.size() !=0 || b.size() !=0 || tank.size() !=0 || enemyTank.size() !=0) 
        {
getWorld().showText(null, getX(), getY()-45);
            getWorld().removeObject(this);
        }
    }
}
Roshan123 Roshan123

2021/1/19

#
I don't know where to use showText(null,4773,88);(1st line or last line or in act method or in addedToWorld) Its only the reason why i m having problems I m only depending upon u
Roshan123 Roshan123

2021/1/19

#
Roshan123 wrote...
I don't know where to use showText(null,4773,88);(1st line or last line or in act method or in addedToWorld) Its only the reason why i m having problems I m only depending upon u
I told this becaz i think that u might be angry that i m asking you again and again
danpost danpost

2021/1/19

#
Roshan123 wrote...
I don't know where to use showText(null,4773,88);(1st line or last line or in act method or in addedToWorld)
The null version should only be used at beginning of act and upon removal of tank from world. Is there any other place in your codes (any class) where you would be removing the tank (like Projectile1 class, maybe)?
Roshan123 Roshan123

2021/1/19

#
danpost wrote...
Roshan123 wrote...
I don't know where to use showText(null,4773,88);(1st line or last line or in act method or in addedToWorld)
The null version should only be used at beginning of act and upon removal of tank from world. Is there any other place in your codes (any class) where you would be removing the tank (like Projectile1 class, maybe)?
Ohhh.... Now i remember. I used it in world and i removed actor class if anyone clicks solo or duo
Roshan123 Roshan123

2021/1/19

#
Will it work???
 if(world1.getObjects(EnemyTank.class).isEmpty() || health ==0 || crate.size() !=0  && world1.solo==1 || stone.size() !=0 || b.size() !=0 || tank.size() !=0 || enemyTank.size() !=0) 
        {
getWorld().showText(null, getX(), getY()-45);
            getWorld().removeObject(this);
        }
danpost danpost

2021/1/19

#
Roshan123 wrote...
Will it work??? << Code Omitted >>
Where?
Roshan123 Roshan123

2021/1/19

#
danpost wrote...
Where?
I added a statement in world which checks wether its solo or not and then removes actor class(EnemyTank class is subclass of it) accordingly So how will i remove the text frm EnemyTank class if someone clicks mik picture and then solo
danpost danpost

2021/1/19

#
Roshan123 wrote...
I added a statement in world which checks wether its solo or not and then removes actor class(EnemyTank class is subclass of it) accordingly So how will i remove the text frm EnemyTank class if someone clicks mik picture and then solo
Use the following to remove actor class:
for (Object obj : getObjects(Actor.class))
{
    Actor actor = (Actor) obj;
    if (actor instanceof EnemyTank))
    {
        showText(null, actor.getX(), actor.getY()-45);
        removeObject(actor);
    }
}
Roshan123 Roshan123

2021/1/20

#
I m not able to get api for instanceof So plz tell me what is instanceof and also tell me...what do u search for api of specified keyword
danpost danpost

2021/1/20

#
Roshan123 wrote...
I m not able to get api for instanceof So plz tell me what is instanceof and also tell me...what do u search for api of specified keyword
There are no APIs for keywords. Keywords are defined scattered throughout the Java tutorials. The instanceof keyword is defined here (see bottom).
Roshan123 Roshan123

2021/1/20

#
Still not working
There are more replies on the next page.
1
2
3
4