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

2013/12/17

How to change a varible of another class in a different class

JasonZhu JasonZhu

2013/12/17

#
This is my rocket where I am looking for an intersecting Supply to increase its own bullets or fuel from the respective Supply type.
    private void checkPowerUp()
    {        
        Actor powerUp = getOneIntersectingObject(Supply.class);
        if(powerUp!=null){
            if(powerUp.getType()==1){
                bullets=bullets+50;
            }
            if(powerUp.getType()==0){
                fuel=fuel+100;
            }
            getWorld().removeObject(powerUp);
        }                
    }
This is my Supply constructor, it has 2 types.
    
    public int type;

public Supply(int type)
    {
        addForce(singlePush);
        life = 350;
        switch(type){
            case 0: 
            setImage("gas.png");break;
            case 1:
            setImage("bullets.png");break;
        }
        this.type=type;
    }
I am basically having trouble writing the intersection part of the code in the method checkPowerUp(). How do I check the Supply's type from the way I've written the code? The way I've written the code doesn't compile properly.
danpost danpost

2013/12/17

#
In the Supply constructor, line 3 should be 'public Supply(int type)' and line 5 appears to be out of place (possibly it goes in the 'act' method, though hard to say considering the lack of information). Also, the 'getType' method is not given. Plus, you cannot call that method on an object of type Actor; it needs to be of type Supply.
You need to login to post a reply.