Use the 'addedToWorld' method of the Actor class to place the code that repeatedly gets a new random location, sets itself there and checks for intersectors until none are found. () An object needs to move forward, then move up 10 meters. In other words - all your World subclass would be doing is adding the object into the world (it could be added at location (0, 0) for all that mattered). (Choose all correct answers) A single motion, such as walking, takes up a lot of room in myFirstMethod. It will allow you to practice Passing Parameters, Creating Custom Images, and creatin. It would be better to have the Obstacle object being added find its own place in the world where it is not intersecting another of its kind. This is the 3rd video in my Greenfoot Full Course. The check for intersection still needs to be done in the Obstacle class. Swapping the order of the two lines should solve that issue.Īfter further investigation into your code, it appears what you wanted to do was randomly add a number of Obstacle objects into the world so that none of them intersected each other. This will fail as you cannot execute a method on a 'null' reference. Line 29 removes the object from the world and then line 30 uses 'getWorld', which will now return a 'null' value, to add a new Obstacle object into the world. There is still one other thing of concern with that block of code. I believe the lines 23 through 30 are misplaced and should be in your Obstacle class. The method 'getOneIntersectingObject' has protected access and can only be called from within an Actor subclass. You cannot use the 'if' statement given at the end of the first post davmac wrote in your AnimalWorld class. I think the correct version of the code above is, therefore: 'if (obstacle.getOneIntersectingObject(Obstacle.class) = null)'. You need to either define a variable, and save the method return value in that variable, or put the method call directly inside the 'if' condition, i.e. You can't just use the method name as if it was a variable. Second, you call the method, but then separately try to use the value that it returned - 'if (getOneInterectingObject = null)'. In this case I think that 'obstacle' would probably do, i.e 'obstacle.getOneIntersectingObject(.)'. Because it's in the Actor class, and is not a static method, you need a reference to the one actor in order to call the method. I think it's the first one that you really want it checks whether one actor is interesting any other actors (and returns a reference to one of the intersecting actors). There are two methods with a similar name in the Actor class: 'getOneIntersectingObject' and 'getIntersectingObjects'. Greenfoot.getOneIntersectingObjects(Obstacle.class) įirst, 'getOneIntersectingObjects' is not a member of the Greenfoot class, so 'Greenfoot.getOneIntersectingObjects(.)' is not right.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |