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

2016/5/28

How do get variable from another class

FutureCoder FutureCoder

2016/5/28

#
I have a counter on one class, and also need it for another class, or it doesn't work. How do I get variable from another class.
danpost danpost

2016/5/28

#
FutureCoder wrote...
I have a counter on one class, and also need it for another class, or it doesn't work. How do I get variable from another class.
Need some attempted code to work with and more detailed background information.
FutureCoder FutureCoder

2016/5/28

#
I have 'private int counter = 0;' in the code for one character and want it so that if it is 10 removes one characters life counter. I just need to get variable from the first character.
danpost danpost

2016/5/28

#
FutureCoder wrote...
I have 'private int counter = 0;' in the code for one character and want it so that if it is 10 removes one characters life counter. I just need to get variable from the first character.
You have 'private int counter = 0;' in the code for one character (what type character is it and how many of them will be in the world at any time?) and want it so that, if it is 10, a life counter (is this a set of actors or a field) from one character (what type character is it and how many of them will be in the world at any time?). Finally, what is the code for the first character (which has the field whose value needs to be acquired in the other) and what is the code of the second character (which needs the value of the field in the first).
FutureCoder FutureCoder

2016/5/28

#
Around five will be in the world and the character is an enemy, this is the code
private int start = 0;
private int attack = 0;
private int normal = 0;
public void killHero()
{
    if(start == 0)
    {
        attack = 0;
        normal = normal +1;
     if(c == 20)
     {
       start = 1;
     }
    }
    if(start == 1)
    {
        setImage("enemy2.png");
        attack = attack+1;
    }
    if(attack == 10)
    {
        setImage("enemy.png");
        normal = 0;
        start = 0;
    }
}
enemy 2 is when it is attacking. I tried using get random number, but it was to unrealistic, and went way to fast, so i created a bunch of counters, so it has the same space of time in between each attack. And when the enemy is attaching, on timer attack that is when it should take life from main character. I copied the code and put it in main character but doesn't work that great. I'm trying to get variable from enemy but don't know how.
danpost danpost

2016/5/28

#
I am not sure how this could even compile. You are using an undeclared variable, 'c', on line 10. I am guessing that 'c' is supposed to be 'normal', which leads me to believe that you want the normal image for 20 act cycles, then the attack for 10 cycles, and then repeating. This only requires one counter (which I will get to shortly). The best way to determine whether an enemy is attacking or not (which is what I believe you are trying to do by acquiring the value of the field in the class of the main character) is by checking what image is currently set to the enemy. However, to do that, you will need the images of the enemy placed in fields in the class of the enemy. Also, since all enemy will be using the same two images, you can make them class fields as opposed to instance fields (the class can hold the images for the objects instead of each object individually holding both images). This would then look like this:
// class fields
public static final GreenfootImage normalImage = new GreenfootImage("enemy1.png");
public static final GreenfootImage attackImage = new GreenfootImage("enemy2.png");

// instance field
private int timer;

// the 'killHero' method
public void killHero()
{
    timer++;
    if (timer == 20) setImage(attackImage);
    if (timer == 30)
    {
        timer = 0;
        setImage(normalImage);
    }
}
Then in the main actor class, to see if an enemy is attacking, you could use something like:
Enemy enemy = getOneIntersectingObject(Enemy.class);
if (enemy != null && enemy.getImage() == Enemy.attackImage)
FutureCoder FutureCoder

2016/5/28

#
c is a typo, sorry about that. thank you
FutureCoder FutureCoder

2016/5/28

#
Thanks, but now its saying 'public static final GreenfootImage normalImage = new GreenfootImage("enemy1.png");' is an illegal start of expression. Is this because i put it in the wrong space
FutureCoder FutureCoder

2016/5/28

#
Never mind, i was able to fix it
FutureCoder FutureCoder

2016/5/28

#
Now it is saying cannot find symbol for variable '.attackImage' . How could i fix this
danpost danpost

2016/5/28

#
FutureCoder wrote...
Now it is saying cannot find symbol for variable '.attackImage' . How could i fix this
Where is it saying this (which class and which line)? and what is the code that you tried on that line?
FutureCoder FutureCoder

2016/5/28

#
if (enemy != null && enemy.getImage() == enemy.attackImage) it is saying .attackImage is wrong
danpost danpost

2016/5/28

#
FutureCoder wrote...
if (enemy != null && enemy.getImage() == enemy.attackImage) it is saying .attackImage is wrong
The last 'Enemy' should be the class name of the enemy -- it does not refer to any object of the class.
FutureCoder FutureCoder

2016/5/28

#
Thanks it works now
You need to login to post a reply.