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

2018/1/9

Shooting a bullet

Echarch11 Echarch11

2018/1/9

#
I am trying to add a bullet to my character where he can only shoot left and right and I am unclear on how to make any of the code. I was just wondering if anybody has any source code on how to add bullets to my game thanks.
danpost danpost

2018/1/9

#
if (<< conditions to fire >>)
{
    Bullet b = new Bullet();
    b.setRotation(180*Greenfoot.getRandomNumber(2));
    addObject(b, getX(), getY());
}
The above, when placed in act method of character, will create bullets that will randomly move either left or right.
Echarch11 Echarch11

2018/1/9

#
will I have to put anything into the bullet class to make it work? Or does everything go in my character class?
danpost danpost

2018/1/9

#
Echarch11 wrote...
will I have to put anything into the bullet class to make it work? Or does everything go in my character class?
The Bullet class should contain whatever states (fields) and/or behaviors (methods) that a bullet will have. A bullet will move (a behavior; movement also means it will have a speed state) and possibly intersect targets. An act method in the bullet class can be used for that. It should also deal with what happens when a target is not intersected with (when it reaches the edge of the world (or out of range -- or its lifetime has expired -- whatever).
xbLank xbLank

2018/1/10

#
I decided to provide you with the Shooting Method I use in my game.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Bullet extends Actor
{
    private int direction,speed;
    public Bullet(int rotation)
    {
        direction = rotation; //passing value
        speed = 15;
    }
    public void act() 
    {
        if(IsOutOfRange())
            getWorld().removeObject(this);
        setRotation(direction);
        move(speed);
    }
    private boolean IsOutOfRange()
    {
        if(this.getX() == 999) //World Edge
            return true;
        return false;
    }
}
public class Character extends Actor
{
	private int reloadtime = 10,
	    multiplier = 5;
		magSize = 10,
		currentAmmo = magSize,
		charWidth = getImage().getWidth()-45;
	public void act()
	{
		if(wait > 0)
			wait--;
        if(Greenfoot.isKeyDown("space"))
            if(wait == 0)
                Shoot();
	}
    private void Shoot()
    {
        if(currentAmmo > 0)
            currentAmmo--;
        getWorld().addObject(new Bullet(getRotation()), getX()+charWidth, getY());
        Greenfoot.playSound("/SFX/shoot.wav");
        wait += (currentAmmo > 0) ? reloadtime : reloadtime*multiplier;
    }
}
danpost danpost

2018/1/10

#
@일리아스 You only need to set the rotation of a bullet once. In the Bullet class, cut line 14 and paste it to replace line 7; then remove 'direction,' from line 4. Next, you cannot move if the bullet is removed from the world. move line 15 to before the 'IsOutOfRange' 'if' "block". Currently, your Character object does not change its rotation. Therefore all bullets will go to the right, which is what they would do without adding direction code to the Bullet class.
xbLank xbLank

2018/1/10

#
danpost wrote...
You only need to set the rotation of a bullet once. In the Bullet class, cut line 14 and paste it to replace line 7; then remove 'direction,' from line 4.
True. Mistake by my side. Thank you.
danpost wrote...
Next, you cannot move if the bullet is removed from the world. move line 15 to before the 'IsOutOfRange' 'if' "block". Currently, your Character object does not change its rotation. Therefore all bullets will go to the right, which is what they would do without adding direction code to the Bullet class.
First of all: That part is wrong. Removing the object will not cast the rest of the code. Its gone. It stops the act method. Therefore it wont "move" after being removed from the world. Second of all: As I mentioned before: This is a snippet of my game. I did not edit anything. I am giving him an idea of how it could look like. In my game you will be able to shoot in multiple directions.
danpost danpost

2018/1/10

#
@일리아스 Removing the object from the world does not make the object non-existent (otherwise, it would be extinguished before you get to place it into a world). Also, the code must run its course, top down, until a return statement or the end of the method is reached (barring run-time errors or a system exit). Now, when the method is exited from, if there are no longer any references to that actor, it will be flagged to be garbage collected. Another proof that the object still exists is that within the method 'this' still refers to that actor; so a reference is still active on it.
xbLank xbLank

2018/1/10

#
This is correct. Thank you. Edited.
You need to login to post a reply.