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

2020/5/13

How can i get the height and width of an object in another class?

cantmember cantmember

2020/5/13

#
I am making a game kind of like agar.io that i have to make it so if the user controlled object is bigger than the enemy, it "eats" it and removes the enemy from the world. I'm having a hard time figuring out how to get the Height and Width of the other class because it doesn't let me share variables between class. I already have the code for checking if you are touching an enemy and removing it by the way. Thanks in advance
public void killEnemy()
    {
        if (canSee(Enemy.class))
        {
            GreenfootImage User = getImage();
            int x  = User.getHeight();
            int y = User.getWidth();
            if (x > Enemy.getHeight() & y > Enemy.getHeight())
                {
                   eat (Enemy.class); 
                    
                }
                else 
                {
                    eat (User.class);
                }
       

        }
danpost danpost

2020/5/14

#
Enemy is a Class name (or a Type). A class does not have an image. Instances created from the class do. Use getOneIntersectingObject to obtain a reference to the "seen" Enemy instance.
You need to login to post a reply.