The dominance of @Joseinnewworld remains undefeated — he just added 20 more #NFTs to his collection 🔥🐋 A true force in the NFToa ecosystem. Grateful for the relentless support, legend 🙌 #eCash $XEC #NFT #NFTCollection #NFTmarketplace #nftartgallery #Nftarts #CryptoCommunity pic.twitter.com/FgYwdEQJpv
— NFToa (@nftoa_) September 9, 2025
Based on the following case study;
"Create a new scenario with a scenario name according to the provisions, by doing a save as.. from the previous scenario. Adjust it to your previous case!!"
How to Add Random Behavior, Supplemental Actors and Navigation Controls in Greenfoot
So what we need to do is adopt from the previous project/scenario, namely here:
A. Adding Random Behavior
1). Software tools have a method to create random numbers >> Greenfoot.getRandomNumber(int x), then open the tools editor in the Elephant class, add the following command block code:
If (Greenfoot.getRandomNumber(100) > 90)
{
turn (Greenfoot.getRandomNumber(90)-45);
}2). Place the code above in the main function act() and below the isAtEdge() method block, or see the example below:
Save the code, then compile it, then instantiate the Elephant object/class into the world!
3). Execute the scenario by pressing the RUN button, so the result will be like this:
Evaluation:
Question 1). Observe and explain the execution results of the scenario!
Answer 1). So with the addition of the random block method, the actor will rotate not only when it reaches the edge of the window, but also when a certain random number, the method applied in my scenario has a condition where random starts from 0-100 and if a number is obtained greater than 90, then the program in the block method is executed, so turn(Greenfoot.getRandomNumber()) is executed. However, in my scenario turn has a condition where the random turn starts from 0 - 90, then a turn of minus (-) 45 degrees means the actor turns left.
Question 2). Change the int x parameter in the Greenfoot.getRandomNumber(int x) method, observe and explain the function and changes of the method!
Answer 2). When I change the random method to 50 or if(Greenfoot.getRandomNumber(50) > 90), then what happens is that the program in the method block will never be executed, because the condition of the method will never be achieved because random is only done from the numbers 0-50, while the condition that must be met is that the number must be greater than 90, so what happens to the actor is that its movement is the same as before or before the addition of the random method.
B. Adding Supplement Actors
To perfect the scenario above, the thing we need to add to the habitat is food (Supplement Actor), so do as in the previous material (creating a new actor/new subclass) by:
- Right click Actor >> New subclass
- Choose the appropriate object for your case, in my case it is Elephant, then I will give Grass as a supplement.
- Instantiate some grass classes into World
- Execute the scenario using the RUN button, so the results will look like this:
Evaluation
Question 3). Observe and explain the execution results of the scenario!
Answer 3). When the elephant actor touches the grass actor, it just passes by and there is no reaction or change in the grass actor. This happens because the grass actor does not yet have a program definition or Act method.
Hehehe it seems like the Elephant is not hungry yet, so he doesn't eat the grass there, so what if we make the Elephant very hungry?! Well, keep following the next steps:
1. Right click on the Elephant actor >> Open Editor
2. Add the following statement code
if ( isTouching(grass.class))
{
removeTouching(grass.class);
}3. Place it under the move(5) method; so that it looks like this image;
4. Compile and run the Scenario. The result in this video;
Evaluation:
Question 4). Observe and explain the execution results of the results of adding the behavior above!
Answer 4). When the elephant actor touches the grass actor, what happens is that the grass actor disappears, this expresses the behavior of the elephant eating grass, this can be done by adding the if method. isTouching(grass.cass){ remove Touching(grass.cass);}
Question 5). Explain the additional function of the following statement code:
if ( isTouching(grass.class))
{
removeTouching(grass.class);
}Answer 5). The function or method if ( isTouching(grass.class)) { removeTouching(grass.class);} has the meaning that "If touching(parameter) then when touching, delete(parameter)" and coincidentally the parameter in my project scenario is grass, so "when the elephant touches the grass, the grass is deleted".
C. Main Actor Control
This time is the final stage of refinement in this project 2, where our scenario is an Elephant that we can control for the direction of its movement. We will try to use 2 navigations on the keyboard, namely the Left Direction and the Right Direction, by following these instructions:
1. Right click on the Elephant class >> Open Editor
2. Delete or deactivate the code block below;
3. Replace with the following code block:
4. Compile and Run the Scenario, then you will get results like this:
Case study:
Implement the method creation on the main actor behavior by creating it into a function sub-module, so that the act() method only contains the following code:
Okay, done, the complete project including the completion of the Case Study above can be downloaded here:
