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

2018/2/6

The game pauses when switching worlds

Arakniode Arakniode

2018/2/6

#
Hi! I have a MenuWorld with a Button object in it. When you click on the Button object, it calls a method in this Button to go to a new CinematicWorld (which will then send you into a PlanetWorld). This works almost fine, because it does create a new CinematicWorld and setWorld()s to it. However, the game is paused in this new CinematicWorld. What's the matter here? (I checked and there are no crashes, no console opening. When I relaunch the game manually, it works fine.) The startGame method in the Button class:
private void startGame(boolean started) {
        if (!started)
        {
            // set all the variables
            /** add this later if needed */
            // create a new CinematicWorld with a random color
            Color c = new Color(Greenfoot.getRandomNumber(55 + 125),
                                Greenfoot.getRandomNumber(55 + 125),
                                Greenfoot.getRandomNumber(55 + 125));
            CinematicWorld cWorld = new CinematicWorld(0, c);
            Greenfoot.setWorld(cWorld); // sends to an "arrive on planet" cinematic
            System.out.println("Hi");
        }
        else // if there is a save file
        {
            /** read from text file and stuff */ 
        }
    }
The CinematicWorld constructor:
 public CinematicWorld(int cinematicType, Color c) /** I might want to add a parameter for planet color here. Then call the PlanetWorld with
                                                 that color as a parameter, once the cinematic is finished */
    {
        // A. GENERAL IMAGE OF THE BACKGROUND:
        super(600, 600, 1);
        // initialize colors
        themeColor = c;
        themeColorD = c.darker();
        // a1. DRAW THE SIMPLE BACKGROUND:
        GreenfootImage background = new GreenfootImage(getWidth(), getHeight());
        background.setColor(Color.BLACK);
        background.fill();
        
        // a2. DRAW STARS:
        background.setColor(Color.WHITE); // color of the stars
        int R = 3; // radius of the stars
        // this "for" loop creates stars randomly
        for (int i = 0; i < 300; i++){
            background.fillOval(Greenfoot.getRandomNumber(getWidth()),
                                Greenfoot.getRandomNumber(getHeight()),
                                R,
                                R);
        }
        
        // a3. DRAW THE PLANET IN THE LOWER-LEFT CORNER:
        int pR = 1000; // radius of the planet
        GreenfootImage llPlanet = new GreenfootImage(pR, pR);
        llPlanet.setColor(themeColor);
        llPlanet.fillOval(0, 0, pR/2, pR/2);
        /** add code to draw other ovals on the planet, for craters */
        background.drawImage(llPlanet, getWidth()/2, getHeight()/2 - 150);
        
        // a4. FINALLY SET THE BACKGROUND:
        setBackground(background);
        
        
        // B. ASSIGNMENTS:
        cType = cinematicType;
        
        // C. LAUNCH THE CINEMATIC:
        cInit();
    }
Thank you very much for your help! :)
danpost danpost

2018/2/6

#
What codes, out of curiosity, are in the 'cInit' method?
Arakniode Arakniode

2018/2/6

#
danpost wrote...
What codes, out of curiosity, are in the 'cInit' method?
Posting that when I get home, but it's basically creating a rocket in the world, so that the cinematic can start
Arakniode Arakniode

2018/2/6

#
danpost wrote...
What codes, out of curiosity, are in the 'cInit' method?
Alright, here's the cInit method in the CinematicWorld class:
private void cInit(){ // place the objects and then call cMove()
        switch(cType){
            case 0: // GOING TO THE PLANET
                Rocket rocket = new Rocket(1);
                rocket.setRotation(120);
                addObject(rocket, getWidth()/3 * 1, getHeight()/3 * 1); // position adjustments here
        }
    }
danpost danpost

2018/2/6

#
Okay, how about the code of the Rocket class (since one is created during CinamaticWorld creation.).
Arakniode Arakniode

2018/2/6

#
danpost wrote...
Okay, how about the code of the Rocket class (since one is created during CinamaticWorld creation.).
Here's the entire rocket class (i used a parameter to determine if its a rocket in the cinematic, or in the planetWorld. I don't know if thats a good idea but thats not really the point)
import greenfoot.*; 

public class Rocket extends TileObject
{
    private int cType;
    public Rocket(int cinematic){ // the cinematic int is : 0 = non cinematic, 1(+) = cinematic
        tileX = 300; //
        tileY = 300; //this sets the initial position on the screen
        this.cType = cinematic;
    }
    public void act()
    {
        switch(cType){
            case 0: // in case of normal rocket
                setTilePos();
                
            case 1: // in case of cinematic
                cMove(cType);
        }
    }
    private void setTilePos(){
        if (getX() != tileX && getY() != tileY){
            setLocation(tileX, tileY);
        }
    }
    public void cMove(int type)
    {
        switch(type)
        {
            case 0:
                break;
            case 1:
                move(1);
        }
    }
}
danpost danpost

2018/2/6

#
Okay, in your first post you stated that no console opened; but, I see you have a 'System.out.println' line in the 'startGame' method. Are you saying that the string "Hi" is not being sent to the console? Another thing that might be of interest -- are you using any static content in the Button class (or elsewhere)?
danpost danpost

2018/2/6

#
Also, I noticed that the Rocket class extends a TileObject class. What code is in it (since it is a part of the Rocket class)?
Arakniode Arakniode

2018/2/6

#
danpost wrote...
Okay, in your first post you stated that no console opened; but, I see you have a 'System.out.println' line in the 'startGame' method. Are you saying that the string "Hi" is not being sent to the console? Another thing that might be of interest -- are you using any static content in the Button class (or elsewhere)?
The "Hi" is being sent to the console, indeed. Forgot about that. I meant there is no exception sent to the console. Also, I am not using any static fields in the Button class.
danpost wrote...
Also, I noticed that the Rocket class extends a TileObject class. What code is in it (since it is a part of the Rocket class)?
The TileObject class is for objects that align on a grid system of Tile objects. There are no tiles in the CinematicWorld. Here is the TileObject class:
import greenfoot.*;

public abstract class TileObject extends Actor
{
    protected int tileX;
    protected int tileY;
    public void act() 
    {
        getTile();
    }    
    protected Actor getTile(){
        Actor currentTile = getOneIntersectingObject(Tile.class);
        return currentTile;
    }
}
And the Tile class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tile extends Actor
{
private int x;
private int y;
public static int tileHeight = 50;
public static int tileWidth = 50;
    public Tile() 
    {
        // image
        GreenfootImage image = new GreenfootImage(tileWidth, tileHeight);
        // uncomment the next 2 lines for tile debugging
        //image.setColor(new Color(255, 0, 0, 128));
        //image.fill();
        setImage(image);
    }
    public Tile getTile(int x, int y){
        return this;
    }
}
danpost danpost

2018/2/6

#
What is the purpose of the act method with the call to 'getTile' in the TileObject class? Having and Actor object (or null) returned and not doing anything with it is like doing nothing. You can remove the act method from that class. The 'getTile' method in the Tile class is also quite pointless. To call the method, you would need a reference to the 'this' that is returned. The 'x' and 'y' parameters are not used in the method and therefore have no effect on the returned value (and never will if you absolutely return 'this').
danpost danpost

2018/2/6

#
As far as the pausing, nothing is apparent as to why you are getting that behavior. Maybe you can upload it with source provided so it can be tested out by others and/or myself. Will be out for a while ... sorry for any delays :(
Arakniode Arakniode

2018/2/8

#
danpost wrote...
What is the purpose of the act method with the call to 'getTile' in the TileObject class? Having and Actor object (or null) returned and not doing anything with it is like doing nothing. You can remove the act method from that class. The 'getTile' method in the Tile class is also quite pointless. To call the method, you would need a reference to the 'this' that is returned. The 'x' and 'y' parameters are not used in the method and therefore have no effect on the returned value (and never will if you absolutely return 'this').
Thanks for the answer! It's true that it's been about 3 months since I started working on this project, and about the same amount of time since I wrote the Tile class. It's true that I didn't have that much experience in Java back then. I'll try a few more things, and then post a first version of the game :)
You need to login to post a reply.