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

2021/6/21

Problem with getting a variable

Turbo_Thorsten Turbo_Thorsten

2021/6/21

#
How can I get a boolean from a specific object (this) into another class?
danpost danpost

2021/6/21

#
Turbo_Thorsten wrote...
How can I get a boolean from a specific object (this) into another class?
Need specifics and attempted codes.
Turbo_Thorsten Turbo_Thorsten

2021/6/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static double enemieskilled;
    public static int b;
    int c;
    public void act() 
    {
        move (10);
        if (isTouching(Border.class))
        {
            removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
        }
        else if (isTouching(Bacteria.class))
        {
         removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
         enemieskilled++;
        }
        else if (isTouching(Tumor.class))
        {
            if (Tumor.badtumor == false)
            {
                removeTouching (Tumor.class);  
                getWorld().removeObject(this);
                enemieskilled++;
                if (Bacteria.bacteria > 2)
                {
                    Bacteria.bacteria--;
                }
                if (Virus.virus > 3)
                {
                    Virus.virus--;
                }
            }
            else
            {
                if (Bacteria.bacteria < 4)
                {
                    Bacteria.bacteria++;
                }
                if (Virus.virus < 5)
                {
                    Virus.virus++;
                }
            }
        }
        else if (isTouching(Virus.class))
        {
         removeTouching (Virus.class); 
         enemieskilled++;
         if(Greenfoot.getRandomNumber(100) < 30)
         {
             c = Greenfoot.getRandomNumber(5);
             switch(c)
                {
                 case 0:
                 nuke();
                 NukeScreen object1 = new NukeScreen ();
                 getWorld().addObject(object1, 350, 50);
                 object1.nukeScreen();
                 MyWorld.p++;
                 break;
                 case 1:
                 Player.timerA = Player.timerA + 300;
                 Player.k = 5;
                 FasterShootingScreen object2 = new FasterShootingScreen ();
                 getWorld().addObject(object2, 250, 50);
                 MyWorld.p++;
                 break;
                 case 2:
                 Player.timerA = Player.timerA + 300;
                 Player.k = 5;
                 FasterShootingScreen object3 = new FasterShootingScreen ();
                 getWorld().addObject(object3, 250, 50);
                 MyWorld.p++;
                 break;
                 case 3:
                 MyWorld.freezeTimer = MyWorld.freezeTimer + 400;
                 FreezeScreen object4 = new FreezeScreen ();
                 getWorld().addObject(object4, 450, 50);
                 MyWorld.p++;
                 break;
                 case 4:
                 MyWorld.freezeTimer = MyWorld.freezeTimer + 400;
                 FreezeScreen object5 = new FreezeScreen ();
                 getWorld().addObject(object5, 450, 50);
                 MyWorld.p++;
                }
         } 
         getWorld().removeObject(this);
        }
        else if (isAtEdge()) 
        {
         getWorld().removeObject(this);
        }
    }   
    public void nuke()
    {
    if (getWorld() != null)
        {
        getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
        getWorld().removeObjects(getWorld().getObjects(Virus.class));
     }
    }
    
}
Turbo_Thorsten Turbo_Thorsten

2021/6/22

#
At line 34 I want to check the Tumor which is touched by the bullet
danpost danpost

2021/6/22

#
Turbo_Thorsten wrote...
At line 34 I want to check the Tumor which is touched by the bullet
You cannot use a class name to acquire the state of an instance. As you may have many instances created from a class, the question of which one you want the state of is left open. You need a reference to the Tumor instance being touched. Use getOneIntersectingObject and cast its type as Tumor class object to allow access to the field value (provided it was made public).
Turbo_Thorsten Turbo_Thorsten

2021/6/22

#
So how do I have to integrate this?
danpost danpost

2021/6/22

#
Turbo_Thorsten wrote...
So how do I have to integrate this?
danpost wrote...
Use getOneIntersectingObject and cast its type as Tumor class object to allow access to the field value (provided it was made public).
Turbo_Thorsten Turbo_Thorsten

2021/6/22

#
I'm not a native speaker and I don't quite understand how to implement this
danpost danpost

2021/6/23

#
Try this for line 34:
if ( !((Tumor)getOneIntersectingObject(Tumor.class)).badtumor )
You need to login to post a reply.