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

2013/5/19

Magazine to gun.

Miikku Miikku

2013/5/19

#
How I can have a magazine to gun? Help plz.
Miikku Miikku

2013/5/19

#
Some one help please.
nooby123 nooby123

2013/5/19

#
You can try adding a variable to represent the amount of magazines the gun has left. Otherwise, please specify your scenario XD
Miikku Miikku

2013/5/19

#
GreenGoo GreenGoo

2013/5/19

#
Once again, it is simple. Have a variable called Ammo in your player class. Every time you fire, presumably by clicking the left mouse button, decrease this variable. Then, when Ammo = 0, have a delay before you reset Ammo to 8, for example, and then you can fire again. In the if statement controlling when you fire, add the condition that Ammo has to be greater than 0.
Miikku Miikku

2013/5/19

#
Code please?
GreenGoo GreenGoo

2013/5/19

#
int Ammo = 10; int reloadDelay = 100; if (Greenfoot.isKeyDown("space") && Ammo > 0) { Code to fire a bullet Ammo--; } if (Ammo <= 0) { reloadDelay--; if (reloadDelay <= 0) { Ammo = 10; reloadDelay = 100; } }
Miikku Miikku

2013/5/19

#
do I put this to Bullet or Soldier who shoots?
GreenGoo GreenGoo

2013/5/19

#
The soldier.
Miikku Miikku

2013/5/19

#
This doesen't act:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Soldier extends Actor
{

private int soldierSpeed = 3;

private GreenfootImage soldierWalk1 = new GreenfootImage("Roboani1.png");
private GreenfootImage soldierWalk2 = new GreenfootImage("Roboani2.png");
private GreenfootImage soldierWalk3 = new GreenfootImage("Roboani3.png");
private int animationCount = 0;
private int frame = 1;

private int mouseX;
private int mouseY;

public static int currentX;
public static int currentY;

public static int currentRot;

int Ammo = 10;
int reloadDelay = 100;



    public void act() 
    {
        move();
        faceMouse();
        shoot();
        
        
        currentX = getX();
        currentY = getY();
        
        currentRot = getRotation();
        
    } 
    
    public void move()

    {
        animationCount ++;
        
        if(Greenfoot.isKeyDown("w"))
        {
            move(3);
            if(animationCount % 8 == 0)
            {
                animationWalk();
            }
        }
        else if(Greenfoot.isKeyDown("s"))
        {
            move(-3);
            if(animationCount % 8 == 0)
            {
                animationWalk();
            }
        }
        else
           {
               setImage(soldierWalk1);
           }
     }
           
    public void animationWalk()
    {
        if(frame == 1)
        {
            setImage(soldierWalk1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage(soldierWalk2);
            frame = 3;
        }
        else if(frame == 3)
        {
            setImage(soldierWalk3);
            frame = 1;
        }
    }
    
    public void faceMouse()
    {
        if(Greenfoot.getMouseInfo() != null)
        {
        mouseX = Greenfoot.getMouseInfo().getX();
        mouseY = Greenfoot.getMouseInfo().getY();
        
        turnTowards (mouseX, mouseY);
        }
    }
    
    public void shoot()
    {
        if (Greenfoot.isKeyDown("space") && Ammo > 0)
        {
            if(Greenfoot.getMouseInfo() != null)
            {
                if(Greenfoot.getMouseInfo().getButton() == 1)
                {                
                    getWorld().addObject(new Bullet(), getX(), getY());
                    Ammo--;
                }
                if (Ammo <= 0)
                {
                    reloadDelay--;
                    if (reloadDelay <= 0)
                    {
                        Ammo = 10;
                        reloadDelay = 100;
                    }
                }
            }
        }
    }   
}
Miikku Miikku

2013/5/19

#
Help?
You need to login to post a reply.