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

2018/4/1

Changing worlds

1
2
3
4
Lavenger Lavenger

2018/4/5

#
danpost wrote...
Lavenger wrote...
when I remove the PIP, do I remove PIP as a whole or pip1, I plan to using the pip actor again after removing it
You can remove all PIP actors with removeObjects(getObjects(PIP.class)) or remove a specific one with removeObject(pip1). It is up to you. If you want to add the same PIP actor back into the world, you will need a field to maintain a reference to the object. Otherwise, you can just add new PIP actor into the world.
I'm having troubles trying to remove the PIP from the other world here's the class code for the actor the player is controlling:
import greenfoot.*;
public class MiniTaihei extends greenfoot.Actor 
{
    Timer actTimer = new Timer(40);
    private boolean once = false;
    private boolean run = false;
    public MiniTaihei()
    {
        getImage().scale(30, 30);
    }

    public void act()
    {
        int nx = 0, ny = 0, fx = 0, fy = 0;
        if (actTimer.iterating()) return;
        if(Wall.class == null)
        {
            if (Greenfoot.isKeyDown("a")&&(getOneObjectAtOffset(fx - 1, fy, Wall.class) == null))
            {fx--;}
            if (Greenfoot.isKeyDown("d")&&(getOneObjectAtOffset(fx + 1, fy, Wall.class) == null))
            {fx++;}
            if (Greenfoot.isKeyDown("w")&&(getOneObjectAtOffset(fx, fy - 1, Wall.class) == null))
            {fy--;}
            if (Greenfoot.isKeyDown("s")&&(getOneObjectAtOffset(fx, fy + 1, Wall.class) == null))
            {fy++;}
            if (fx*fy == 0 && fx+fy != 0) 
            {
                nx = fx;
                ny = fy;
                setLocation(getX()+nx, getY()+ny);
                actTimer.start();
            }
        }
        if (!getObjectsAt(this.getX(),this.getY() , MiniKirie.class).isEmpty())
        {
            getWorld().removeObjects(getObjects(Actor.class)); 
            getWorld().removeObjects(getObjects(PIP.class)); 
        }
    }   
}
Here's the code that has errors:
        if (!getObjectsAt(this.getX(),this.getY() , MiniKirie.class).isEmpty())
        {
            getWorld().removeObjects(getObjects(Actor.class)); 
            getWorld().removeObjects(getObjects(PIP.class)); 
        }
The error lies mainly in getObjects and getObjectsAt What I'm trying to do here is that when the actor <MiniTaihei> goes into the same cell as <MiniKirie>, the <Minigame1>world will be cleared of actors and the pip would be removed so that the player can continue playing the main game The question is: why was the code wrong, I'm not sure of what did I do wrong
danpost danpost

2018/4/5

#
There is no getObjectsAt method in the Actor class. You could use getObjectsAtOffset(0, 0, MiniKirie.class) or getIntersectingObject(MiniKirie.class) or getOneIntersectingObject(MiniKirie.class); but simplest is to use isTouching(MiniKirie.class). I doubt you want to remove all actors from your main world (see line 36). Remove that line.
Lavenger Lavenger

2018/4/5

#
danpost wrote...
There is no getObjectsAt method in the Actor class. You could use getObjectsAtOffset(0, 0, MiniKirie.class) or getIntersectingObject(MiniKirie.class) or getOneIntersectingObject(MiniKirie.class); but simplest is to use isTouching(MiniKirie.class). I doubt you want to remove all actors from your main world (see line 36). Remove that line.
if I cant use:
    getWorld().removeObjects(getObjects(Actor.class)); 
then how do I remove all actor in Minigame1?
danpost danpost

2018/4/5

#
Lavenger wrote...
if I cant use:
    getWorld().removeObjects(getObjects(Actor.class)); 
then how do I remove all actor in Minigame1?
Sorry, yes, you can use that line. It is the next line that is a problem. The actor only knows the "minor" world it is in. It knows nothing about the PIP object or the world the PIP object is in, the "major" world. The "major" world does know the PIP object, the PIP object knows the "minor" world and the "minor" world knows its actors. So what is needed is a way to indicate to either the PIP object or to the "major" world that the PIP object needs removed. Better is to inform the "major" world when to remove it so that the PIP support class is not modified. Please show your "major" world code so that it can be modified properly. Also, indicate what variables/fields hold the PIP object and the "minor" world in question.
Lavenger Lavenger

2018/4/8

#
This is the Major world code:
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    
    //For GIF background
    GifImage gifImg = new GifImage("Umaru.gif");
    
    //For Timing stuff
    private Actor TimeDisplay = new SimpleActor();
    private Actor Cooldown = new SimpleActor();
    private int frames;
    private int Cframes = (3+Greenfoot.getRandomNumber(5))*3300;
    
    //For Yen stuff
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    public int getHeight = 552 ;
    public int getWidth = 1200 ;
    
    //For PIP
    PIP pip1, pip2, pip3, pip4, pip5; // keeps a reference on 5 PIP object (Minigsme1, Minigame2, Minigame3, Minigame4, Minigame5 respectively)
    Actor mouseActor; // holds the actor being dragged
    int mouseOffX, mouseOffY; // holds the offsets of mouse location from center of object being dragged
    
    // class constructors usually (by convention) go next
    
    //To construct MyWorld
    public MyWorld()
    {
        //For building the world window size
        super(1400, 675, 1);
        
        //For updating the Yen Display
        adjustYen(0); // to initialize image
        prepare();
        addObject(YenDisplay, 1265, 50); // wherever
        
        //For updating the time display
        updateTimeDisplay();
        addObject(TimeDisplay, 1265, 20);
        
        //For triggering the minigame
        updateCooldownDisplay();
        addObject(Cooldown, 1265, 35);
    }
    
    //For GIF background
    public void gifAnimation()
    {
        for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
        setBackground(gifImg.getCurrentImage());
    }     
    
    // methods (by convention) go last
    
    //For executing all preccesses in MyWorld
    public void act()
    {
        
        //For GIF background
        gifAnimation();
        
        //For updating the Timing stuff
        if ((++frames)%55 == 0) updateTimeDisplay();
        if ((--Cframes)%55 == 0) updateCooldownDisplay();
        if (Cframes == 0)
        {
            Minigame();
            Cframes += ((3+Greenfoot.getRandomNumber(5))*3300); //chooses a cooldown time between 3-7 minutes randomly     
        }
        
        // control dragging of PIP objects
        if (Greenfoot.mousePressed(null) && mouseActor == null)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse.getActor() == null || mouse.getActor().getClass() != PIP.class) return;
            mouseActor = mouse.getActor();
            mouseOffX = mouseActor.getX()-mouse.getX();
            mouseOffY = mouseActor.getY()-mouse.getY();
            removeObject(mouseActor);
            addObject(mouseActor, mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseDragged(mouseActor))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            mouseActor.setLocation(mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseClicked(mouseActor)) mouseActor = null;
    }
    
    
    //For Yen Display stuff
    public int getAccumulatedYen()
    {
        return DYen;
    }
    public void adjustYen(float adjustment)
    {
        AYen += adjustment;
        DYen = (int) AYen;
        YenDisplay.setImage(new GreenfootImage("Yen: "+DYen+"¥", 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }

    //For Time Display stuff
    private void updateTimeDisplay()
    {
        int Time = frames/55; // total seconds
        int TotalMinutes = Time/60; // total minutes
        String hours = "0"+(TotalMinutes/60); // string total hours
        String minutes = "0"+(TotalMinutes%60); // string remaining minutes
        String seconds = "0"+(Time%60); // string remaining seconds
        String hrs = hours.substring(hours.length()-2); // hour output string
        String mins = minutes.substring(minutes.length()-2); // minutes output string
        String secs = seconds.substring(seconds.length()-2); // seconds output string
        String TimeFormat = "Time: "+hrs+"hrs "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
        TimeDisplay.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
        
    }
    
    //Minigame stuff
    
    //For timing the appearance of the Minigame
    private void updateCooldownDisplay()
    {
        int Time = Cframes/55;
        String minutes = "0"+(Time/60); // string remaining minutes
        String seconds = "0"+(Time%60); // string remaining seconds
        String mins = minutes.substring(minutes.length()-2); // minutes output string
        String secs = seconds.substring(seconds.length()-2); // seconds output string
        String TimeFormat = "Minigame countdown: "+mins+"mins "+secs+"secs"; //Preview: Time: 12345hrs 12mins 12secs
        Cooldown.setImage(new GreenfootImage(TimeFormat, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
    
    //For choosing the Minigame
    private void Minigame()
    {
        switch(Greenfoot.getRandomNumber(5))
        {
            case 1:if (!getObjects(Taihei.class).isEmpty())
                       {Minigame1();} //Chases Kirie because Umaru placed Taihei's money in Kirie's pocket in secret and plans to get it later on
                   break;

            case 2:if (!getObjects(Sylphynford.class).isEmpty())
                       //{Minigame2();} //Plays a 2 player minigame to beat Sylphynord's new score as a challenge
                   break;

            case 3:if (!getObjects(Ebina.class).isEmpty())
                       //{Minigame3();} //Introduces Umaru to a fashion modeling job
                   break;

            case 4:if (!getObjects(Kirie.class).isEmpty())
                       //{Minigame4();} //Animal crossing minigame
                   break;

            case 5:if (!getObjects(Taihei.class).isEmpty())
                       //{Minigame5();} //
                   break;
        }
    }
    //Calling out the Minigames
        private void Minigame1()
    {
        World minor = new Minigame1(); // create the minor world to be display inside of this world
        Class[] order = {}; // list the paint order of the minor world here
        pip1 = new PIP(minor, order); // create the pip object
        addObject(pip1, 110, 110); // add new PIP object into this world
    }
    /*
     * Uncomment this when youre done doing the minigames
        private void Minigame2()
    {
        World space = new Minigame2();
        Class[] order2 = {};
        pip2 = new PIP(space, order2);
        addObject(pip2, 690, 110);
    }
        private void Minigame3()
    {
        World space = new Minigame3();
        Class[] order2 = {};
        pip3 = new PIP(space, order2);
        addObject(pip3, 690, 110);
    }
        private void Minigame4()
    {
        World space = new Minigame4();
        Class[] order2 = {};
        pip4 = new PIP(space, order2);
        addObject(pip4, 690, 110);
    }
        private void Minigame5()
    {
        World space = new Minigame5();
        Class[] order2 = {};
        pip5 = new PIP(space, order2);
        addObject(pip5, 690, 110);
    }
    */
    //For placing actors into the world
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
    }
}
This is the Minor world code:
import greenfoot.*;
public class Minigame1 extends World
{
    public Minigame1()
    {
        super(20, 10, 30);
        // setBackground
        GreenfootImage bg = new GreenfootImage("sand2.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        StartGame();
    }
    public void StartGame()
    {
        int randX = 0, randY = 0;
        for (int i=0; i<=15;)
        {
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (1 + Greenfoot.getRandomNumber(19));
                randY = (Greenfoot.getRandomNumber(9));
            }
            addObject(new Wall(), randX, randY);
            i++;
        }
        
        // add actor to world
        addObject(new MiniTaihei(), 0, 9);
        addObject(new MiniKirie(), randX, randY);
    }
}
This is the class code of the actor that the player uses to move
import greenfoot.*;
public class MiniTaihei extends greenfoot.Actor 
{
    Timer actTimer = new Timer(40);
    private boolean once = false;
    private boolean run = false;
    public MiniTaihei()
    {
        getImage().scale(30, 30);
    }

    public void act()
    {
        int nx = 0, ny = 0, fx = 0, fy = 0;
        if (actTimer.iterating()) return;
        if(Wall.class == null)
        {
            if (Greenfoot.isKeyDown("a")&&(getOneObjectAtOffset(fx - 1, fy, Wall.class) == null))
            {fx--;}
            if (Greenfoot.isKeyDown("d")&&(getOneObjectAtOffset(fx + 1, fy, Wall.class) == null))
            {fx++;}
            if (Greenfoot.isKeyDown("w")&&(getOneObjectAtOffset(fx, fy - 1, Wall.class) == null))
            {fy--;}
            if (Greenfoot.isKeyDown("s")&&(getOneObjectAtOffset(fx, fy + 1, Wall.class) == null))
            {fy++;}
            if (fx*fy == 0 && fx+fy != 0) 
            {
                nx = fx;
                ny = fy;
                setLocation(getX()+nx, getY()+ny);
                actTimer.start();
            }
        }
    }   
}
And this is the class code of the actor the player is supposed to catch
import greenfoot.*;
public class MiniKirie extends Actor
{
    public void act() 
    {
        
    }    
}
danpost danpost

2018/4/8

#
I have two questions: (1) is there only to be one minigame running at any one time; and (2) does a repeat minigame start a new minigame of said type or is it a continuation of the previous minigame of said type.
Lavenger Lavenger

2018/4/8

#
danpost wrote...
I have two questions: (1) is there only to be one minigame running at any one time; and (2) does a repeat minigame start a new minigame of said type or is it a continuation of the previous minigame of said type.
1) there's only one minigame running of said type at one time 2) there's a code that chooses 1 new minigame out of 5 minigames under a counter, there will be no continuation. there is a possibility that the same minigame will appear again but it will start anew, not continued from where it ended before.
danpost danpost

2018/4/8

#
Lavenger wrote...
1) there's only one minigame running of said type at one time
That is not what I asked. I was basically asking if two different minigames could be running at the same time. Please rephrase your answer.
Lavenger Lavenger

2018/4/8

#
danpost wrote...
Lavenger wrote...
1) there's only one minigame running of said type at one time
That is not what I asked. I was basically asking if two different minigames could be running at the same time. Please rephrase your answer.
there will not be multiple minigames running at any given one time, only one would be active along with the main game
danpost danpost

2018/4/8

#
Okay, then you only need one reference to a PIP object -- call it pip. You will also need a reference to the minor world that the pip simulates -- call it minor. So, instead of line 23 in your MyWorld class, you would have: and replace all from line 136 to line 199 with this:
private void Minigame()
{
    if (pip != null && pip.getWorld() != null) return; // will not add new pip if one is already in world
    minor = null;
    switch (Greenfoot.getRandomNumber(5))
    {
        case 0: if (!getObjects(Taihei.class).isEmpty()) minor = new Minigame1(); break;
        case 1: if (!getObjects(Sylphynford.class).isEmpty()) minor = new Minigame2(); break;
        case 2: if (!getObjects(Ebina.class).isEmpty()) minor = new Minigame3(); break;
        case 3: if (!getObjects(Kirie.class).isEmpty()) minor = new Minigame4(); break;
        case 4: if (!getObjects(Taihei.class).isEmpty()) minor = new Minigame5(); break;
    }
    if (minor != null)
    {
        pip = new PIP(minor, new Class[] {});
        addObject(pip, 690, 110);
    }
}
Now, in your act method, you can add the following code to remove the PIP object when its minor is void of actors:
if (pip != null && pip.getWorld() == this && minor.getObjects(null).isEmpty()) removeObject(pip);
and you will not have to try to remove the PIP object from the world by code in the MiniTaihei class.
Lavenger Lavenger

2018/4/8

#
what does this line of code do?:
if (pip != null && pip.getWorld() == this && minor.getObjects(null).isEmpty()) removeObject(pip);
I'm a little confused because the code is too compact ^^" also,
danpost wrote...
Now, in your act method
which act method, the MyWorld or Minigame world?
danpost danpost

2018/4/8

#
Lavenger wrote...
what does this line of code do?:
if (pip != null && pip.getWorld() == this && minor.getObjects(null).isEmpty()) removeObject(pip);
I'm a little confused because the code is too compact ^^"
If pip is assigned a PIP object; and if that PIP object is in this world; and if there are there no actors in its minor world, then remove that PIP object from this world.
Lavenger Lavenger

2018/4/8

#
danpost wrote...
Lavenger wrote...
what does this line of code do?:
if (pip != null && pip.getWorld() == this && minor.getObjects(null).isEmpty()) removeObject(pip);
I'm a little confused because the code is too compact ^^"
If pip is assigned a PIP object; and if that PIP object is in this world; and if there are there no actors in its minor world, then remove that PIP object from this world.
oh its possible to add 3 && conditions... all along I thought it was only possible to add 2 && conditions
danpost danpost

2018/4/8

#
Lavenger wrote...
danpost wrote...
Now, in your act method
which act method, the MyWorld or Minigame world?
The MyWorld act method.
danpost danpost

2018/4/8

#
Lavenger wrote...
oh its possible to add 3 && conditions... all along I thought it was only possible to add 2 && conditions
You can combine as many as you want AND'ing (&&) and OR'ing (||) them in any arrangement.
There are more replies on the next page.
1
2
3
4