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

2016/8/18

code for pause world

1
2
loxtn10 loxtn10

2016/8/18

#
I have a game that features a shop, I would like to pause the whole world while I'm interacting with the other surface, the shop. The idea was when the player is touching the shop and press "x" the shop would pop up and the whole world pauses but doesn't delete the progress, In short it just pops up over the world so it would still leave the game paused. When the shop pops up I can still sneak a pic of the back which is the progress of my game.I can't seem to come up a code that pauses a game and opens another interface just to buy and apply the changes on the other world. I need help thanks a lot !
danpost danpost

2016/8/18

#
You can open a new world for the shop and have it hold on to the current world so that it can be returned to. You can see code similar to that in my Crayon Babies: A World Changing Demo scenario.
loxtn10 loxtn10

2016/8/19

#
my code is here THE ACTOR CODE:
if((isTouching(meh.class) || isTouching(meh2.class) ) && ("x".equals (Greenfoot.getKey())))
        {
            Greenfoot.getKey();
            Greenfoot.setWorld(new srfc(inWorld));//THIS IS WHERE I WANT TO PASS THE inWorld VARIABLE
            
        }
I need to get the variable inWorld from the World Scene which is here THE WORLD CODE :
public Scene(World inWorld)//THIS IS THE inWorld VARIABLE
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(2500,300, 1); 
        addObject(new drop(),750,220);
        addObject(new drop(),300,220);
        addObject(new drop(),1050,220);
        addObject(new drop(),1,220);
        addObject(new shopp(),229,120);
        addObject(new shop(),229,125);
        addObject(new sign(),445,217);
        addObject(new meh(), 500, 220);        
        addObject(new counter(), 155,37);       
        addObject(new count(), 90,37);
        meh.ammo = 40;
        shield.shield = 0;
    }
I have no idea how to pass variable inWorld from World to an actor to pass it to the new World to save my progress. please help
danpost danpost

2016/8/19

#
loxtn10 wrote...
I have no idea how to pass variable inWorld from World to an actor to pass it to the new World to save my progress. please help
The Scene class appears to be for your game-play world, which is what will be passed to the shop world. Therefore, line 1 should be:
public Scene()
I will presume that the srfc class is for your shop world. Its field to hold the 'inWorld' and its constructor should look like this:
// game-play world field (in srfc class)
private World gameWorld;

// srfc class constructor
public srfc(World inWorld)
{
    super(< parameters >);
    gameWorld = inWorld;
    // etc.
}
and to return from the shop, you would use:
Greenfoot.setWorld(gameWorld);
If this is to be done from an actor class, you will need a method to get the game world in the srfc class:
public World getGameWorld()
{
    return gameWorld;
}
and then use:
srfc shop = (srfc) getWorld();
Greenfoot.setWorld(shop.getGameWorld());
Now, to set a shop world active from an actor class, you can use:
Greenfoot.setWorld(new srfc(getWorld());
loxtn10 loxtn10

2016/8/19

#
I have another problem
public void updateImage()
    {
        GreenfootImage buy = (new GreenfootImage ("" + ammo,30,Color.WHITE,Color.BLACK));//THE WORD AMMO HERE HAS UNDERLINE
    }  
The word ammo had red underline, this ammo is declared on another world called srfc and I want the Class namely shopammo to access the ammo from the world srfc, How am I gonna do it ? Another:
private void checkShop()
    {
        if(shopp.show == 1){Greenfoot.setWorld(new srfc());//THIS CODE DOESN'T WORK}
    }
the code doesn't work, how am I gonna set world to srfc? this code is from a World for it to setWorld a World namely srfc.
danpost danpost

2016/8/19

#
This is getting to be quite complicated and confusing. You are really not given enough information to make it clear what class is which, what each is supposed to be used for, which objects should be permanently retained throughout a single running of the project, what class problem fields are located in, etc. I am also getting the impression that you have a number of classes that you should not even have and fields that are not necessary as well. Also, it is apparent that you are not following (at least not completely) what I have already suggested (the last code given does not jive with what you just posted).
loxtn10 loxtn10

2016/8/21

#
Here is the code, I have one error. only ONE error I changed the srfc name into srrfc
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Scene here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scene extends World
{

    /**
     * Constructor for objects of class Scene.
     * 
     */   
    int spawn3 = 300;
    int spawn = 300;
    int spawn1 = 260;        
    int spawn4 = 1300;
    int enemy2 = 550;
    int enemyy = 550;  
    public Scene()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(2500,300, 1); 
        addObject(new drop(),750,220);
        addObject(new drop(),300,220);
        addObject(new drop(),1050,220);
        addObject(new drop(),1,220);
        addObject(new shopp(),229,120);
        addObject(new shop(),229,125);
        addObject(new sign(),445,217);
        addObject(new meh(), 500, 220);        
        addObject(new counter(), 155,37);       
        addObject(new count(), 90,37);
        meh.ammo = 40;
        shield.shield = 0;
    }
    boolean offNA =getObjects(NA.class).isEmpty();
    int NAtime = 0;
    public void act()
    {
        spawn_things();
        noteAmmo();
        checkShop();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void spawn_things()
    {
        //SPAWN ENEMY
        if (spawn3 > Greenfoot.getRandomNumber(20)+100){
            addObject(new wew2(), 1,220);
            spawn3 = 0;
        }
        spawn3++;
        if (spawn > Greenfoot.getRandomNumber(20)+100){
            addObject(new wew(), 2500, 220);
            spawn = 0;
        }
        spawn ++;
        //SPAWN AMMO
        if (spawn1 > Greenfoot.getRandomNumber(10)+ 250){          
            addObject(new ammo(), Greenfoot.getRandomNumber(1450)+589,220);
            spawn1 = 0;
        }   
        spawn1 ++;
        //SPAWN SHIELD
        if (spawn4 > Greenfoot.getRandomNumber(1)+1300){          
            addObject(new shield(), Greenfoot.getRandomNumber(1450)+589,220);
            spawn4 = 0;
        }   
        spawn4 ++;
       //SPAWN MINI BOSS
        if (enemy2 > Greenfoot.getRandomNumber(1)+1000){
               addObject(new enemy2(),2500,203);
               enemy2 = 0;
           }
           enemy2 ++;
        if (enemyy > Greenfoot.getRandomNumber(1)+1000){
               addObject(new enemy(),1,203);
               enemyy = 0;
           }
        
        enemyy ++;
        
    }
    private void noteAmmo()
    {
        //TIMER FOR NOT ENOUGH AMMO DISPLAY
        if (offNA)
        { 
            NAtime++;
            if (NAtime == 30)
            { 
                removeObjects(getObjects(NA.class));
                NAtime = 0;
            }
        }
    }
    private void checkShop() //THIS IS THE IMPORTANT LINE FOR THIS DISCUSSION
    {                                                                                         // down here
        if(shopp.show == 1)Greenfoot.setWorld(new srrfc());  //the red underline is in the "new srrfc())"
    }
}

loxtn10 loxtn10

2016/8/21

#
CODE of the srrfc world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class srfc here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class srrfc extends World
{

    /**
     * Constructor for objects of class srfc.
     * 
     */
    private World gameWorld;
    public static int ammo = 0;
    public srrfc(World inWorld)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 300, 1);                 
        addObject(new counter(), 155,37);       
        addObject(new count(), 90,37);
        addObject(new shopammo(),1,1);
        gameWorld = inWorld;
    }
    public void act()
    {
        int coinn = coin.coin; 
        int ammoo = meh.ammo;
        boolean buy = coinn-ammo >= 0;
        
        if (Greenfoot.getKey().equals ("up"))
        {
            Greenfoot.getKey();
            ammo ++;
        }
        else if (Greenfoot.getKey().equals ("down"))
        {
            Greenfoot.getKey();
            ammo --;
        }
        else if (Greenfoot.getKey().equals ("x"))
        {
            Greenfoot.getKey();
            if (buy)
            {
                coinn -= ammo;
                ammoo += ammo;
                ammo = 0;
                Greenfoot.setWorld(gameWorld);
            }
            else{Greenfoot.setWorld(gameWorld);}
        }
        
    }
}


    
loxtn10 loxtn10

2016/8/21

#
first code up there is for the Scene world, the world where the game is being played,the second code is the shop the srrfc world. the only error i get is when I attempt to set the world to srrfc from the scene world, I don't know what to do. what I already did is this.
danpost wrote...
The Scene class appears to be for your game-play world, which is what will be passed to the shop world. Therefore, line 1 should be: ? 1 public Scene() I will presume that the srfc class is for your shop world. Its field to hold the 'inWorld' and its constructor should look like this: ? 1 2 3 4 5 6 7 8 9 10 // game-play world field (in srfc class) private World gameWorld; // srfc class constructor public srfc(World inWorld) { super(< parameters >); gameWorld = inWorld; // etc. } and to return from the shop, you would use: ? 1 Greenfoot.setWorld(gameWorld);
I didn't do this since this is only for the actors, and what I use is world class for this
danpost wrote...
If this is to be done from an actor class, you will need a method to get the game world in the srfc class: ? 1 2 3 4 public World getGameWorld() { return gameWorld; } and then use: ? 1 2 srfc shop = (srfc) getWorld(); Greenfoot.setWorld(shop.getGameWorld()); Now, to set a shop world active from an actor class, you can use: ? 1 Greenfoot.setWorld(new srfc(getWorld());
loxtn10 loxtn10

2016/8/21

#
instead of this
loxtn10 wrote...
I didn't do this since this is only for the actors, and what I use is world class for this
I should say this: I didn't do this since this is only for the actors, and the class I used for setWorld is of World class
loxtn10 loxtn10

2016/8/21

#
still, this image
danpost danpost

2016/8/21

#
loxtn10 wrote...
instead of this
loxtn10 wrote...
I didn't do this since this is only for the actors, and what I use is world class for this
I should say this: I didn't do this since this is only for the actors, and the class I used for setWorld is of World class
Okay. Then, do this:
// change this
new srrfc()
// to this
new srrfc(this)
(where 'this' refers to the currently active Scene world object).
loxtn10 loxtn10

2016/8/21

#
finally ! THANKS A LOT AFTER DAYS I got this problem. Solved it... BTW what's the syntax for this ?just for knowledge and certainty of the idea. I'm not yet used to it, a prof gave project, but didn't teach any for this, self study's kinda hard though.. Another request: Could you check out the game I will be uploading, spent so many time looking for bugs couldn't fix it, Known bugs are: miniboss doesn't die. when player's direction is left the enemy from left tocuhes it, the player doesn't die. The game
loxtn10 loxtn10

2016/8/21

#
Another problem: Nullpointerexception when i click x. the current code for when x is clicked and near the shop the show becomes 1;
 public void act() 
    {
        getImage().setTransparency(0);
        show = 0;
        if((isTouching(meh.class) || isTouching(meh2.class) ) && ("x".equals (Greenfoot.getKey())))
        {
            Greenfoot.getKey();
            show = 1;
        }
        
    } 
after the show gets 1 value this code then happens
private void checkShop()
    {
        if(shopp.show == 1)
        {
          Greenfoot.setWorld(new srrfc(this));
        }
    }
then proceeds to thes after opening the shop. RIGHT BEFORE THE SHOP OPENS nullpointerexception happens in this code. I've already cleared the Greenfoot.getKey() which means no more registered key. but nullpointerexception happens
private World gameWorld;
    public static int ammo = 0;
    public srrfc(World inWorld)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 300, 1);                 
        addObject(new counter(), 155,37);       
        addObject(new count(), 90,37);        
        addObject(new shopammo(),1,1);
        gameWorld = inWorld;
    }
    public void act()
    {
        int coinn = coin.coin; 
        int ammoo = meh.ammo;
        boolean buy = coinn-ammo >= 0;             
        addObject(new shopammo(),50,50);
        if (Greenfoot.getKey().equals ("up") || Greenfoot.isKeyDown("up")) //NULLPOINTEREXCEPTION happens here.  <<<----------------------------
        {
            Greenfoot.getKey();
            ammo ++;
        }
        else if (Greenfoot.getKey().equals ("down") || Greenfoot.isKeyDown("down"))//SOMETIMES HAPPENS HERE TOO <<<------------------------------
        {
            Greenfoot.getKey();
            ammo --;
        }
        else if (Greenfoot.getKey().equals ("x") || Greenfoot.isKeyDown("x"))
        {
            Greenfoot.getKey();
            if (buy)
            {
                coinn -= ammo;
                ammoo += ammo;
                ammo = 0;
                Greenfoot.setWorld(gameWorld);
            }
            else{Greenfoot.setWorld(gameWorld);}
        }
        
    }
danpost danpost

2016/8/21

#
I will be out of touch for a number of hours (probably about 8 or so). I will look into your issues then. For now, however, I can say (after a quick look) that you have complicated things by having a separate class for left facing and right facing actor and subclassing 'coin' with 'count' (a 'count' object is not a 'coin' object); also, you are using the 'static' modifier on fields that do no belong to the classes they are in, but to objects of their class. Although you have things working (to some extent), these "issues", frankly, are not considered "good programming techniques" and could cause real issues while expanding on the project. Anyway, until later, maybe you can work on some of this.
There are more replies on the next page.
1
2