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

2021/11/30

if loop not looping

Aaron-aid Aaron-aid

2021/11/30

#
im having a issue where my if loop ain't working
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class mouse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mouse extends Actor
{
    int speed = 2;
    static String shootkey = "Q";
    boolean shots = false;
    int runonce = 0;
    int x2 = 50;
    /**
     * Act - do whatever the mouse wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       PlayerStuff();
       
       devstats();
    }   
    void devstats()
    {
        //getWorld().showText("shots"+ shots, 100, 100);
    }
    void PlayerStuff()
    {
        Movement();
        Weapons();
    }
    void Movement()
    {
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
    
        if (Greenfoot.isKeyDown("w"))
        {
            move(speed);
        }
        if (Greenfoot.isKeyDown("S"))
        {
            move(-speed);
        }
    }
    void Weapons()
    {
        Pistols(P_1911.class,5,10,50);
    }
    void Pistols(Class x,int dam,int ItemSpeed,int cooldown)
    {
        int oldcooldown = cooldown;
        
        if (cooldown >= 0)
           {
               getWorld().showText("timer"+ cooldown, 100, 100);
               cooldown = cooldown - 1;
               getWorld().showText("timer"+ cooldown, 100, 100);
               
           }
        if(isTouching(x))
        {
           Actor Item = (Actor)getWorld().getObjects(x).get(0);
           Item.setLocation(getX(), getY());
           if (Greenfoot.isKeyDown(shootkey)){
           
           shots = true;
           
        }else{shots = false;}
        
        }
        if (shots == true)
        {
            
            if (runonce == 0){
             good bullet = new good();
             getWorld().addObject(bullet, getX(), getY());
           
           
           //end
             runonce = 1;
        }
          
           
           
        
        }
    }
    
}
Aaron-aid Aaron-aid

2021/11/30

#
lines 56 57 58 59 60 61 62
Aaron-aid Aaron-aid

2021/11/30

#
it runs but it only prints it once, I'm left with a print of "timer 4" and it never runs again :c
Spock47 Spock47

2021/11/30

#
Hi Aaron-aid, I think there is a misunderstanding. The if-statement is not a loop. An if-statement is only executed once. The loop in place is "outside" of your code, namely Greenfoot automatically calls the act method repeatedly. That means, that at every call of the act-method, the call "Pistols(P_1911.class,5,10,50);" is repeated (but as you can see always again with the original values, especially 50 for the countdown). Because of that, the "getWorld().showText("timer"+ cooldown, 100, 100);" will always be called with the value 50 in line 58 and directly after that with value 49 at line 60. Therefore, you will always only see "Timer: 49". What you want is that there is not a new countdown in every loop iteration, but that the same countdown is used in all loop iterations. You can achieve that by replacing the method parameter "countdown" with an attribute "countdown". However, if I guess right, you want to have more than one weapon later on and adding one countdown per weapon directly into the Mouse class would get confusing very soon. 1. So, I suggest that you create a Weapon class that stores the attributes you need for the Weapon:
public class Weapon  
{    
    final Class wClass;
    final int dam;
    final int itemSpeed;
    int cooldown;
    
    public Weapon(final Class _class, final int _dam, final int _itemSpeed, final int _cooldown)
    {
        this.wClass = _class;
        this.dam = _dam;
        this.itemSpeed = _itemSpeed;
        this.cooldown = _cooldown;
    }
    
}
(as you can see, I also included the dam and the itemSpeed properties, although they seem to be unused right now, but I guess you already have plans for them). 2. Then you can define a list of weapons in your Mouse class:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

...

private final List<Weapon> weapons = new ArrayList<Weapon>(Arrays.asList(new Weapon(P_1911.class, 5, 10, 50)));
3. and use it in the according method:
void weapons()
    {
        for (final Weapon weapon : weapons)
        {
            pistols(weapon);
        }
    }
    
    void pistols(final Weapon weapon)
    {
        if (weapon.cooldown >= 0)
        {
            getWorld().showText("timer"+ weapon.cooldown, 100, 100);
            weapon.cooldown = weapon.cooldown - 1;
            getWorld().showText("timer"+ weapon.cooldown, 100, 100);
            
        }
        final Actor touchingWeapon = this.getOneIntersectingObject(weapon.wClass);
        if(touchingWeapon != null)
        {
            touchingWeapon.setLocation(getX(), getY());
            shots = Greenfoot.isKeyDown(shootkey);
        }
        if (shots && runOnce)
        {
            getWorld().addObject(new Good(), getX(), getY());
            //end
            runOnce = false;
        }
    }
4. In total the source code of class Mouse lookslike that (note that I allowed myself to simplify it a little bit and to change upper/lower-casing to the general conventions that say "class names should start upper-case, method and variable names should start lower-case".
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

/**
 * Write a description of class mouse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mouse extends Actor
{
    int speed = 2;
    static String shootkey = "Q";
    boolean shots = false;
    private boolean runOnce = true;
    int x2 = 50;
    
    private final List<Weapon> weapons = new ArrayList<Weapon>(Arrays.asList(new Weapon(P_1911.class, 5, 10, 50)));
    
    /**
     * Act - do whatever the mouse wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       playerStuff();
       devStats();
    }   

    void devStats()
    {
        //getWorld().showText("shots"+ shots, 100, 100);
    }
    
    void playerStuff()
    {
        movement();
        weapons();
    }
    
    void movement()
    {
        final MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            turnTowards(mouse.getX(), mouse.getY());
        }
     
        if (Greenfoot.isKeyDown("w"))
        {
            move(speed);
        }
        if (Greenfoot.isKeyDown("S"))
        {
            move(-speed);
        }
    }
    
    void weapons()
    {
        for (final Weapon weapon : weapons)
        {
            pistols(weapon);
        }
    }
    
    void pistols(final Weapon weapon)
    {
        if (weapon.cooldown >= 0)
        {
            getWorld().showText("timer"+ weapon.cooldown, 100, 100);
            weapon.cooldown = weapon.cooldown - 1;
            getWorld().showText("timer"+ weapon.cooldown, 100, 100);
            
        }
        final Actor touchingWeapon = this.getOneIntersectingObject(weapon.wClass);
        if(touchingWeapon != null)
        {
            touchingWeapon.setLocation(getX(), getY());
            shots = Greenfoot.isKeyDown(shootkey);
        }
        if (shots && runOnce)
        {
            getWorld().addObject(new Good(), getX(), getY());
            //end
            runOnce = false;
        }
    }
     
}
Now, the timer runs down from 50 to -1 (actually quite fast) and you can easily add other weapons later on by just changing the initialisation line of the list (from step 2). Live long and prosper, Spock47
Aaron-aid Aaron-aid

2021/11/30

#
oooh, thanks man!
You need to login to post a reply.