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

2015/2/20

Help with bullet rotation after shot....

Hnkzx Hnkzx

2015/2/20

#
Rocket's code:
public class Rocket extends Animal
{
   public void act()
   {
       lookForRobot();
       if(Greenfoot.isKeyDown("left"))
       {
           turn(-5);
        }
       if(Greenfoot.isKeyDown("Right"))
       {
           turn(5);
        }
       if(Greenfoot.isKeyDown("Up"))
       {
           move(10);
        }
       if(Greenfoot.isKeyDown("Shift"))
       {
          move(20);
        }
       if(Greenfoot.isKeyDown("Space"))
       {
          getWorld().addObject(new Bullet(), getX(), getY());
        }
    }
   public void lookForRobot()
    {
        if(canSee(Robot.class)) 
        {
            eat(Robot.class);
        }
    } 
   public void shootBullet()
   {

    }
}
Bullet's Code: public class Bullet extends Animal { public void act() { Rocket rocket = (Rocket) getWorld().getObjects(Rocket.class).get(0); setRotation(rocket.getRotation()); move(20); lookForAlien(); deleteAtEdge(); } public void lookForAlien() { if(canSee(Alien.class)) { eat(Alien.class); } } public void deleteAtEdge() { if (atWorldEdge()) { getWorld(). removeObject(this); } } private int speed = 10; }
Hnkzx Hnkzx

2015/2/20

#
I need help making the bullet shoot at the direction that it was originally shot at, rather than rotating with the rockets movements
Hnkzx Hnkzx

2015/2/20

#
Bullets code:
public class Bullet extends Animal
{
public void act()
    {
        Rocket rocket = (Rocket) getWorld().getObjects(Rocket.class).get(0);
        setRotation(rocket.getRotation());
        move(20);
        lookForAlien();
        deleteAtEdge();
    }
public void lookForAlien()
    {
        if(canSee(Alien.class)) 
        {
            eat(Alien.class);
        }
    }
public void deleteAtEdge()
    {
        if (atWorldEdge())
        {
            getWorld(). removeObject(this);
        }
    }

private int speed = 10;
}
danpost danpost

2015/2/20

#
Code within the 'act' method will execute repeatedly while the actor is in the world. Lines 5 and 6 of the Bullet class has the bullet repeatedly setting its rotation to that of the Rocket object. You only need this action done one time when the bullet is created and added to the world. There are two places you can do this at in your code. You can override the 'addedToWorld(World)' method of the Actor class and put the code there or you can put the code in the Rocket class where you create and add the bullet into the world. The second option is the one I prefer; firstly, because it is the Rocket that determines the rotation of the bullet; and, secondly, the bullet needs to find or be supplied the Rocket object or its rotation while the Rocket, on the other hand, will have the Bullet object readily available to it and can perform the required steps immediately. First, remove lines 5 and 6 from the Bullet class code. Then replace line 24 of the Rocket class with the following:
Bullet bullet = new Bullet();
addObject(bullet, getX(), getY());
By splitting up the line you had, we now have a reference to the bullet in the 'bullet' variable. Now, follow that with the following line:
bullet.setRotation(getRotation());
And you are done (at least with the rotation of the bullet).
Hnkzx Hnkzx

2015/2/20

#
there seems to be a problem : addObject(bullet, getX(), getY()); as it says it cannot find the symbol - method addObject(Bullet,Int,Int)
danpost danpost

2015/2/20

#
Hnkzx wrote...
there seems to be a problem : addObject(bullet, getX(), getY()); as it says it cannot find the symbol - method addObject(Bullet,Int,Int)
Yeah, the code is in an Actor subclass. It should be 'getWorld().addObject........'
Hnkzx Hnkzx

2015/2/20

#
Thank you... however it did not seem to fix the problem
danpost danpost

2015/2/20

#
Hnkzx wrote...
Thank you... however it did not seem to fix the problem
Explain. Is it still keeping the same rotation as the Rocket object, turning with it? Please post the revised Bullet and Rocket classes.
Hnkzx Hnkzx

2015/2/20

#
Bullet:
public class Bullet extends Animal
{
public void act()
    {
        move(20);
        lookForAlien();
        deleteAtEdge();
    }
public void lookForAlien()
    {
        if(canSee(Alien.class)) 
        {
            eat(Alien.class);
        }
    }
public void deleteAtEdge()
    {
        if (atWorldEdge())
        {
            getWorld(). removeObject(this);
        }
    }

private int speed = 10;
}
Rocket:
public class Rocket extends Animal
{
   public void act()
   {
       lookForRobot();
       if(Greenfoot.isKeyDown("left"))
       {
           turn(-5);
        }
       if(Greenfoot.isKeyDown("Right"))
       {
           turn(5);
        }
       if(Greenfoot.isKeyDown("Up"))
       {
           move(10);
        }
       if(Greenfoot.isKeyDown("Shift"))
       {
          move(20);
        }
       if(Greenfoot.isKeyDown("Space"))
       {
          Bullet bullet = new Bullet();
          getWorld(). addObject(bullet, getX(), getY());
        }
    }
   public void lookForRobot()
    {
        if(canSee(Robot.class)) 
        {
            eat(Robot.class);
        }
    } 
}
danpost danpost

2015/2/20

#
You did not do the final step as far as what I suggested you do (you are missing the follow-up to lines 24 and 25).
danpost wrote...
First, remove lines 5 and 6 from the Bullet class code. Then replace line 24 of the Rocket class with the following:
Bullet bullet = new Bullet();
addObject(bullet, getX(), getY());
By splitting up the line you had, we now have a reference to the bullet in the 'bullet' variable. Now, follow that with the following line:
bullet.setRotation(getRotation());
And you are done (at least with the rotation of the bullet).
You need to login to post a reply.