This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mint_Greenie
Mint_Greenie wrote ...

2014/6/13

Very Confused! Pls Help!

1
2
Mint_Greenie Mint_Greenie

2014/6/13

#
Hi, I have a game which is called 'Infinity' the game allows the user to draw a character and then play with it. I have 2 worlds in one project, one being world, and the other being Sky. The first world that launches in the game is called 'world' I have managed to make the game open up another world, 'Sky' when the user hits the 'Play Game' button. I've managed to do this with the code best described below.
1
2
Sky gw = new Sky();
Greenfoot.setWorld(gw);
The only step I need now is to change the actors image which is called Bird, for your information 'Bird' doesn't already have a picture. I basically want to send a picture to Bird before I switch the world from 'world' to 'Sky'. So if you want a brief explanation of what I want, I want some code that will let me pass the image that the user had drawn in the 'world' class to the Bird actor in the 'Sky' class! Please help, I'm very confused and stumped. Thanks, Mint_Greenie
erdelf erdelf

2014/6/13

#
creating a static GreenfootImage in the Bird class should solve this, i think
Mint_Greenie Mint_Greenie

2014/6/13

#
How would I do this @erdelf? Any code examples?
danpost danpost

2014/6/13

#
I have answered this for you twice already (though, you did not previously supply any class or actor names. Even still, you did not supply the name of the field that holds the image drawn. In the following, I used 'image' for that:
1
2
3
Sky sky = new Sky();
((Actor)sky.getObjects(Bird.class).get(0)).setImage(image);
Greenfoot.setWorld(sky);
the part
1
(Actor)sky.getObjects(Bird.class).get(0);
gets the needed reference to the bird in the sky world.
Mint_Greenie Mint_Greenie

2014/6/13

#
@danpost: When I tried the code you supplied me with it informs me with a error. Saying greenfoot actors are private.
danpost danpost

2014/6/13

#
Please copy/paste the error message and the relevant code into a post.
Mint_Greenie Mint_Greenie

2014/6/13

#
@danpost: image has private access in greenfoot.Actor
danpost danpost

2014/6/13

#
Please copy/paste the relevant code (the class where you are allowing the drawing of the GreenfootImage object that you need to transfer to the Bird actor in the Sky world -- the 'world' class).
Mint_Greenie Mint_Greenie

2014/6/13

#
Options Class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.JOptionPane; import java.util.*; public class Options extends Actor { private String inputStr; private int buttonNbr; //private GreenfootSound ring; //private Label phoneMsg; String red; /** * Show a dialog when the user clicks this Actor. */ public void act() { if (Greenfoot.mousePressed(this)) { optionsDialog(); //displayDialog(); // dialogs are modal } } public void optionsDialog() { int result = JOptionPane.showConfirmDialog(null,"Press 'yes' to save, 'no' to make your own color", "Press 'yes' to save, 'no' to make your own color", JOptionPane.YES_NO_OPTION); if(result < 1) { fileDialog(); } else { pictureDialog(); } } public void pictureDialog() { red = JOptionPane.showInputDialog( "How much red?"); int red_int = Integer.parseInt(red); String green = JOptionPane.showInputDialog( "How much green?"); int green_int = Integer.parseInt(green); String blue = JOptionPane.showInputDialog( "How much blue?"); int blue_int = Integer.parseInt(blue); String alpha = JOptionPane.showInputDialog( "How much alpha?"); int alpha_int = Integer.parseInt(alpha); List<Constructor> c = getWorld().getObjects(Constructor.class); (c.get(0)).set(red_int, green_int, blue_int, alpha_int); } /** * Hold a dialog with the user. */ public void fileDialog() { Sky sky = new Sky(); ((Actor)sky.getObjects(Bird.class).get(0)).setImage(image); Greenfoot.setWorld(sky); } /** * Display the results of the dialog. */ public void displayDialog() { World w = getWorld(); /*if (phoneMsg != null) { w.removeObject(phoneMsg); }*/ String msg = inputStr + " phone home"; //phoneMsg = new Label(msg, 24); int x = w.getWidth() / 2; int y = w.getHeight() / 2 + 50; //w.addObject(phoneMsg, x, y); } } Bird Class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Bird extends Actor { /** The gravity to the ground. */ public static final int GRAVITY = 1; /** The horizontal speed that the bird flies (the pipes move backwards). */ public static final int SPEED = 10; /** The vertical speed when the bird flaps. */ public static final int FLAP_SPEED = -10; /** The current virtical speed. */ private int speed = 0; /** The score. */ private int score = 0; /** * Creates a new bird. * */ public Bird() { speed = 0; score = 0; } /** * Things to do for each turn. * */ public void act() { // Checks if the bird flaps. checkFlap(); // Moves the bird. move(); // Checks if the bird hits the obstacles. if (checkHit()) { Sky sky = (Sky) getWorld(); sky.gameOver(score); } else { // Checks if the bird gets ths score. checkScore(); } } /** * Checks if the bird flaps. * */ private void checkFlap() { if (Greenfoot.mouseClicked(null)) { speed = FLAP_SPEED; } } /** * Moves the bird. * */ private void move() { speed = speed + GRAVITY; setLocation(getX(), getY() + speed); } /** * Checks if the bird hits the obstacles. * * @return true if hit, false otherwise */ private boolean checkHit() { if (isTouching(Pipe.class)) { return true; } if (getY() == getWorld().getHeight() - 1) { return true; } return false; } /** * Checks if the bird gets ths score. * */ private void checkScore() { if (isTouching(ScoreLine.class)) { removeTouching(ScoreLine.class); score++; Sky sky = (Sky) getWorld(); sky.setScore(score); } } }
danpost danpost

2014/6/13

#
You are confusing me! You said you had two worlds: one called 'world' and another called 'Sky'; and you wanted to pass an image from one world to the other. So, I asked for the code to your 'world' class and you give me the above :/ where is the 'world' class code???
Mint_Greenie Mint_Greenie

2014/6/13

#
Sorry danpost! world: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class world here. * * @author (your name) * @version (a version number or a date) */ public class world extends World { int width2 = 10; int height2 = 10; /** * Constructor for objects of class world. * */ public world() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Constructor(), 300, 200); addObject(new Info(), 100, 300); addObject(new Options(), 100, 100); addObject(new Scaler(), 400, 100); addObject(new Instructions(), 236,335); addObject(new border(), 259, 217); } } sky: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Sky extends World { /** The spacing between the pipe pair.*/ public static final int PIPES_SPACING = 150; /** The minimal height of a pipe. */ public static final int PIPE_MIN_HEIGHT = 25; /** The interval between two pipe pairs. */ private final int PIPE_INTERVAL = 30; /** The timer to the next pipe pair. */ private int pipeTimer = 0; /** The scoreBoard. */ private ScoreBoard scoreBoard = null; /** * Constructor for objects of class Sky. * */ public Sky() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Bird(), getWidth() / 2, getHeight() / 2); scoreBoard = new ScoreBoard(); addObject(scoreBoard, 70, 50); pipeTimer = PIPE_INTERVAL * 2; setPaintOrder(GameOver.class, ScoreBoard.class, Bird.class, Pipe.class); } /** * Things to do for each turn. * */ public void act() { // Adds a pipe pair periodically. addPipePairPeriodically(); } /** * Sets the score. * * @param score the score */ public void setScore(int score) { scoreBoard.setScore(score); } /** * Game over. * * @param score the score */ public void gameOver(int score) { addObject(new GameOver(score), getWidth() / 2, getHeight() / 2); Greenfoot.stop(); } /** * Adds a pipe pair periodically. * */ private void addPipePairPeriodically() { pipeTimer--; if (pipeTimer == 0) { addPipePair(); pipeTimer = PIPE_INTERVAL; } } /** * Adds a pipe pair. * */ private void addPipePair() { int pipeMaxHeight = getHeight() - PIPES_SPACING - PIPE_MIN_HEIGHT; int height1 = PIPE_MIN_HEIGHT + Greenfoot.getRandomNumber(pipeMaxHeight - PIPE_MIN_HEIGHT); int y1 = height1 / 2 - 1; int height2 = getHeight() - height1 - PIPES_SPACING; int y2 = getHeight() - height2 / 2 - 1; int y3 = height1 + PIPES_SPACING / 2 - 1; addObject(new Pipe(height1), getWidth() - 1, y1); addObject(new Pipe(height2), getWidth() - 1, y2); addObject(new ScoreLine(), getWidth() - Pipe.WIDTH / 2, y3); } }
danpost danpost

2014/6/13

#
Ok, it looks like you have a Constructor object that has the data needed. Please show the code for that class (or at least the 'set' method in that class).
Mint_Greenie Mint_Greenie

2014/6/14

#
@danpost: The constructor is here! import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; //import java.awt.Font; import javax.swing.JOptionPane; /*import java.util.Calendar; import java.util.List; import java.util.Date;*/ import java.io.*; import java.text.SimpleDateFormat; import javax.imageio.ImageIO; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.*; /** * Write a description of class Constructor here. * * @author (your name) * @version (a version number or a date) */ public class Constructor extends Actor { int width; String input = "m"; int height; int circlew = 10; int circleh = 10; int x1; int y1; int line = 0; int pastX = 0; int pastY = 0; boolean first = true; boolean scaled = false; int dif = 200; static Scanner sc = new Scanner(System.in); public Constructor() { width = getImage().getWidth(); height = getImage().getHeight(); } /** * Act - do whatever the Constructor wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //line = Tool.getTool(); change();//get key input if(line < 1) point(); else if(line < 2) line(); else if(line < 3) rect();//what to draw else if(line < 4) Text(); else if(line < 5) Erase(); } public void Erase() { if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); int newX = mouse.getX(); int newY = mouse.getY(); clearRect(newX, newY,circlew,circleh); //g.clearRect(newX,newY, circlew, circleh); } if(Greenfoot.mouseDragged(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); int newX = mouse.getX(); int newY = mouse.getY(); clearRect(newX, newY,circlew,circleh); //g.clearRect(newX,newY, circlew, circleh); } } public void clearRect(int x, int y, int width, int height) { for(int t = 0; t < width; t++) { for(int u = 0; u < height; u++) { getImage().setColorAt(x+t, y+u,new Color(255, 255, 255, 0)); } } } public void set(int red, int green, int blue, int alpha) { getImage().setColor(new Color(red, green, blue, alpha)); //g.setColor(new Color(red, green, blue, alpha)); } public void rect() { if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); int newX = (int) Math.round(mouse.getX()/circlew)*circlew; int newY = (int) Math.round(mouse.getY()/circleh)*circleh; getImage().fillRect(newX, newY,circlew,circleh); //g.fillRect(newX,newY, circlew, circleh); } } public void setSize(int x, int y) { //g.scale(x, y); /*int c = 600/x; int d = 400/y; BufferedImage bi2 = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi2.getGraphics(); Color color_blocks = new Color; for(int g = 0;g < x; g++){ for(int h = 0; h < y; h++){ g2.setColor(getImage().getColorAt(g*c,h*d)); g2.fillRect(g,h,1,1); } }*/ width = x; height = y; getImage().scale(x,y); } public void Text() { if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); int newX = mouse.getX(); int newY = mouse.getY(); String post = JOptionPane.showInputDialog("What would you like to write?"); /*int FONT_SIZE = Integer.parseInt(JOptionPane.showInputDialog("What font would you like")); Font font = getImage().getFont(); font = font.deriveFont(100); getImage().setFont(font);*/ getImage().drawString(post, newX, newY); //g.drawString(post, newX+dif, newY+100); } } public void line() { if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if(first == true) { x1 = mouse.getX(); y1 = mouse.getY(); first = false; } else { getImage().drawLine(x1, y1, mouse.getX(), mouse.getY()); //g.drawLine(x1, y1, mouse.getX(), mouse.getY()); first = true; } } } public void point() { if(Greenfoot.mouseClicked(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); getImage().fillOval(mouse.getX()-circlew/2, mouse.getY()-circleh/2,circlew,circleh); //g.fillOval(mouse.getX()-circlew/2,mouse.getY()-circleh/2, circlew, circleh); } if(Greenfoot.mouseDragged(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); getImage().fillOval(mouse.getX()-circlew/2, mouse.getY()-circleh/2,circlew,circleh); if(pastX > mouse.getX()-10&&pastX<mouse.getX()+10) for(int i=0;i<Math.abs(pastX -mouse.getX());i++) { getImage().fillOval(pastX-circlew/2+(i*(mouse.getX()-pastX))/Math.abs(mouse.getX()-pastX), pastY-circleh/2+(i*(mouse.getY()-pastY))/Math.abs(mouse.getX()-pastX),circlew,circleh); } pastX = mouse.getX(); pastY = mouse.getY(); //g.fillOval(mouse.getX()-circlew/2,mouse.getY()-circleh/2, circlew, circleh); } } public void change() { if(Greenfoot.isKeyDown("up")){ Greenfoot.setSpeed(40); circleh++; ((world) getWorld()).width2++; } else if(Greenfoot.isKeyDown("down")){ Greenfoot.setSpeed(40); circleh--; ((world) getWorld()).width2--; } else if(Greenfoot.isKeyDown("right")){ Greenfoot.setSpeed(40); circlew++; ((world) getWorld()).height2++; } else if(Greenfoot.isKeyDown("left")){ Greenfoot.setSpeed(40); circlew--; ((world) getWorld()).height2--; } else { Greenfoot.setSpeed(100); } if(Greenfoot.isKeyDown("1")) { line = 1; } if(Greenfoot.isKeyDown("2")) { line = 2; } if(Greenfoot.isKeyDown("0")) { line = 0; } if(Greenfoot.isKeyDown("3")) { line = 3; } if(Greenfoot.isKeyDown("4")) { line = 4; } if(Greenfoot.isKeyDown("r")){ getImage().setColor(new Color(255,0,0,255)); //g.setColor(new Color(255,0,0,255)); } if(Greenfoot.isKeyDown("g")){ getImage().setColor(new Color(0,255,0,255)); //g.setColor(new Color(0,255,0,255)); } if(Greenfoot.isKeyDown("o")){ getImage().setColor(Color.orange); //g.setColor(Color.orange); } if(Greenfoot.isKeyDown("m")){ getImage().setColor(Color.magenta); //g.setColor(Color.magenta); } if(Greenfoot.isKeyDown("b")){ getImage().setColor(new Color(0,0,255,255)); //g.setColor(new Color(0,0,255,255)); } if(Greenfoot.isKeyDown("w")){ getImage().setColor(new Color(255,255,255,255)); //g.setColor(new Color(255,255,255,255)); } if(Greenfoot.isKeyDown("c")){ getImage().setColor(Color.cyan); //g.setColor(Color.cyan); } if(Greenfoot.isKeyDown("p")){ getImage().setColor(Color.pink); //g.setColor(Color.pink); } } public void saveFile(String cool) { BufferedImage bi = new BufferedImage(getImage().getWidth(), getImage().getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics g = bi.getGraphics(); for(int w = 0; w < width+100; w++) { for(int h = 0; h < height+100; h++) { try{ if(getImage().getColorAt(w, h) != Color.white) { g.setColor(getImage().getColorAt(w,h)); g.fillRect(w,h,1,1); } } catch(IndexOutOfBoundsException e) {} } } try { ImageIO.write(bi, "png", new File(cool)); } catch (IOException exception) { JOptionPane.showConfirmDialog(null,"Didn't save", "Didn't save", JOptionPane.YES_NO_OPTION); } } }
danpost danpost

2014/6/14

#
Then, line two should get a reference to the image set to the Constructor object:
1
2
3
4
Sky sky = new Sky();
GreenfootImage image = ((Constructor)getObjects(Constructor.class).get(0)).getImage();
((Actor)sky.getObjects(Bird.class).get(0)).setImage(image);
Greenfoot.setWorld(sky);
As written, this code should be in your 'world' class. As I do not know what mechanism you are using to trigger the changing of worlds (whether it be automatic after all input in received or a button is pressed; or what), I cannot at this point help out on this part. I am stating this because I noticed you have a method, 'fileDialog' in your Options class (a subclass of Actor) that has the code I gave above (which was intended to be in a subclass of World. The name of the method does not reflect what the method does and the code will have to be modified for it to work in a subclass of Actor (add 'getWorld.' in front of 'getObjects').
Mint_Greenie Mint_Greenie

2014/6/14

#
@danpost: Thanks a lot for your help! Just one last question. Is there any way I can execute the code from the 'world' class from the 'options' class? Options Class Code:
1
2
3
4
5
6
//The file dialog from when the user hits the save button in the options menu!  
public void fileDialog()
    {
      World world = new World();
      world.playGame();
    }
World Class Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The world class where the user draws the       *image.
 *
 * @author Mint_Greenie
 * @version 1.0.1
 */
public class world extends World
{
    int width2 = 10;
    int height2 = 10;
    /**
     * Constructor for objects of class world.
     *
     */
    public world()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(new Constructor(), 300, 200);
        addObject(new Info(), 100, 300);
        addObject(new Options(), 100, 100);
        addObject(new Scaler(), 400, 100);
         addObject(new Instructions(), 236,335);
          addObject(new border(), 259, 217);
    }
    public playGame(){
        Sky sky = new Sky(); 
        GreenfootImage image = ((Constructor)getObjects(Constructor.class).get(0)).getImage(); 
        ((Actor)sky.getObjects(Bird.class).get(0)).setImage(image); 
        Greenfoot.setWorld(sky); 
}
}
There are more replies on the next page.
1
2