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

2013/8/11

Access methods of actors not built into greenfoot from world??

Entity1037 Entity1037

2013/8/11

#
Alright, so basically I'm trying to change the value of multiple actors through world, but I'm having trouble: it won't let me call custom methods without using this prefix (I think): (()).shadowImagery(shadowImagery);
    //Shadow Imagery
        if (Greenfoot.isKeyDown("s")&&sdown==false){
            if (shadowImagery==false){shadowImagery=true;}else{shadowImagery=false;}
            sdown=true;
            updateShadowImagery();
        }else if (! Greenfoot.isKeyDown("s")){sdown=false;}
    }
    Class[] shadowers = {Ball.class};
    public void updateShadowImagery(){
        for (int i=0; i<shadowers.length; i++){
            List Shadowers = getObjects(shadowers[i]);
            if (! Shadowers.isEmpty()){
                for (int s=0; s<Shadowers.size(); s++){
                    Actor shadower = (Actor) Shadowers.get(s);
                    shadower.shadowImagery(shadowImagery);
                }
            }
        }
    }
If someone could help me, that would be great because I have no idea what do to about this
Gevater_Tod4711 Gevater_Tod4711

2013/8/11

#
I don't realy get whats the problem in this code. What exactly is not working and what do you want the code to do?
Zamoht Zamoht

2013/8/11

#
Let's say you want to create a puzzle game. Create a new class for world and call it PuzzleWorld for instance, then change the world you use to a subclass of our new world. Create a new Actor called PuzzleActor (again for a puzzle game). In the PuzzleActor write this:
    public PuzzleWorld getWorld()
    {
        return (PuzzleWorld)super.getWorld();
    }
Make the actors you want to call custom methods from the world a subclass of PuzzleActor. If this is done correctly you should be able to call getWorld().customMethod();
davmac davmac

2013/8/11

#
Entity1037, for your code above, am I understanding correctly that you want to have several different actor classes which all have a shadowImagery method? In that case you could have an abstract base class (let's call it Shadower) which declares the method, and then have your other classes extend that class. Then you just remove the loop in your code above and instead:
public void updateShadowImagery(){  
    List<Shadower> shadowers = getObjects(Shadower.class);  
    if (! shadowers.isEmpty()){  
        for (int s=0; s<shadowers.size(); s++){  
            Actor shadower = (Actor) shadowers.get(s);  
            shadower.shadowImagery(shadowImagery);  
        }  
    }  
}
Note I changed your list name from "Shadowers" to "shadowers" because variable names in Java should start with a lower-case letter. You can further improve on that using a for-each loop:
public void updateShadowImagery(){  
    List<Shadower> shadowers = getObjects(Shadower.class);  
    for (Shadower shadower : shadowers){  
        shadower.shadowImagery(shadowImagery);  
    }  
}
Entity1037 Entity1037

2013/8/15

#
OK. But, would the base class call the method in world, or would have the method itself? I have no idea how abstract classes work...
danpost danpost

2013/8/15

#
@Entity1037, you can refer to the Java tutorial page on Abstract Classes and Methods to see how they work and how to code them.
Entity1037 Entity1037

2013/8/15

#
Alright. I did exactly what he said, but it can't recognize the method updateShadowImagery when it does:
shadower.shadowImagery(shadowImagery);
I also can't really understand what I should use to achieve what I want to do (initiate the same method that resides in multiple actors). Could someone just spell it out or something, because I just can't really learn from the way the oracle website explains things (honestly, the structure of the tutorials are poor).
davmac davmac

2013/8/15

#
Your base class, Shadower, must declare the method. I'm guessing that you either didn't declare it at all, or otherwise, your declaration parameters don't match the parameter types you used in the method call.
davmac davmac

2013/8/15

#
And, for a brief explanation: You are already familiar with how base classes work, even if you don't realise it. The "Actor" class has an act() method (which does nothing). All your own Actor subclasses can implement an act() method which redefines the method from the base class. If you have a reference to an Actor, you can call act() and the redefined version will be called. The principle is the same here; you have a Shadower class which defines a method, and then you have subclasses which redefine the method to do whatever you want. The Shadower class doesn't have to be an abstract class, so don't worry about that if it's confusing you.
Entity1037 Entity1037

2013/8/15

#
Wow... now I get everything!!! NOW it all makes sense! Thank you!!! Also, I must admit that my Computer Science teacher at my school is really bad at teaching, so... this is how I normally learn things (using the internet frequently). So thank you for being awesome!
You need to login to post a reply.