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

2017/4/3

how to access variable from intersecting object

alfonsc alfonsc

2017/4/3

#
Actor bullet = getOneIntersectingObject(Bullet.class);
        if (bullet != null)
        {
            check = true;
        }
        if (bullet == null && check == true)
        {
            lifes = lifes - bullet.value;
            getWorld().addObject(enemyLifes, getX(), getY() - 35);
            enemyLifes.multiplier = enemyLifes.multiplier - bullet.value;
            check = false;
        }
I'm trying to access the variable "value" from the actor "bullet" but it doesn't work. The following message appears: "cannot find symbol - variable value". I'm guessing it's because it's an actor and not a class. Can someone help?
Super_Hippo Super_Hippo

2017/4/3

#
Try to change line 1 to this:
Bullet bullet = (Bullet) getOneIntersectingObject(Bullet.class);
danpost danpost

2017/4/3

#
You have your conditions backwards. The code block, lines 3 through 5, will execute if a bullet intersects and the block, lines 7 through 12, will execute without an intersecting bullet; yet that is where you are trying to access the value. Also, with the 'bullet' variable reference declared as type Actor, the 'value' field will never be found. Change line 1 to:
Bullet bullet = (Bullet)getOneIntersectingObject(Bullet.class);
and swap 'bullet != null' with 'bullet == null' (in lines 2 and 6).
alfonsc alfonsc

2017/4/3

#
Thank you. It worked but now, as soon as I hit the target Greenfoot stops and I get the message: "java.lang.NullPointerException"
danpost danpost

2017/4/3

#
alfonsc wrote...
Thank you. It worked but now, as soon as I hit the target Greenfoot stops and I get the message: "java.lang.NullPointerException"
You will need to show the entire Bullet class.
alfonsc alfonsc

2017/4/3

#
Got it! Thanks for the help!
You need to login to post a reply.