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

2018/4/1

Changing worlds

1
2
3
4
danpost danpost

2018/4/11

#
Lavenger wrote...
it seems like I'm having troubles getting the debugger to show, so I can't see the actTimer value. it says "Thread is running. Threads must be stopped to view details." any idea on how to fix that?
You do not need to use the debugger here. To inspect an object, pause the scenario, right click on the object and select Inspect from the pop-up menu.
Lavenger Lavenger

2018/4/11

#
danpost wrote...
You did not show the revised code -- adding it into the world and setting its active state.
here's the revised code of MiniTaihei and MyWorld respectively:
import greenfoot.*;
public class MiniTaihei extends greenfoot.Actor 
{
    int nx = 0, ny = 0, fx = 0, fy = 0;
    Timer actTimer = new Timer(40);
    private boolean once = false;
    private boolean run = false;
    public MiniTaihei()
    {
        getImage().scale(30, 30);
    }

    public void act()
    {
        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();
            }
        }
        else if (isTouching(MiniKirie.class))
        {
            getWorld().removeObjects(getWorld().getObjects(Actor.class));
        }
    }   
}
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    
    //For GIF background stuff
    GifImage gifImg = new GifImage("Umaru.gif");
    
    //For Timing stuff
    private Actor TimeDisplay = new SimpleActor(), Cooldown = new SimpleActor();
    private int frames;
    private int Cframes = (3+Greenfoot.getRandomNumber(5))*55;
    
    //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 Buttons stuff
    public Actor MKButton = new SimpleActor(), HButton = new SimpleActor(), BButton = new SimpleActor(), AButton = new SimpleActor(), SButton = new SimpleActor(), KButton = new SimpleActor(), EButton = new SimpleActor(), TButton = new SimpleActor();
    public Actor Buttons[] = {MKButton, HButton, BButton, AButton, SButton, KButton, EButton, TButton};
    private String ButtonImages[] = {"MKButton", "HButton", "BButton", "AButton", "SButton", "KButton", "EButton", "TButton"};
    private int ButtonsArraySize = Buttons.length;
    
    //For PIP stuff
    PIP pip;
    World minor;
    public Boolean setActiveState = false;
    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 adding in the buttons to add the actors into the world
        for( int i = 0; i < ButtonsArraySize; i++)
        {
            Buttons[i].setImage(ButtonImages[i] + ".png"); Buttons[i].getImage().scale(70, 70); addObject(Buttons[i], 1175, (120 + (i*50)));
            i++;
            Buttons[i].setImage(ButtonImages[i] + ".png"); Buttons[i].getImage().scale(70, 70); addObject(Buttons[i], 1325, (120 + ((i-1)*50)));
        }
    }
    
    //For GIF background
    private 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 buying the Characters
        if(Greenfoot.mouseClicked(MKButton) && AYen >= 100 && (getObjects(MgrKanau.class).isEmpty())) {AYen -= 100; addObject(new MgrKanau(), 600, 600);}
        if(Greenfoot.mouseClicked(HButton) && AYen >= 200 && (getObjects(Hikari.class).isEmpty())) {AYen -= 200; addObject(new Hikari(), 600, 600);}
        if(Greenfoot.mouseClicked(AButton) && AYen >= 400 && (getObjects(Alex.class).isEmpty())) {AYen -= 400; addObject(new Alex(), 600, 600);}
        if(Greenfoot.mouseClicked(SButton) && AYen >= 800 && (getObjects(Sylphynford.class).isEmpty())) {AYen -= 800; addObject(new Sylphynford(), 600, 600);}
        if(Greenfoot.mouseClicked(BButton) && AYen >= 1600 && (getObjects(Bomba.class).isEmpty())) {AYen -= 1600; addObject(new Bomba(), 600, 600);}
        if(Greenfoot.mouseClicked(KButton) && AYen >= 3200 && (getObjects(Kirie.class).isEmpty())) {AYen -= 3200; addObject(new Kirie(), 600, 600);}
        if(Greenfoot.mouseClicked(EButton) && AYen >= 6400 && (getObjects(Ebina.class).isEmpty())) {AYen -= 6400; addObject(new Ebina(), 600, 600);}
        if(Greenfoot.mouseClicked(TButton) && AYen >= 12800 && (getObjects(Taihei.class).isEmpty())) {AYen -= 12800; addObject(new Taihei(), 600, 600);}
        
        //For updating the Timing stuff
        if ((++frames)%55 == 0) updateTimeDisplay();
        if ((--Cframes)%55 == 0) updateCooldownDisplay();
        if (Cframes == 0)
        {
            Minigame();
            Cframes += ((3+Greenfoot.getRandomNumber(5))*55); //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 removing ended Minigames
        if (pip != null && pip.getWorld() == this && minor.getObjects(null).isEmpty())
        {
            removeObject(pip);
        }
    }
    
    
    //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 and calling out the Minigame
    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(3))
        {
            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(Alex.class).isEmpty()) minor = new Minigame4(); break;
            case 4: if (!getObjects(Bomba.class).isEmpty()) minor = new Minigame5(); break;
        }
        if (minor != null)
        {
            pip = new PIP(minor, new Class[] {});
            pip.setActiveState(true);
            addObject(pip, 690, 110);
        }
    }
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
    }
}
danpost wrote...
You do not need to use the debugger here. To inspect an object, pause the scenario, right click on the object and select Inspect from the pop-up menu.
oh okay...
danpost wrote...
Inspect the Taihei object and then inspect its actTimer. If its value is 40, then the timer was never started (if everything else was done properly).
well I have inspected it and its stuck at 40, what can I do to start it?(I think the active state is active so I don't think that it's the active state that's wrong here)
danpost danpost

2018/4/11

#
What Timer class are you using? Show please.
Lavenger Lavenger

2018/4/11

#
danpost wrote...
What Timer class are you using? Show please.
public class Timer  
{
    private int delayAmount; // the number of steps to complete one cycle
    private int counter; // the current value of the timer
    private boolean paused; // the current paused state of the timer
    private boolean looping; // the looping state of the timer
    
    /**
     * this secondary constructor creates a timer object that does not loop automatically
     *
     * @param amount the number of steps to complete one cycle
     */
    public Timer(int amount)
    {
        this(amount, false); // calls the primary constructor with a looping value of false
    }
    
    /**
     * this primary constructor sets up a timer object
     *
     * @param amount the number of steps to complete one cycle
     * @param toLoop a flag indicating whether the timer should loop automatically or not
     */
    public Timer(int amount, boolean toLoop)
    {
        delayAmount = amount; // number of steps to delay
        looping = toLoop; // save looping state
        if (looping) counter = -1; // adjust counter if looping (looping counters must be explicitly started)
    }
    
    /**
     * usually called by the object that created the timer to execute one step and return a boolean value
     * indicating whether the timer is still running or not; a paused state is a busy state and returns a
     * true value just as if the timer was still running
     *
     * @return a boolean value indicating whether the timer is paused or has not completed a cycle, or not
     */
    public boolean iterating()
    {
        if (paused) return true; // timer is paused
        if (counter == 0 && looping) counter = delayAmount; // reset counter if end of cycle and looping
        if (counter > 0) counter--; // decrement counter
        return counter > 0; // return true if cycle is not complete
    }
    
    /**
     * stops the timer
     */
    public void stop()
    {
        counter = -1; // sets counter value to one indicating the timer is stopped
        paused = true; // sets paused flag to true
    }
    
    /**
     * starts the timer
     */
    public void start()
    {
        paused = false; // sets pausd flag to false
        counter = looping ? 0 : delayAmount; // sets counter to an appropriate value depending on looping state
    }
    
    /**
     * un-pauses the timer
     */
    public void resume()
    {
        paused = false; // sets paused flag to false
    }
    
    /**
     * pauses the timer
     */
    public void pause()
    {
        paused = true; // sets paused flag to true
    }
}
danpost danpost

2018/4/11

#
Okay, it is my Timer class. Try changing line 31 of your MiniTaihei class to this:
actTimer = new Timer(40);
Lavenger Lavenger

2018/4/11

#
danpost wrote...
Okay, it is my Timer class. Try changing line 31 of your MiniTaihei class to this:
actTimer = new Timer(40);
doesn't seem to be working...
danpost danpost

2018/4/11

#
In the MiniTaihei class, remove lines 16, 17 and 33; also, remove the word else from line 34.
Lavenger Lavenger

2018/4/11

#
danpost wrote...
In the MiniTaihei class, remove lines 16, 17 and 33; also, remove the word else from line 34.
it's working now but the actor seems to move really fast once and it doesn't seem to move after that one key input Example: -key "d" is pressed -actor moves to the right and reaches the end of the screen sporadically -actor doesn't respond to the key input like it's supposed to (it does respond but its very random)
danpost danpost

2018/4/11

#
Remove the second conditions from lines 18, 20, 22 and 24; then change line 26 to:
if (fx*fy == 0 && fx+fy != 0 && getOneObjectAtOffset(fx, fy, Wall.class) == null)
Lavenger Lavenger

2018/4/12

#
The result after the change is still the same, I have a feeling that what I'm describing is probably not what you think it is, I have uploaded the scenario for you to see what's wrong with it, sorry that I couldn't explain well enough and that you have to see for yourself... here's the scenario ...
danpost danpost

2018/4/12

#
Lavenger wrote...
The result after the change is still the same, I have a feeling that what I'm describing is probably not what you think it is, I have uploaded the scenario for you to see what's wrong with it, sorry that I couldn't explain well enough and that you have to see for yourself... here's the scenario ...
I got it -- will investigate.
danpost danpost

2018/4/12

#
In the MiniTaihei class: * remove lines 4, 28 and 29; * change "nx" and "ny" in line 30 to "fx" and "fy"; and * insert the following at line 16.
int fx = 0, fy = 0;
All line numbers refer to locations as shown above.
Lavenger Lavenger

2018/4/12

#
thanks, its working fine now, but for future references, can you tell me what was wrong about the code that I have before?
danpost danpost

2018/4/12

#
Lavenger wrote...
thanks, its working fine now, but for future references, can you tell me what was wrong about the code that I have before?
fx and fy were not getting zeroed every act cycle.
Lavenger Lavenger

2018/4/13

#
oh I see... didn't even see that while I was checking
You need to login to post a reply.
1
2
3
4