![]() ![]() To add a new class, create it as a subclass of the Actor class. They are mainly used to alter the look of the World, for example the size and background image. For example, the Actor classes can move and turn. There are a number of methods that can be applied to items in the different classes. In the image above, the WombatWorld is the sand background. ![]() In Greenfoot, the World class is the background. In the example above, the WombatWorld subclass is connected below the World class and the Wombat subclass is connected below the Actor class. There are two main classes in Greenfoot: ‘World’ and ‘Actor’. Greenfoot’s two main classes, ‘World’ and ‘Actor’ tLocation(hook.getX(),hook.In Greenfoot, the classes and subclasses are shown on the right-hand side of the window. If (Greenfoot.isKeyDown("down") & hook.height <</a> getWorld().getHeight()-getY()-5) //move hook down If (Greenfoot.isKeyDown("up") & hook.height > 5) //move hook up If (Greenfoot.isKeyDown("right")) //move boat and hook to the right side If (Greenfoot.isKeyDown("left")) //move boat, line and hook to the left side GetWorld().addObject(hook, getX(), getY()+10) //add the hook to the world
GetWorld().addObject(line,
I added the objects when the world was created, which placed the line at a Y offset from the boat, and the hook at the end of the line. I had previously drawn a line, well see 2pixels wide by 200 pixels. I was just being lazy and wanted the HookLine to inherit some methods from the Animal class without having to re-do the code. It is simply a black line in the middle the screen that move left or right with the boat and doesn't connect the boat to the hook or get bigger or smaller. The line is drawn, but is a static image (that does move around with the boat along the X axis) that doesn't do anything. I add it the world in the world subclass Ocean using this: Img.drawLine(boat.getX(), boat.getY(), hook.getX(), hook.getY()) GreenfootImage img = new GreenfootImage(2,100) Import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Here is the source for my HookLine class, the class that I want to use to draw a line from the boat to the hook. That way, you can clear and refill the image with black up to the current length of the line and relocate the hook at that time. Probably the easiest way to set it up is with a line image that is the length of the maximum line length and have the line location at exactly the same location as the boat plus a y-offset of half the maximum line length. No matter what you decide in the above, it would be best NOT to have act methods in either the line or the hook classes, but to call methods in those classes from the boat class, otherwise you may end up with some synchronization problems. It would probably be best to do that in the same place you control the length of the line. You would have a choice of having the boat also control the location of the hook or have the line control that. The variable length of the line can be controlled either by the boat or by the line itself (not talking image here). With the line as an actor, to keep it with the boat, the boat object will have to control its location. ![]()
0 Comments
Leave a Reply. |