![]() : Index: 0, Size: 0Īt (ArrayList.java:653)Īt (ArrayList.java:429)Īt Hero.act(Hero.java:23) public void act()Īctor 2 = (Actor)getWorld().getObjects(2.class). It works the first time but throws an error immediately after that: When an enemy comes into contact with the hero the hero class is supposed to remove the enemy class with the same X and Y. You can find the method in the javadoc for Actor. You can place the call to getWorld here (although you don’t need to – the world you’re being added to is also passed as a parameter). This method will be called when the actor is added to a world. There is help for what you want to do (one-time initialisation after the actor has been added to the world): There is a callback method called “addedToWorld(World world)”. Java uses object orientation, a programming paradigm that represents concepts as objects. That’s not an error, it is a normal possible state.) Greenfoot is a visual and interactive computer program that uses Java to build games, simulations and other graphical programs. (This indicates that it is not in any world. While an actor is not in a world, ‘getWorld()’ will return null. It hasn’t even been completely created yet, and certainly not been inserted into a world yet. While you are in the constructor, you are still in the middle of creating the actor. When you make a new actor, it is created first, and then (possibly) inserted into the world. You can put them into the world, and take them out again. Why is (Board) getWorld() returning null when placed in the constructor of an actor?Īctors can exist without being in the world. Last I checked if the return value of the getWorld() depends on if the call is at runtime (while playing) or it is the first time called from the constructor of myBoard – as the last “act” of pressing the compile-button īut – (Board) getWorld() – always returns null when placed in the constructor of the Ball independent of the time it’s asked. My second idea was to use the myBoard.score() method in different methods in the Ball class, making it a local instance variable of the Ball, and only updating this variable the first time, it’s used.įurther investigations showed me, that – (Board) getWorld() – is returning null, whenever placed in the constructor of the Ball class. In Greenfoot, arrays are a way to hold and access multiple variables, and assign different values to new instances each time the while loop executes and produces a. ![]() True or false Mark for Review (1) Points. A classis a defined object that is shown in the Greenfoot class display. In Greenfoot, the instance has a source code editor. A worldin Greenfoot is the area in which objects interact. But the further investigations showed me, that a reference to the World (Board) is null, whenever placed in the constructor of the Ball: World getWorld() () getRotation() World getClass() getXY() Correct Correct 12. My first idea, why this didn’t worked, was the order in which the objects are created, so perhaps the Board wasn’t created when the constructor of the Ball was run. I received a runtime error when using the call It did not work placing the call myBoard = (Board) getWorld() in the constructor of the Ball. In order to understand the differences between the methods used in episode JoC #27 and #28 I have tried to make a mix of the two ideas.įirst I tried to create an instance variable in the Ball class to contain the reference to the Board. The call score() consists of the following two lines:īoard myBoard = (Board) getWorld() // the (Board) is casting the type World to the type Board ![]() In episode JoC #28 is used a casting method to get access to the score of the Board via the method getWorld() in order to loosen the overall coupling of references. Tanks for a high quality teaching material 18km! Typically in Greenfoot, class A gets a reference to class B in one of two ways: Class A is passed the reference to B on creation. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |