This is my first time trying to import a whole new class like this and I'm not sure how to do it. Can you help me out? I've tried numerous things for over an hour and just can't quite get it. What I'm trying to do is stitch this together with my project as an overlay that gets called automatically, but it keeps competing with the world that I already have and really have no clue. Do I have to redo all my classes under this one so they are called correctly?
No, this is based on java packaging. This is basically like a folder system. Therefore, copy the whole „rccookie“ folder into your project, so that the structure is like
YourScenarioFolder
|-images
|-rccookie
|-MyWorld.java
|-MyWorld.class
|-project.greenfoot
|-More of your classes
Then you can import a class from the package like this:
// At the very top of the file just like the greenfoot import
import rccookie.ui.basic.Button;
This way you import the Button class. You can also write
import rccookie.ui.basic.*;
import rccookie.zu.advanced.*;
import rccookie.ui.util.*;
To import absolutely everything about the ui.
Actually the import greenfoot.*; statement works exactly the same, only that is does not reference a folder in your scenario‘s folder.
You can also take a look into the MyWorld source code in my demo to see how I did this.
I understand the folder packaging and the importing, but am having trouble actually getting it to instantiate on the screen. I'll go ahead and copy paste my usual code over to form a title image and hope you can help me from there. I have since deleted the original code I used to try and instantiate the fancy screen, but all I need are two buttons if you can help me out. The code I pasted is used to handle switching between rooms in the game I am making if that helps.
import greenfoot.*;
/**
* This class is used to initialize and control changing of worlds
*/
public class ControlWorld extends World
{
static Actor kara; // an actor to be passed from world to world
Portal portalLeft, portalRight, portalUp, portalDown;
Actor btnExit, btnStart;
/**
* This initial world constructor starts the control of changing worlds for the rooms.
*/
public ControlWorld()
{
super(800, 600, 1);
kara = new Kara();
String text = "\nAlex His APCSA Project";
GreenfootImage img = new GreenfootImage(text, 40, Color.BLACK, new Color(0, 0, 0, 0));
getBackground().drawImage(img, 400-img.getWidth()/2, 150);
}
public void act()
{
World world = new Room1();
world.addObject(kara, getWidth()/2, getHeight()/2);
Greenfoot.setWorld(world);
}
/**
* This continuation constructor is used to go from one world to the next.
*
* @param wide the width of the new world (in cells)
* @param high the height of the new world (in cells)
* @param cellsize the size of a single cell in the new world (in pixels)
*/
public ControlWorld(int wide, int high, int cellsize)
{
super(wide, high, cellsize);
}
}
Say you want to switch between worlds with a button. Then all you need is something like this in the constructor:
addObject(new TextButton(„Switch world“).addClickAction(m -> Greenfoot.setWorld(new SecondWorld())), 300, 350);
This will initialize and set a new world named SecondWorld
By the way, if you want your text to have the same design as the buttons, you can use the Text class. For example:
addObject(new Text("\nAlex His APCSA Project", 40), 400, 150);
would replace your title drawing in the constructor. Text is in rccookie.greenfoot.ui.basic;
Thank you! The project is sadly due today, so I cannot really explore too much more in your UI, but there is a very good chance I will come back to it later because I'm actually really proud of this one. It's my first knock-off lol.
2020/12/9
2020/12/9
2020/12/9
2020/12/9
2020/12/9
2020/12/9
2020/12/9
2020/12/10
2020/12/10
2024/5/27