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

2015/1/20

How to move bullet creation location

1
2
howdoesonecode howdoesonecode

2015/1/20

#
as of now when i fire the bullets are creating at the center of my character i need it to be set at the front of the gun.... also when you do fire the picture of the bullet appears in the middle of the character too facing the right no matter what direction you are facing HELP!
danpost danpost

2015/1/20

#
What code are you using to create and add the bullet into the world now?
howdoesonecode howdoesonecode

2015/1/20

#
Bullet 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 Shooter
{
private int direction, speed;
    public bullet(int dir)
    {
        direction = dir;
    }


    int time=10;
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
 
    /**
     * 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()
    {
        setRotation(direction);
        move(speed);
    
        destroyEnemies ();
        move(17);
        kill();
        if (this.atWorldEdge()) 
        {
            getWorld().removeObject(this);
        }  
        }
    
 
    public bullet()
    {
        GreenfootImage image=getImage();
        image.scale(40,6);
        setImage(image);
    }  
 
    public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor enemy = getOneIntersectingObject(Enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy); 
    }
}
    public void kill()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor blueenemy = getOneIntersectingObject(Enemy.class);
        if(blueenemy != null) 
        {
            getWorld().removeObject(blueenemy);                    
        }
    }

}
"Gun" Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shooter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shooter extends Actor
{
    public void act() 
    {
        if(Greenfoot.isKeyDown("space") )
        {
            getWorld().addObject(new bullet(getRotation()), getX(), getY());
        }
        if (Greenfoot.isKeyDown ("w"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown ("s"))
        {
            move(-3);
        }
        if (Greenfoot.isKeyDown ("d"))
        {
            turn(4);
        }
        if (Greenfoot.isKeyDown ("a"))
        {
            turn (-4);
        }
    }    
}
danpost danpost

2015/1/20

#
If you break line 15 into its individual parts:
bullet b = new bullet(getRotation());
getWorld().addObject(b, getX(), getY());
you can then work with the bullet right there. That is, after those two lines, you can:
b.setRotation(getRotation()); // set its rotation
b.move(20); // adjust its initial location
You would not need to pass the rotation to the bullet class to have its rotation set there.
howdoesonecode howdoesonecode

2015/1/20

#
in the line of b.move(20); the 20 is moving it in what aspect X.. Y? and you said i dont need the b.setRotation right
Super_Hippo Super_Hippo

2015/1/20

#
The method 'move' moves the object in the direction it is facing right now.
howdoesonecode howdoesonecode

2015/1/20

#
i put this in and it stays in the same place even though the character is turning.... so if it is infront of him in one position if i turn around it goes behind him
danpost danpost

2015/1/20

#
howdoesonecode wrote...
in the line of b.move(20); the 20 is moving it in what aspect X.. Y?
The 'move' method takes into account the rotation of the actor -- the actor moves forward in the direction it faces. An actor with zero rotation will move from left to right; with rotation 90 it will move downward; 180, right to left; and at 270 it will move upward. All in-between values will have the actor move that distance at that rotation to the closest possible screen pixel.
and you said i dont need the b.setRotation right You either do not need my last line 1 above or do not need to pass the rotation to the bullet class and have it set there. Both ways will set the bullet to the proper rotation; but, you only need to set its rotation once.
howdoesonecode howdoesonecode

2015/1/20

#
Super_Hippo wrote...
The method 'move' moves the object in the direction it is facing right now.
how would i make it stay in the same place on the character no matter direction?
howdoesonecode howdoesonecode

2015/1/20

#
danpost wrote...
howdoesonecode wrote...
in the line of b.move(20); the 20 is moving it in what aspect X.. Y?
The 'move' method takes into account the rotation of the actor -- the actor moves forward in the direction it faces. An actor with zero rotation will move from left to right; with rotation 90 it will move downward; 180, right to left; and at 270 it will move upward. All in-between values will have the actor move that distance at that rotation to the closest possible screen pixel.
and you said i dont need the b.setRotation right You either do not need my last line 1 above or do not need to pass the rotation to the bullet class and have it set there. Both ways will set the bullet to the proper rotation; but, you only need to set its rotation once.
it only stays at the front in one direction it wont rotate with the character
Super_Hippo Super_Hippo

2015/1/20

#
The bullet should stay on the character?
howdoesonecode howdoesonecode

2015/1/20

#
Super_Hippo wrote...
The bullet should stay on the character?
its like if it is at the front of the gun facing right when i turn around it would be at the opposite side
Super_Hippo Super_Hippo

2015/1/20

#
I thought the bullet would fly away when it is fired (Something like move(2) in the act method.) and a new one is created every time space is pressed.
howdoesonecode howdoesonecode

2015/1/20

#
Super_Hippo wrote...
I thought the bullet would fly away when it is fired (Something like move(2) in the act method.) and a new one is created every time space is pressed.
it does fly away.... but where the bullet starts from stays in the same spot
danpost danpost

2015/1/20

#
It would be better if you explained exactly, in full and in detail, at one time, instead of having us give you what you do not want multiple times before you get what you want.
There are more replies on the next page.
1
2