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
danpost danpost

2021/1/15

#
Roshan123 wrote...
<< Code Omitted >> Do u mean something like this???
Yes.
danpost wrote...
at end of constructor and at end of act method
Roshan123 Roshan123

2021/1/15

#
Roshan123 wrote...
//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);
}
But text is also shown after the tank is removed I want text to be removed after tank is removed
danpost danpost

2021/1/15

#
Roshan123 wrote...
But text is also shown after the tank is removed I want text to be removed after tank is removed
Remove it immediately before removing tank using same line as at beginning of act.
Roshan123 Roshan123

2021/1/16

#
Sorry, I don't understand???
danpost danpost

2021/1/16

#
Roshan123 wrote...
Sorry, I don't understand???
Show code where actor is being removed and indicate what class it is in.
Roshan123 Roshan123

2021/1/16

#
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);
        }
    }
}
Roshan123 Roshan123

2021/1/16

#
Line 79 Actor is not in the world....(error)
Roshan123 Roshan123

2021/1/16

#
Now its removing text but giving error in line 79 Before that i did a silly mistake, that's why it was not removing the text
danpost danpost

2021/1/16

#
Roshan123 wrote...
Now its removing text but giving error in line 79
danpost wrote...
Remove it (the text) immediately before removing tank using same line as at beginning of act.
Move line 79 to before line 273.
Roshan123 Roshan123

2021/1/18

#
its working But what is protected addedToWorld(World w) RcCookies also recommend it for checking collision before adding but i never asked him about it
danpost danpost

2021/1/18

#
Roshan123 wrote...
what is protected addedToWorld(World w)
It is an Actor class metthod (a method provided by greenfoot) that greenfoot automatically calls when an actor is added into a world. It is, by default, empty of any code. It allows you to override it to give some implementation to it (similar to the act method, also provided in the Actor class, also empty by default, also automatically called by greenfoot, but called every act cycle, and also can be overridden).
Roshan123 Roshan123

2021/1/18

#
Sorry but i m not able to understand Can u pls tell me frm where did u learnt this method for 1st time. Its so complicated for me( or give a example by showing its main purpose of using)
danpost danpost

2021/1/18

#
Roshan123 wrote...
Can u pls tell me frm where did u learnt this method for 1st time.
When first starting out with greenfoot, you should look over what is provided by the World and Actor classes. That is, glance over the API documentation of the greenfoot.World and greenfoot.Actor classes to (1) see what is needed to construct an object of the class; and (2) see what methods are provided that work with objects of the class. To "learn" a class, concentrate first on the methods that return nothing (void), primitive types (int, double, float, boolean, etc.) and String objects. You should already be familiar with the java.lang.String class. Once familiar with those, you can begin looking at methods that return objects other than of String type (like java.util.List). Another useful class to be familiar with is the java.lang.Math class. Of course, you should become familiar with the other classes provided by greenfoot also.
Roshan123 Roshan123

2021/1/18

#
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
danpost danpost

2021/1/18

#
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
Move line 26 to before line 39.
There are more replies on the next page.
1
2
3
4