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

2014/11/11

Get access of a variable in another class

Raiv3n Raiv3n

2014/11/11

#
Hi, I want to get access of a variable in another class, beause I want to have same rotation of my projectile as my shuttle. Here is the code of the shuttle:
import greenfoot.*;  
public class Shuttle extends Actor
{
    private int speed;
    private int direction;
    private int cooldown;
    public int rotation;
    public Shuttle()
    {
        setRotation(90);
        speed=5;
        direction=5;
        cooldown=0;
        
    }
    public void act() 
    {
        move1();
        turn1();
        shoot();
        isTouching();
        slowMotion();
        rotationShuttle();
        getRotationShuttle();
    }    
    public void move1()
    {
        if(Greenfoot.isKeyDown("W"))
        {
            move(speed * -1);
            
        }
        if(Greenfoot.isKeyDown("S"))
        {
            move(speed);
        }
    }
    public void turn1()
    {
        if(Greenfoot.isKeyDown("A"))
        {
            setRotation(getRotation() - direction);
        }
        if(Greenfoot.isKeyDown("D"))
        {
            setRotation(getRotation() + direction);
        }
    }
    public void shoot()
    {
        if(cooldown==0)
        {
            if(Greenfoot.isKeyDown("SPACE"))
            {
                Projectile projectile =new Projectile();
                getWorld().addObject(projectile,getX(), getY());
            
                cooldown=25;
            }
        }
        else
        {
            if(cooldown>0)
            {
                cooldown=cooldown-1;
            }
        }
    }
    public void isTouching()
    {
        if(isTouching(Ufo.class))
        {
            Greenfoot.stop();
        }
    }
    public void slowMotion()
    {
        if(Greenfoot.isKeyDown("Q"))
        {   
            
                
            Greenfoot.delay(3);
            ((Cash)getWorld().getObjects(Cash.class).get(0)).bumpCount(-40);
        }
    }
    public void rotationShuttle()
    {
        rotation=getRotation();
    }
    public int getRotationShuttle()
    {
        return rotation;
    }
}
And here of my projectile:
import greenfoot.*;  

public class Projectile extends Shuttle
{
    private int speedProjectile;
    private int rotation;
    public Projectile()
    {
        speedProjectile=8;
        
        int rotation = ((Shuttle) getWorld().getObjects(Shuttle.class).get(0)).getRotationShuttle();
       
        
        setRotation(rotation);
    }
    public void act() 
    {
        projectileMove();
        border();
    }    
    public void projectileMove()
    {
        move(speedProjectile);
    }
    public void border()
    {
        if(getX()>860 || getX()<0 || getY()>860 || getY()<0)
        {
            getWorld().removeObject(this);
        }
    }
    public void test()
    {
        int rotationShuttle = ((Shuttle) getWorld().getObjects(Shuttle.class).get(0)).getRotationShuttle();
        setRotation(rotationShuttle);
    }
}
I get this error:
java.lang.NullPointerException
	at Projectile.<init>(Projectile.java:11)
	at Space.<init>(Space.java:18)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at greenfoot.core.Simulation.newInstance(Simulation.java:596)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:437)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:483)
	at greenfoot.core.Simulation.maybePause(Simulation.java:296)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
	at Projectile.<init>(Projectile.java:11)
	at Space.<init>(Space.java:18)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at greenfoot.core.Simulation.newInstance(Simulation.java:596)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:437)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:483)
	at greenfoot.core.Simulation.maybePause(Simulation.java:296)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Hope you can help me :)
danpost danpost

2014/11/11

#
Line 7 through 15 of your Projectile class is your constructor. The constructor is called when you execute the 'new' keyword. It is what creates the object; which must be done before anything can be done with it (for an actor, that would include putting it in the world). Since it cannot be in a world during construction, 'getWorld' will always return a 'null' value when used within the constructor. There is an 'addedToWorld' method that is always called (but has no implementation until given some) that can be overridden where you can place the code you have in the constructor where it will work (using the 'getWorld' method will not be necessary as it is supplied as an argument to the method). However, you can remove lines 11 and 14 from the constructor and add insert the following at line 57 of your Shuttle class:
projectile.setRotation(getRotation());
Raiv3n Raiv3n

2014/11/16

#
Ty for your help danpost! :)
You need to login to post a reply.