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

2015/12/14

Calling actor from another class?

GodOfAllTime GodOfAllTime

2015/12/14

#
Hi, so I am making a game in which you throw a knife to kill enemies, and the code for that is here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public boolean hasKnife = false;
    public void pickUpKnife()
    {
        if(hasKnife = false)
        {
            Actor tKnife;
            tKnife = getOneObjectAtOffset(0, 0, ThrowingKnife.class);
            if(tKnife != null)
            {
                World world = getWorld();
                getWorld().removeObject(tKnife);
 
                hasKnife = true;
            }
        }
    }
     
    public void throwKnife()
    {
        if(hasKnife = true)
        {
            if(Greenfoot.isKeyDown("space"))
            {
                getWorld().addObject(new ThrowingKnife(), getX(), getY());
            }
        }
    }
Basically, at the begining of the game, the knife appears and you must pick it up. Once you do, the above code executes. However, for the projectiles to work, the knife must be moving, so in the throwing knife class I have to do this:
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
    moving();
}  
 
public void moving()
{
    if(hasKnife = true)
    {
        move(5);
    }
}
However, once I do this the error, "cannot find symbol - variable hasKnife" appears. I thought I could call the actor with Actor mainplayer = new MainPlayer(), but that didn't work. Is there a way to access boolean between classes? If so, how? Thank you very much in advance!
fejfo fejfo

2015/12/14

#
the hasKnife variable is in the Player class so you can't access it from the Knife class so first of in the player the hasKnife must be public :
1
public boolean hasKnife = true/false;
and in the Knife you need to keep a refference to the player who trowed it :
1
2
3
4
Player thrower;
public Knife(Player thrower) {
    this.thrower = thrower;
}
when generating the knife you should use :
1
new Knife(this);
now you can use :
1
thrower.hasKnife
in the Knife class and you have :
1
if(hasKinfe = true) ...
in an if you should use "==" and for a boolean you don't need to use anthing at all you can just do : "if(thrower.hasKnife) ..."
mik mik

2015/12/14

#
In this case, you don't actually need to check the 'hasKnife' boolean at all. If the ThrowingKnife act method executes, then obviously the ThrowingKnife exists. And if it exists, then the player must have had it before (otherwise you would not have created it). And, in fact, hasKnife may well be false again while the knife is moving, because the player has thrown it already. In short: the knife, when it acts, can just move, no matter what. (Maybe a bit faster than move(5)...). Also, pay attention to what fejfo said about the = symbol: in an if statement, it has to be ==:
1
if (hasKnife == true) ...
or simply:
1
if (hasKnife) ...
danpost danpost

2015/12/14

#
mik wrote...
In this case, you don't actually need to check the 'hasKnife' boolean at all. If the ThrowingKnife act method executes, then obviously the ThrowingKnife exists. And if it exists, then the player must have had it before (otherwise you would not have created it).
That would be true if the knife was still to begin with so the player can pick it up. The knife is going to need to work in two different states, an unthrown state and a thrown state. This can be done simply by adding an int speed field to the class initially set to zero. When thrown, the value can be changed so that it moves.
You need to login to post a reply.