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

2016/10/26

Make a Airplane drop a bomb

mmccurdy mmccurdy

2016/10/26

#
In my game there is an Airplane that is suppose to drop a bomb when it gets to a certain point. I have tried to make it drop by using a getY() method but it wont work. Any help is welcome
public class Airplane extends Animal
{

    public void act() 
    {
      move();
      
      if (getY() == 450)
      {
        getWorld().addObject(new bomb(), getX(), getY()); 
      }
      
      else if (atWorldEdge())
      {
       getWorld().removeObject(this); 
      }
      
      else if (isTouching(Bullet.class))
      {
        getWorld().removeObject(this);
      
      }
      
      
     
    }    


public class bomb extends Animal
{
    
    private GreenfootImage image1;
    public bomb()
    {
      image1 = new GreenfootImage("explosion.png");  
    }
    
    public void act() 
    {
        turnTowards(850,450);
        move();
        canSee();
    }  
    
    public void move()
    {
        move(10);
    }
    
    public void canSee()
    {
        int number = 0;
        if( isTouching(Wonder.class) )
        {
           setImage("explosion.png");
           
           getWorld().removeObject(this);
      
        }
    }

}
Super_Hippo Super_Hippo

2016/10/26

#
Is the speed of the airplane above 1? Then it could jump from 449 to 451 for example and the bomb is not added.
mmccurdy mmccurdy

2016/10/26

#
No there is not a speed but i thought the bomb was being added by the if (getY() == 450) { getWorld().addObject(new bomb(), getX(), getY()); }
danpost danpost

2016/10/26

#
mmccurdy wrote...
No there is not a speed but i thought the bomb was being added by the if (getY() == 450) { getWorld().addObject(new bomb(), getX(), getY()); }
Yes, that would add a new bomb into the world. But, as Super_Hippo was trying to point out, if the airplane is never exactly at a y-coordinate of '450', a bomb will not spawn. Your airplane is moving 10 pixels forward at a time. The rotation of the airplane and where the airplane is placed into the world are two things that must be factored in. Please show code or explain these two things. As I do not see any rotating or turning in the airplane code, it leads me to wonder if you wanted the following for line 8:
if (getX() == 450)
where the x-coordinate value is used for the condition to spawn a bomb.
Nosson1459 Nosson1459

2016/10/26

#
Please show Animal class code.
Nosson1459 Nosson1459

2016/10/26

#
it shud b
if(getY()>=450)
Nosson1459 Nosson1459

2016/10/26

#
mmccurdy wrote...
No there is not a speed but i thought the bomb was being added by the if (getY() == 450) { getWorld().addObject(new bomb(), getX(), getY()); }
bec. it passes 450 w/o ever being = to 450 (i.e. it goes from 445 to 455 skipping 450 so it doesnt add it
danpost danpost

2016/10/26

#
Nosson1459 wrote...
it shud b
if(getY()>=450)
If you coded this (and bombs were created at all), it would create multiple bombs close to each other, which (I am sure) is not wanted.
Nosson1459 Nosson1459

2016/10/26

#
so do
if(getY()>=450&&getY()<=561)
(bec it moves in increments of 10)
danpost danpost

2016/10/27

#
Nosson1459 wrote...
so do
if(getY()>=450&&getY()<=561)
(bec it moves in increments of 10)
The range would have to be ten, if the speed is ten:
if (getY() >= 450 && getY() < 460)
and that is only if the actor has a rotation of 90. Like I said before, the placement and rotation of the actor are two factors that determine where it will be positioned on each act cycle.
You need to login to post a reply.