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

2015/11/1

How can I ask up the value of a variable while in another class?

Ann Ann

2015/11/1

#
So here is the code in my Hero class. (I want to be able to change the value of jumpHeight when the Hero walks into a specific object)
public class Hero extends AnimatedActor
{
  
    private int vSpeed = 0;
    private int acceleration = 2;
    public int jumpHeight = -20;  // how high to jump


    
    //I want to be able to change the value of jumpHeight when the Hero walks into a specific object
     public int getjumpHeight() 
{ 
	return jumpHeight; 
}

And this is my Object class: How can I make sure that when my Hero touches the object, his jump height increases?
public class Object extends Mover
{
    
    public void act() 
    {
         move();
            
         
         //In this class I want the jumpHeight to change when the Hero touches the object
         
         //How do I now retrieve the value of jumpHeight from the Hero class ?
         
         getjumpHeight();
      Actor hero = getOneIntersectingObject(Hero.class); //I also don't know what to do with this
        if (hero != null)
        { 
            
            jumpHeight = jumpHeight + 10;
            
             
            
            world.removeObject(this);
    }    
}
 

           
}
danpost danpost

2015/11/1

#
To begin with, using 'Object' as a subclass of anything is not recommended. The Object class is the root class of everything. Refer to the Object class API documentation. Next, why not place to code to heighten the jump when touching the object in the Hero class. By the way, increasing the height of a jump involves decreasing the jumpHeight field because it is a negative value. In the Hero class, you would only need to check for one of these objects when a jump is being made, applying its extra force at that time.
You need to login to post a reply.