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

2014/8/24

How to use the getKey() method

Artyoum Artyoum

2014/8/24

#
Currently, I am trying to make my player pick up a weapon. Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rifle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rifle extends Weapon
{
    private Killable k;
    public boolean canFire;
    public int weaponCheck;
    public String g;
    
    public Rifle(Killable k_)
    {
        k = k_;
        canFire = false;
        weaponCheck = 1;
        g = new String("g");
    }
    
    /**
     * Act - do whatever the Rifle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        Actor a = (Actor)k;
        if(a.getWorld() == null)
        {
            getWorld().removeObject(this);
        }
        
        if(Greenfoot.mousePressed(null))
        {
            canFire = true;
        }
        if(Greenfoot.mouseClicked(null))
        {
            canFire = false;
        }
        
        if(findPlayer(40))
        {
            if(Greenfoot.isKeyDown("g"))
            {
                weaponCheck = 2;
                if((weaponCheck/2) == 1)
                {
                    setRotation(a.getRotation());
                    setLocation(a.getX(), a.getY());
                    setWeaponLocation(this, 25, -12);
                }
            }
            shoot();
        }
    }
    
    public void createBullet()
    {
        Bullet b = new Bullet(this);
        triangulateBulletLocation(b, getImage().getWidth()/2+1, 0);
    }
    
    public void shoot()
    {
        if(Greenfoot.getMouseInfo() != null)
        {
                if(canFire)
                    createBullet();
            
        }
    }
}
I have tried to use the getKey() method and have been unsuccessful, I do not know how to properly implement it. Can anyone help?
danpost danpost

2014/8/24

#
The details of the weapon has nothing to do with your player picking it up -- meaning that the code given is irrelevant (unless it has something to do with using 'getKey', which you say you are trying to implement; but, you do not indicate how or where you are trying to implement it). We cannot help without more info.
Artyoum Artyoum

2014/8/25

#
Ok, I have updated the scenario. http://www.greenfoot.org/scenarios/12033 I am trying to get the player to pick up and hold the Rifle. You must currently hold "g" in order to pickup and hold the weapon. I am trying to implement the getKey() method currently where the isKeyDown("g") method is.
danpost danpost

2014/8/25

#
I am looking into it now. However, you should be able to use 'isKeyDown' in conjunction with an instance field and a check to see if the weapon is already picked up or not. Maybe use a 'private Weapon weilded' field in the class of the player. I will see what I can find. In the code above, the condition on line 51 will always be true as you are setting the value of the field to two in the previous line; so half of its value will always be one (see the condition).
danpost danpost

2014/8/25

#
Couple of things -- well, a few (though, even in total, they will not completely fix your pick up issue. (1) you have two GreenfootImage fields in your Test world class that you do not need at all (after compiling several times, eventually JavaHeapSpace errors will start popping up); (2) you should probably move some (if not all) of the instance fields out of the Rifle class and put them in the Weapon class where they are more accessible; (3) unless you absolutely need the Killable interface, you could rid yourself of it (a better use of it might be as an abstract subclass of Actor); (4) the rifle created and added to the world in the world class code is not the same as the one referenced in the Player class (which is created there); do not create one in the Player class and remove the 'Rifle r' field; (5) there really should be no code in the Rifle class (or any superclass of it) for movement; a weapon does not move on its own -- it is not part of its behavior; the player moves it, and the code to move it should be in that class; If you add a 'private Weapon weilded' field to the Player class and set it to whatever weapon the player picks up, you will be able to move it from that class. Set the field back to 'null' when the weapon is dropped. Only allow the weapon to fire if (a) it intersects the player (easy way to get a quick reference to the player); and (b) the weapon wielded by the player it that weapon.
danpost danpost

2014/8/25

#
You should be a little more descriptive with your field names. It is not too terrible to use one-letter names for local fields (fields declared within a method) -- especially for common usages ('x', 'y', 'v'elocity, 'r'otation, 'w'idth, 'h'eight, etc.); but, for instance fields and class fields, you should use names that are a bit more descriptive. This also helps when using the 'find' function. Trying to find all your references to 't' in your Test world class, though there were none but the lines declaring the field and setting it, just about every line of code had one -- and many had a bunch..
Artyoum Artyoum

2014/8/25

#
There is no code for movement in the Rifle class; that is the bullet class. There are methods for setting the location of the bullet and the location of the Rifle in reference to the Player. I will try and clean up as much of the code as I can. So you are suggesting an if statement in the Player class? If not, can you clarify? Thank you.
danpost danpost

2014/8/25

#
Artyoum wrote...
There are methods for setting the location of the bullet and the location of the Rifle in reference to the Player
This is what I said should go in the Player class. Well, the bullet has movement behavior, but the Rifle does not. I was suggesting an if statement in the Weapon class to qualify when a shot can be fired.
You need to login to post a reply.