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

2017/6/14

Changing between a pistol and an rifle

Dudesame Dudesame

2017/6/14

#
hey, i'm kind of stuck trying to work this out. I want to be able to pick up a power-up which will enable me to shoot quicker, but i don't know how to do so. here is my current code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FrenchMan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FrenchMan extends ScrollingActor
{
    private int timerPistol;
    private int timerRifle;
    public int direction;
    private boolean Rifle = false; 
   
    /**
     * Act - do whatever the FrenchMan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int health = 3;



    public void act() 
    {
      move();
    eat();
    GreenfootImage left = new GreenfootImage("preview_idle.gif");
 MouseInfo mouse = Greenfoot.getMouseInfo();
 turning();
shoot();

 
    }    
    public void move()
    {
        if (Greenfoot.isKeyDown("d")){
        setLocation(getX() + 5, getY()); 
    }
 
     if (Greenfoot.isKeyDown("w")){
        setLocation(getX(), getY() - 5);
    }
        if (Greenfoot.isKeyDown("a")){
        setLocation(getX() - 5, getY());
    }
     if (Greenfoot.isKeyDown("s")){
        setLocation(getX(), getY() + 5);
    }
        
        
    }
    public void eat()
    {
        Actor Rock; 
    Rock = getOneObjectAtOffset(0,0, Rock.class); 
    if (Rock != null) 
    { 
        World world; 
        world = getWorld(); 
        world.removeObject(Rock);
         Greenfoot.playSound("332629__treasuresounds__item-pickup.wav");    
        }
    }
public void turning()
{
     MouseInfo mouse = Greenfoot.getMouseInfo();

    if(mouse != null) {
            setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));


}
 
}
public void shoot() {
    
    if (Rifle = false) 
    setImage("preview_idle.gif");
    if (timerPistol > 0) timerPistol--; // run timer
    if (timerPistol == 0 && Greenfoot.isKeyDown("space"))
    {
         Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        direction = getRotation();
        b.setRotation(direction);
        Greenfoot.playSound("gun-gunshot-02.wav");
        timerPistol = 30; // start timer (adjust value as needed)
   

   
   
    
       
    }
}


}
lookaround lookaround

2017/6/14

#
Somthing like this at the end ?
        if (Rifle == false) {
            setImage("preview_idle.gif");
            if (timerPistol > 0) {
                timerPistol--; // run timer
            }
            if (timerPistol == 0 && Greenfoot.isKeyDown("space"))
            {
                Bullet b = new Bullet();
                getWorld().addObject(b, getX(), getY());
                direction = getRotation();
                b.setRotation(direction);
                Greenfoot.playSound("gun-gunshot-02.wav");
                timerPistol = 30; // start timer (adjust value as needed)
            }
        }
        else {
                        if (timerRifle > 0) {
                timerRifle--; // run timer
            }
            if (timerRifle == 0 && Greenfoot.isKeyDown("space"))
            {
                Bullet b = new Bullet();
                getWorld().addObject(b, getX(), getY());
                direction = getRotation();
                b.setRotation(direction);
                Greenfoot.playSound("gun-gunshot-02.wav");
                timerRifle = 15; // start timer (adjust value as needed)
            }
        }
but i do recommend that shoot is only activated when space is pressed. You could make another method in your act method that makes that your timer counts down.
Dudesame Dudesame

2017/6/14

#
Thank you, but when i collect my power-up now, the rifle power-up only stays for split second then it reverts back to the default pistol. here is my current code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FrenchMan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FrenchMan extends ScrollingActor
{
    private int timerPistol;
    private int timerRifle;
    public int direction;
    private boolean Rifle = false; 
   
    /**
     * Act - do whatever the FrenchMan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int health = 3;



    public void act() 
    {
      move();
    eat();
    GreenfootImage left = new GreenfootImage("preview_idle.gif");
 MouseInfo mouse = Greenfoot.getMouseInfo();
 turning();
shoot();
gunfind();
 
    }    
    public void move()
    {
        if (Greenfoot.isKeyDown("d")){
        setLocation(getX() + 5, getY()); 
    }
 
     if (Greenfoot.isKeyDown("w")){
        setLocation(getX(), getY() - 5);
    }
        if (Greenfoot.isKeyDown("a")){
        setLocation(getX() - 5, getY());
    }
     if (Greenfoot.isKeyDown("s")){
        setLocation(getX(), getY() + 5);
    }
        
        
    }
    public void eat()
    {
        Actor Rock; 
    Rock = getOneObjectAtOffset(0,0, Rock.class); 
    if (Rock != null) 
    { 
        World world; 
        world = getWorld(); 
        world.removeObject(Rock);
         Greenfoot.playSound("332629__treasuresounds__item-pickup.wav");    
        }
    }
public void turning()
{
     MouseInfo mouse = Greenfoot.getMouseInfo();

    if(mouse != null) {
            setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));


}
 
}
public void shoot() {
    
   if (Rifle == false) {
    setImage("preview_idle.gif");
    if (timerPistol > 0) {
        timerPistol--; // run timer
    }
    if (timerPistol == 0 && Greenfoot.isKeyDown("space"))
    {
        Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        direction = getRotation();
        b.setRotation(direction);
        Greenfoot.playSound("gun-gunshot-02.wav");
        timerPistol = 30; // start timer (adjust value as needed)
    }
}
else {
                if (timerRifle > 0) {
        timerRifle--; // run timer
    }
    if (timerRifle == 0 && Greenfoot.isKeyDown("space"))
    {
        Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        direction = getRotation();
        b.setRotation(direction);
        Greenfoot.playSound("gun-gunshot-02.wav");
        timerRifle = 5; // start timer (adjust value as needed)
    }

}
}
private void gunfind(){
 
       Actor gun; 
    gun = getOneObjectAtOffset(0,0, Gun.class); 
    if (gun != null) 
    { 
        World world; 
        world = getWorld(); 
        world.removeObject(gun);
        Greenfoot.playSound("332629__treasuresounds__item-pickup.wav");
        setImage("survivor-idle_rifle_0.png");
        
    }
        


        
        
}
}
danpost danpost

2017/6/15

#
I do not see any control for the 'Rifle' field (nowhere is its value changed).
Dudesame Dudesame

2017/6/15

#
Ah ok. thank you for my help. here is the finished code for those wondering.
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FrenchMan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FrenchMan extends ScrollingActor
{
    private int timerPistol;
    private int timerRifle;
    public int direction;
    private boolean Rifle = false; 
    private int ammo; 
   
    /**
     * Act - do whatever the FrenchMan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int health = 3;



    public void act() 
    {
      move();
    eat();
    GreenfootImage left = new GreenfootImage("preview_idle.gif");
 MouseInfo mouse = Greenfoot.getMouseInfo();
 turning();
shoot();
gunfind();
ammocheck();
    }    
    public void move()
    {
        if (Greenfoot.isKeyDown("d")){
        setLocation(getX() + 5, getY()); 
    }
 
     if (Greenfoot.isKeyDown("w")){
        setLocation(getX(), getY() - 5);
    }
        if (Greenfoot.isKeyDown("a")){
        setLocation(getX() - 5, getY());
    }
     if (Greenfoot.isKeyDown("s")){
        setLocation(getX(), getY() + 5);
    }
        
        
    }
    public void eat()
    {
        Actor Rock; 
    Rock = getOneObjectAtOffset(0,0, Rock.class); 
    if (Rock != null) 
    { 
        World world; 
        world = getWorld(); 
        world.removeObject(Rock);
         Greenfoot.playSound("332629__treasuresounds__item-pickup.wav");    
        }
    }
public void turning()
{
     MouseInfo mouse = Greenfoot.getMouseInfo();

    if(mouse != null) {
            setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));


}
 
}
public void shoot() {
    
   if (Rifle == false) {
    setImage("preview_idle.gif");
    if (timerPistol > 0) {
        timerPistol--; // run timer
    }
    if (timerPistol == 0 && Greenfoot.isKeyDown("space"))
    {
        Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        direction = getRotation();
        b.setRotation(direction);
        Greenfoot.playSound("gun-gunshot-02.wav");
        timerPistol = 30; // start timer (adjust value as needed)
    }
}
else {
           if (ammo > 0)   
    if (timerRifle > 0) {
        timerRifle--; // run timer
    }
    if (timerRifle == 0 && Greenfoot.isKeyDown("space"))
    {
        Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        direction = getRotation();
        b.setRotation(direction);
        Greenfoot.playSound("gun-gunshot-02.wav");
        timerRifle = 5; // start timer (adjust value as needed)
        ammo--;
    }

}
}
private void gunfind(){
 
       Actor gun; 
    gun = getOneObjectAtOffset(0,0, Gun.class); 
    if (gun != null) 
    { 
        World world; 
        world = getWorld(); 
        world.removeObject(gun);
        Greenfoot.playSound("Pistol Gun Cock SOUND Effect.wav");
        setImage("survivor-idle_rifle_0.png");
        Rifle = true; 
        ammo = 100;
    }
        


        
        
}
private void ammocheck(){
    if (ammo <1)
    {
        Rifle = false;
       
    }
    

}
}

again, thank you for your help.
You need to login to post a reply.