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

2020/10/11

Check collision before adding

1
2
3
4
5
Roshan123 Roshan123

2020/10/17

#
Roshan123 wrote...
Plz write a program to check collision If red team destroys blue team, then blue team will be removed from the world and it will be added again to the world again within 1sec. But i want it to add randomly(i was successful in it...) but after adding it to the world it should not interesting with any actor.class (i don't know how to do it, so plz tell me)
Only read this
Roshan123 Roshan123

2020/10/17

#
DatHeroAndy wrote...
Roshan123 wrote...
Plz write a program to check collision If red team destroys blue team, then blue team will be removed from the world and it will be added again to the world again within 1sec. But i want it to add randomly(i was successful in it...) but after adding it to the world it should not interesting with any actor.class (i don't know how to do it, so plz tell me)
So, if I am understanding this correctly, you don't want to use getOneIntersectingObject(), right?
Yes, but only when red team destroys blue tank(then it will be null) and also want to add it randomly Note that it should not collide with any object If it collides then it will be added again
danpost danpost

2020/10/17

#
Roshan123 wrote...
If it collides then it will be added again
RcCookie's initial code post has the newly added actor reposition itself until not touching another actor, removing the need to add it again.
Roshan123 Roshan123

2020/10/17

#
RcCookie wrote...
You should definitely use the second version where it is limited to 100 trys. That version should not lag. Btw here is the complete second version:

static final int MAX_ITERATIONS= 100;

@Override
protected void addedToWorld(World w) {
    int i = 0;
    while(getOneIntersectingObject(null) != null && i++ < MAX_ITERATIONS) {
        //set a new random location. You may wanna use your own code here
        setLocation((int)(Math.random() * w.getWidth()), (int)(Math.random() * w.getHeight()));
    }
    if(i == MAX_ITERATIONS) {
        w.removeObject(this);
        return;
    }
}
I mean its working but not properly I will post the code after some time,so plz wait for the time being I have a problem in line 6 If its touching any class then it gets removed I want it only to be removed when its adding and not for the whole time It might be my error also, becaz i added some of my code So plz wait.....
danpost danpost

2020/10/17

#
You may want to try ">=" on line 10.
Roshan123 Roshan123

2020/10/17

#
Plz wait
Roshan123 Roshan123

2020/10/17

#
import greenfoot.*;
public class B extends Actor
{
    int t,count,speed=5;
    boolean upKeyDown;
    int movt;
    Counter counter;
    int health=150;
    int s[]={0,1,2};
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    static final int m=100;
    public B(Counter counter)
    {
      this.counter=counter;
      getImage().scale(60,60);
      setRotation(180);
      background = getImage();
    }
    public void act() 
    {
        add();
        magnet();
        sheild();
        speed();
        stone();
        die();
        moveANDturn();

    }
    protected void add()
    {
     World w=getWorld();
     int i=0;
     if(getWorld()!=null && getWorld().getObjects(B.class).size()>0)
     {
     while(getOneIntersectingObject(null)!=null && i++<m)
     {
         setLocation((int)(Math.random()*w.getWidth()), (int)(Math.random()*w.getHeight()));
     
     }
     if(i>=m)
     {
         w.removeObject(this);
         return;
     }
    
     getWorld().showText(""+i+""+m, 400, 300);
    }
    }
    public void magnet()
    {
       
     if(Greenfoot.isKeyDown("w") && isTouching(Magn_feild.class))
     {
         speed=+2;
     }else speed=5;
    }
    public void sheild()
    {
        if(isTouching(Crate.class))
        {
            int rand=Greenfoot.getRandomNumber(s.length);
            if(rand==0)
            getWorld().addObject(new BSheild(),getX(),getY() );
            if(rand==1)getWorld().addObject(new HealthB(),410,26 );
            if(rand==2)getWorld().addObject(new Score_inc_b(counter),getX(),getY());
        }
    }
    public void speed()
    {
        Actor Speed=getOneIntersectingObject(Speed.class);
        if(Greenfoot.isKeyDown("w") && Speed !=null)
        {
          speed=+16;
        }
        
    }
    public void stone()
    {
      Actor Stone=getOneIntersectingObject(Stone.class);
      Actor EnemyTank=getOneIntersectingObject(EnemyTank.class);
      Actor EnemyTank1=getOneIntersectingObject(EnemyTank1.class);
      Actor Sheild=getOneIntersectingObject(Sheild.class);
      Actor Tank=getOneIntersectingObject(Tank.class);
      if(Greenfoot.isKeyDown("w") && Stone       !=null
      || Greenfoot.isKeyDown("w") && EnemyTank   !=null 
      || Greenfoot.isKeyDown("w") && EnemyTank1  !=null 
      || Greenfoot.isKeyDown("w") && Sheild      !=null  
      || Greenfoot.isKeyDown("w") && Tank        !=null) 
      {
        speed=0;
      }
      if(upKeyDown != Greenfoot.isKeyDown("w") && Stone      !=null 
      || upKeyDown != Greenfoot.isKeyDown("w") && EnemyTank1 !=null
      || upKeyDown != Greenfoot.isKeyDown("w") && EnemyTank  !=null 
      || upKeyDown != Greenfoot.isKeyDown("w") && Sheild     !=null  
      || upKeyDown != Greenfoot.isKeyDown("w") && Tank       !=null) 
      {
        move(-15);
      }
        
    }   
    public void moveANDturn()
    {    
        if(Greenfoot.isKeyDown("w"))
        {
          movt++;
        }else {turn(t);movt=0;}
        if(movt>7)
        {
         move(speed);   
        }
        if(upKeyDown != Greenfoot.isKeyDown("w"))
        {
            upKeyDown= !upKeyDown;
            if(upKeyDown)
            {
            count+=1;
            Projectile1 projectile1=new Projectile1();
            getWorld().addObject(projectile1, getX(),getY());
            projectile1.setRotation(getRotation());
            }
        }
        if(count % 2==0)
        {
            t=4;
        }else t=-4;
    } 
    public void die()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(""+health,20,Color.WHITE, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth()-31), 
                        (image.getHeight()-text.getHeight()-20));
        setImage(image);
        if(isTouching(Projectile.class))
        {
         counter.tscore++;
        }
        Actor Projectile=getOneIntersectingObject(Projectile.class);
        if(Projectile !=null)
        {
         health-=50;
         getWorld().removeObject(Projectile);
        }
        if(health ==0 )
        {
            removeTouching(Tank.class);
            getWorld().removeObject(this);
        }
    }
} 
Roshan123 Roshan123

2020/10/17

#
read from line 12-50
Roshan123 Roshan123

2020/10/17

#
r u their or not??? (reply anythnig)
danpost danpost

2020/10/17

#
Roshan123 wrote...
r u their or not??? (reply anythnig)
anything
Roshan123 Roshan123

2020/10/17

#
Plz fix it Its removing when any object intersects I want this, only when it gets added to the world (to check...if it intersects while adding then it should be removed and not for the whole time)
danpost danpost

2020/10/17

#
Roshan123 wrote...
read from line 12-50
Remove lines 22, 33, 35, 36, 45 and 48. Then change line 20 to precisely the following::
protected void addedToWorld(World w)
In line 48, change "getWorld()" to "w".
Roshan123 Roshan123

2020/10/18

#
But its not doing anything Its not adding when red is not in the world If blue destroys red, then red is removed from the world And when its null it should be added randomly to the world and also it should check collision If intersecting then it should be added again randomly to the world
Roshan123 Roshan123

2020/10/18

#
In world B mainB=new B(counter); //Constructor mainB=new mainB(counter); addObject(mainB,2,1); //Act //I removed it and added RcCookies code(not in world, i mean in class as RcCookies told me):- if(timer>0 && --timer ==0) addObject(new B(counter),2,1); if(timer==0 && getObjects(B.class).isEmpty())timer =100; // For adding again when its null //It doesn't check collision and doesn't add randomly
danpost danpost

2020/10/18

#
Copy/paste world act method (in entirety). Use code tags.
There are more replies on the next page.
1
2
3
4
5