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

2012/7/24

help with subclass referencing and 'cannot find symbol - ...' errors

sparrow72 sparrow72

2012/7/24

#
I tried to insert a screen-shot of my 'Actor classes' but couldn't, but this is mostly what it looks like: -World classes- World Background ... -Actor classes- Actor Menu Start Buttons Back Controls Credits Levels Level1 Pause Ready Settings ...
public class Start extends Menu
{
    /**
     * Act - do whatever the Start wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Start start = new Start();
        getWorldaddObject(start, 300, 200);
        
        Levels levels = new Levels();
        addObject(levels, 175, 75);
        
        Controls controls = new Controls();
        addObject(controls, 425, 75);
        
        Settings settings = new Settings();
        addObject(settings, 425, 325);
        
        Credits credits = new Credits();
        addObject(credits, 175, 325);

    }    
}
On the line ‘getWorldaddObject(start, 300, 200);’ I got the error: cannot find symbol – variable getWorld If I take this out I get cannot find symbol – method addObject(Start,int,int) Yes the “s” was capitalized, I then tried making it a capital “s” and got cannot find symbol – Start I then simply put everything into the Background (preferred not to because I would like to simply call on ‘start’ and put up my main menu) and in my Levels:
public class Levels extends Buttons
{
    /**
     * Act - do whatever the Levels wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        click();

    }    

    public void click() 
    {

        if (Greenfoot.mouseClicked(this))
        {
            
            getWorld().removeObject(Controls.class);  

            getWorld().removeObject(Credits.class);  

            getWorld().removeObject(Settings.class);  

            Level1 level1 = new Level1();
            getWorld().addObject(level1, 100, 375);

            Ready ready = new Ready();
            getWorld().addObject(ready, 500, 375);

            Back back = new Back();
            getWorld().addObject(back, 100, 375);

            setLocation(175, 75);

        }    
    }
}
I got the error: in ‘getWorld().removeObject(Controls.class);’ method removeObject in class greenfoot.world cannot be applied to given types; required: greenfoot.Actor found: java.lang.Class<Controls> reason: actual argument java.lang.Class<Controls> cannot be converted to greenfoot.Actor by method invocation conversion then I took out ‘getWorld().’ And go the error cannot find symbol – method removeObject(java.lang.Class<Controls>) by the way I do have a lot of code in my other actors and know how to reference other actors(just not when they are in subclasses), but I have noted all of them from being created so as not to interfere. Please help me correct these bugs (or if you can’t maybe just tell me what I am doing wrong or how to reference things in sub-classes) Thank you, :)
danpost danpost

2012/7/24

#
All methods, including 'getWorld' must have a set of parenthesis '( )' after the method name. That is where any parameters would go, if any data was being passed to the method. An empty set just means that no extra data is needed to have the method do what it has to do. Line 10 should read getWorld().addObject(start, 300, 200); Lines 13, 16, 19, and 22 should all start the same way.
danpost danpost

2012/7/24

#
This is a somewhat common mistake: getWorld().removeObject(Controls.class); The method 'removeObject' is a World method that takes an Object (just one) as its parameter (not a class). The method 'removeObjects' is also a World method, but takes a List (or Collection) of Objects as its parameter. If you want to remove all instances of a class, you can use:
getWorld().removeObjects(getWorld().getObjects(Controls.class));
sparrow72 sparrow72

2012/7/24

#
Thanks! so now 'Start' is working, and 'Levels' also works with that line but how would I go about removing something if I only had one of it, or if i had 2 but only wanted 1 gone( 2 separate issues of mine).
danpost danpost

2012/7/24

#
If you only had one instance of an Actor sub-class, you could use either method. If you had multiple instances, and only wanted to remove one of them, you need to use 'removeObject' (without the 's'). You need to get a reference to the object to remove it. Sample code:
Start start = new Start();
getWorld().addObject(start, 300, 200);
Start start2 = new Start();
getWorld().addObject(start2, 200, 300);
// 'start' and 'start2' are references to Start class objects in the world
getWorld().removeObject(start2);
There are several ways to get a reference to a specific object to remove it, The 'Object' reference must be cast to 'Actor' or a sub-class of 'Actor'. There are various methods in the Actor class that either return an Actor object or a 'List' of objects; and some in the World class that return 'List's of object. For the ones in the Actor class that return just one Actor, you need not check for null to remove, but you may want to know if an Actor object was removed or not. For the ones that return a 'List', you will have to check that the 'List' is not empty, then somehow determine which one in the list you want to remove. If is does not matter, just use '.get(0)' on the list, cast to 'Actor', and remove it. If it matters, you will have to iterate through the list and find the one you want to remove by comparing unique characteristics of the object (maybe location, size, transparency, rotation, or even the value of a field of that type object (which requires casting to the specific sub-class of Actor); to name a few). It may even take a combination of characteristics to determine the correct one. Hope this helps!
sparrow72 sparrow72

2012/7/24

#
Thanks!!, I have been trying to figure this out for a while. (I have two tanks fighting and trying to detect bullets, but they always detect there own!) on that note how would I detect the 'Bullet' 'bulletBad'?
Bullet bulletBad = (Bullet) getOneIntersectingObject(BulletBad);  
because this line gives me the error message cannot find symbol - variable BulletBad I then changed the (BulletBad) to (bulletBad) continuing on to get errors, so how to I detect (by intersection) specific objects of one class? Thanks Again! XD
davmac davmac

2012/7/24

#
To specify a class as a parameter, you can use a class literal, which ends with ".class". So, in this case it should be:
Bullet bulletBad = (Bullet) getOneIntersectingObject(BulletBad.class);    
You need to login to post a reply.