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

2017/12/14

How to make a new object type appear to replace another

protocolUnknown protocolUnknown

2017/12/14

#
Hello, for my game I want the player to be able to switch between a melee weapon or ranged weapon. To do this they can press "T" for melee or "Y" for ranged. (The below code is in my class called "Player" which is the actor the player controls):
1
2
3
4
5
6
7
8
9
if(Greenfoot.getKey()== "T")
       {
       world.newWeapon(new Melee());
      }
       
      if(Greenfoot.getKey()== "Y")
       {
        world.newWeapon(new RangedWeapon());        
      }
^This code is suppose to access a method called newWeapon() in my MyWorld class which is a child of my World class, and it is suppose to run that method in order to replace the weapon on the player currently (code for method (in MyWorld class)is below):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void newWeapon(Weapon w)
    {
        weapon = w;
        addWeaponToGame(weapon);
 
    }
     
    public void addWeaponToGame(Weapon w)
    {
//checks is weapon exists already, if it does then it will destroy it
        if(weapon != null)
        removeObject(weapon);
 
//sets weapon variable to the new weapon
        weapon =w;
        addObject(weapon, p.getX(), p.getY());
         
    }
the variable 'p' just stores a reference to the player object. The problem I am having is that pressing either 'T' or 'Y' does not change the weapons. The weapon that I already have equipped remains and never leaves. I had set it as so in my MyWorld constructor:
1
addWeaponToGame(new Melee());
So, the method does work as the melee weapon is on the player upon running the game, but it does not work for changing weapons again. Also, am I right in thinking the best way to switch object out is to destroy the original then set that variable which was holding the object to a new object? I'm thinking the problem might lay there. Any help would be appreciated thank you.
danpost danpost

2017/12/14

#
You are using 'getKey' more than once in an act step. The second call to the method will never return the same key input and usually return a 'null' value. In general, the 'getKey' method should be reserved for data input -- not for game control. Use a combination of a boolean field with the 'isKeyDown' method for game control. The field is to track the latest known state of the key so that you can tell when the state actually changes. Without the field, you only know when the key is down or up, not the moments when it goes down or up. Basically:
1
2
3
4
5
6
7
8
9
10
11
12
// field to track 't' key
private boolean tDown;
 
// in act or a methhod it calls
if (tDown != Greenfoot.isKeyDown("t")) // detects change in state of 't' key
{
    tDown = ! tDown; // saves new state
    if (tDown) // asks if key was pressed
    {
        // new weapon call to world
    }
}
and similarly for the 'y' key.
DanPost_FAN69 DanPost_FAN69

2017/12/14

#
protocolUnknown wrote...
Hello, for my game I want the player to be able to switch between a melee weapon or ranged weapon. To do this they can press "T" for melee or "Y" for ranged. (The below code is in my class called "Player" which is the actor the player controls):
1
2
3
4
5
6
7
8
9
if(Greenfoot.getKey()== "T")
       {
       world.newWeapon(new Melee());
      }
       
      if(Greenfoot.getKey()== "Y")
       {
        world.newWeapon(new RangedWeapon());        
      }
^This code is suppose to access a method called newWeapon() in my MyWorld class which is a child of my World class, and it is suppose to run that method in order to replace the weapon on the player currently (code for method (in MyWorld class)is below):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void newWeapon(Weapon w)
    {
        weapon = w;
        addWeaponToGame(weapon);
 
    }
     
    public void addWeaponToGame(Weapon w)
    {
//checks is weapon exists already, if it does then it will destroy it
        if(weapon != null)
        removeObject(weapon);
 
//sets weapon variable to the new weapon
        weapon =w;
        addObject(weapon, p.getX(), p.getY());
         
    }
the variable 'p' just stores a reference to the player object. The problem I am having is that pressing either 'T' or 'Y' does not change the weapons. The weapon that I already have equipped remains and never leaves. I had set it as so in my MyWorld constructor:
1
addWeaponToGame(new Melee());
So, the method does work as the melee weapon is on the player upon running the game, but it does not work for changing weapons again. Also, am I right in thinking the best way to switch object out is to destroy the original then set that variable which was holding the object to a new object? I'm thinking the problem might lay there. Any help would be appreciated thank you.
PUSH THE SHUTDOWN BUTTON AND THROW IT OUT OF YOUR WINDOW IT WILL WORK
protocolUnknown protocolUnknown

2017/12/15

#
danpost, that worked! I did not realise that that might be the problem. Thanks a lot.
You need to login to post a reply.