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

2013/11/16

bullet help

1
2
KevinWelch2001 KevinWelch2001

2013/11/16

#
I have it so when the mouse is clicked it makes bullets spawn but it move's in only one direction I want it to shoot in the direction of were the spaceship is facing here is the code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(2);
        foundasteroid();
        shotasteriod();
    }    
      public boolean foundasteroid()
    {
        Actor asteroid = getOneObjectAtOffset(2, 2, asteroid.class);
        if(asteroid != null) 
        {
            return true; 

        }
        else {
            return false;
        }
    }

    /**
     * destroy's asteroid and bullet.
     */
    public void shotasteriod()
    {
        Actor asteroid = getOneObjectAtOffset(2, 2, asteroid.class);
        if(asteroid != null) {
            Greenfoot.playSound("Explosion.mp3");
            getWorld().removeObject(asteroid);
            getWorld().removeObject(this);

        }
    }
}
KevinWelch2001 KevinWelch2001

2013/11/17

#
can anybody help me?
danpost danpost

2013/11/17

#
The spaceship class is where you are creating and adding a new bullet object into the world; you should probably set its rotation there also. Moving only two pixels per act will allow only about 16 different angles to move in. This would make an average of 22 to 23 degrees between adjacent angles. If you do not wish to increase the speed, you might consider using double values to track the location of the bullet for smoother movement.
KevinWelch2001 KevinWelch2001

2013/11/17

#
and what would setting the rotation of the bullet look like in code
RUMMAKER RUMMAKER

2013/11/17

#
pretty much what danpost said, and the boolean can be made more efficient with return asteriod != null; instead of the if else block
RUMMAKER RUMMAKER

2013/11/17

#
after creating the thing to shoot Bullet b = new Bullet(); b.setRotation(angle you want);
KevinWelch2001 KevinWelch2001

2013/11/17

#
how do I set it to the the same rotation as the space ship the it can rotate itself?
danpost danpost

2013/11/17

#
RUMMAKER wrote...
after creating the thing to shoot Bullet b = new Bullet(); b.setRotation(angle you want);
The angle you want is the angle of the Spaceship (if your spaceship with a rotation of zero faces right). If you are creating the bullet in the class of the spaceship, you get the rotation of the spaceship with the 'getRotation' method.
RUMMAKER RUMMAKER

2013/11/17

#
if the object creating the bullet is the space ship you gota make a method in ur spaceShip calss like public void fireBullet() { Bullet b = new Bullet();//makes the bullet b.setRotation(getRotation());//sets the rotation of the bullet to be the same as the ship's getWorld().addObject(b, getX(), getY()); }
KevinWelch2001 KevinWelch2001

2013/11/17

#
ty very much
KevinWelch2001 KevinWelch2001

2013/11/17

#
I have another question I used atWorldEdge in my bullet class so it would disappear when it hit the world edge. but this comes up java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge(Bullet.java:53) at Bullet.act(Bullet.java:20) at greenfoot.core.Simulation.ac
RUMMAKER RUMMAKER

2013/11/17

#
that means you are trying to do something with the bullet after it has been removed. just put return; after removing the bullet and it should be fine; if that does not work then put a if statement in front of all ur code to check to see if bullet is in world like this act() { if(getWorld() != null)//is in a world { * ur code } }
KevinWelch2001 KevinWelch2001

2013/11/17

#
here is all of the code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Actor { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(5); if(getWorld() != null) { } shotasteriod(); if ( atWorldEdge() ) { getWorld().removeObject(this); } } /** * destroy's asteroid and bullet. */ public void shotasteriod() { Actor asteroid = getOneObjectAtOffset(2, 2, asteroid.class); if(asteroid != null) { Greenfoot.playSound("Explosion.mp3"); getWorld().removeObject(asteroid); getWorld().removeObject(this); } } public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } }
RUMMAKER RUMMAKER

2013/11/17

#
public void act() { move(5); shotasteriod(); if ( atWorldEdge() ) { getWorld().removeObject(this); return; } } you also don't need the if(getWorld() != null) thing in this case
KevinWelch2001 KevinWelch2001

2013/11/17

#
I figured out that the problem is when it hits the asteroid it causes the error
There are more replies on the next page.
1
2