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

2011/12/9

Help with calling variables in two different classes

criscotheshiz criscotheshiz

2011/12/9

#
I'm trying to call a variable from one class in another and it keeps saying its not public in Greenfoot.Actor I've declared it as public already
kiarocks kiarocks

2011/12/9

#
what variable?
criscotheshiz criscotheshiz

2011/12/9

#
I'm trying to use getRotation() to assign a value to a variable and then call it in another class so I can use setRotation() and they will have the same rotation
kiarocks kiarocks

2011/12/9

#
What your code?
criscotheshiz criscotheshiz

2011/12/9

#
Shot class where i am trying to call the variable
public class Shot  extends Actor
{
    private Crab myCrab;
    
    /**
     * Constructor for a Shot. You must specify the ship the shot comes from.
     */
    public Shot(Crab myCrab)
    {
        this.setRotation(rotation);
        this.myCrab = myCrab;
        
    }

    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int ypos = getY();
        move(6);
        if (ypos > 0) {
            ypos = ypos - 5;
            setLocation(getX(), ypos);
            Actor rock = getOneIntersectingObject(Worm.class);
            if (rock != null) {
                // We've hit an asteroid!
                hitAnAsteroid();
                getWorld().removeObject(rock);
                getWorld().removeObject(this);
            }
        }
        else {
            // I reached the top of the screen
            getWorld().removeObject(this);
        }
    }
    
    /**
     * This method gets called (from the act method, above) when the shot hits an
     * asteroid. It needs to do only one thing: increase the score counter.
     * (Everything else, such as removing the asteroid which was hit, is dealt
     * with in the act method).
     */
    private void hitAnAsteroid()
    {
        CrabWorld spaceWorld = (CrabWorld) getWorld();
        
    }
    
    
}
and Crab class where i declared the variable
public class Crab extends Actor
{
    
    /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int shotTimer = 0;
    public int rotation;
    public void act() 
    {
        rotation = this.getRotation();
        moveAndTurn();
        eat();
        shoot();
    }
    
    
    public void moveAndTurn()
    {
        
        move (6);
        if (Greenfoot.isKeyDown("left"))
        {
            this.setRotation(180);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            this.setRotation(0);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            this.setRotation(90);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            this.setRotation(270);
        }
        if ((Greenfoot.isKeyDown("up"))&&(Greenfoot.isKeyDown("right")))
        {
            this.setRotation(315);
        }
        if ((Greenfoot.isKeyDown("right"))&&(Greenfoot.isKeyDown("down")))
        {
            this.setRotation(45);
        }
        if ((Greenfoot.isKeyDown("down"))&&(Greenfoot.isKeyDown("left")))
        {
            this.setRotation(135);
        }
        if ((Greenfoot.isKeyDown("left"))&&(Greenfoot.isKeyDown("up")))
        {
            this.setRotation(225);
        }
    }
    
    public void eat()
    {
        Actor worm;
        worm = getOneObjectAtOffset(0,0,Worm.class);
        if (worm!=null)
        {
            World world;
            world = getWorld();
            world.removeObject(worm);
            Greenfoot.playSound("explode.wav");
            
        }
    }   
    
     public void shoot() 
    {

        if (shotTimer > 0) {
            shotTimer = shotTimer - 1;
        }
        else if (Greenfoot.isKeyDown("space")) {
            getWorld().addObject(new Shot(this), getX(), getY());
            setRotation(getRotation());
            shotTimer = 10; // delay next shot
        }
    }
    
    
}
kiarocks kiarocks

2011/12/9

#
You need mycrab.rotation instead of just rotation.
criscotheshiz criscotheshiz

2011/12/10

#
Okay i have no syntax errors now but when i shoot while moving up or down the shot stays in place and when i go diagonally the shot just goes left or right
kiarocks kiarocks

2011/12/10

#
It looks like somethings weird in your shot.class. Dont know what though.
davmac davmac

2011/12/10

#
In the Shot class, can you go through lines 22-25 and explain what each one does? I think this will give you a clue as to what is going wrong.
xillius200 xillius200

2012/4/21

#
change getWorld().addObject(new Shot(this), getX(), getY()); setRotation(getRotation()); to Shot shot = new Shot(); getWorld.addObject(shot,getX(),getY()); shot.setRotation(getRotation()); that should fix problem
You need to login to post a reply.