Greenfoot addobject from other class1/7/2024 ![]() If(coin.getClass(). You really mean one of two things: (1) can an Actor object add a new object of a different type into the world or, (2) can the addObject method be used in an Actor subclass Answers: (1) yes, any Actor object of any type, new or otherwise, can be added into the world by any Actor object. There, we see that it has the following signature: return type method name parameters. GetWorld().addObject(havecoin, 300, 300) The addObject method is a method of the World class. private static boolean isCrankTurned=false Īctor havecoin = new Message("Its a penny") * the 'Act' or 'Run' button gets pressed in the environment. * Act - do whatever the Inspector wants to do. * Write a description of class Inspector here. Import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) other programmers how to use a method or a class. The Timer class would look something like this: City city new City() tWorld(city) city.removeObjects(city.getObjects(Hero2.class)) By the way, your 'getWorld' method call on line 6 still returns the world where the click occurred. We are also using class libraries from Greenfoot, a framework that allows us. The addObject method is a World class method that adds a new object to the. You would add an instance field to the Timer class to hold a PlayWorld object and add an argument to the Timer constructor to pass the PlayWorld object to it. You will refer to this documentation often. Let us say your world class is called 'PlayWorld' that the method is in. Because the timer is not added to the world, it needs to be 'given' the world object. Now, you want that timer object you created to, let us say, call a method in your world class that ends the game playing time and shows a final score screen. ![]() For example, let us say you had a Timer class and create a timer object that you do not place in the world (in fact, if you never add any Timer objects into the world, you should probably drop the 'extends Actor' part of the class declaration statement). ![]() Then you would not use 'getWorld()' but the reference given. Really, the only issue is the with the object not placed in the world, there is no reference to the world unless you pass the world object or any actor object that is in that world to the method in question. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |