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

2018/3/30

"Minigame" Event

1
2
Lavenger Lavenger

2018/3/30

#
This is my plan for triggering the Event(Minigame): 1) Generate number(3-7) for minute coundown V 2) When countdown reaches 0 seconds, generate number(0-4) to tigger the Event V 3) Check if the actor that "has" the Event exists in the world V 4) If yes, trigger the Event. If no, move back to Step 1 However i need some help on the actual coding, please teach me how to do it... 1) I don't know how to check if a specific actor exists in the world 2) How to start the countdown as soon as the game starts
danpost danpost

2018/3/30

#
1) Use: 3+Greenfoot.getRandomNumber(5) 2) Use: switch(Greenfoot.getRandomNumber(5)) 3) Use: if (!getObjects(<< whatever >>.class).isEmpty())) or if (<< actor >>.getWorld() != null) 4) just reset the timer and only start at (1) when timer reaches 0
Lavenger Lavenger

2018/3/30

#
danpost wrote...
2) Use: switch(Greenfoot.getRandomNumber(5))
what's switch?
danpost danpost

2018/3/30

#
Lavenger wrote...
what's switch?
See here.
Lavenger Lavenger

2018/3/31

#
i've coded the switch, is this alright?:
    private void Minigame()
    {
        switch(Greenfoot.getRandomNumber(5))
        {
            case 1:    if (Taihei.getWorld() != null)
                           {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 (Sylphynford.getWorld() != null)
                           {Minigame2();} //Plays a 2 player minigame to beat Sylphynord's new score as a challenge
                       break;

            case 3:    if (Ebina.getWorld() != null)
                           {Minigame3();} //Introduces Umaru to a fashion modeling job
                       break;

            case 4:    if (Kirie.getWorld() != null)
                           {Minigame4();} //Animal crossing minigame
                       break;

            case 5:    if (Taihei.getWorld() != null)
                           {Minigame5();} //
                       break;
        }
    }
        private void Minigame1()
    {
        //to be implemented
    }
        private void Minigame2()
    {
        //to be implemented
    }
        private void Minigame3()
    {
        //to be implemented
    }
        private void Minigame4()
    {
        //to be implemented
    }
        private void Minigame5()
    {
        //to be implemented
    }
PS: all of the .getWorld seems to be gving me errors that says: "non-static method getWorld() cannot be referenced from a static context"
Vercility Vercility

2018/3/31

#
No, randomNumber(5) gives numbers from 0 to 4, the 5 is exclusive Where did you declare/initialize the characters? Show the Complete class code
danpost danpost

2018/3/31

#
danpost wrote...
if (<< actor >>.getWorld() != null)
Note that you need the actor, not the class. Another way might be to use this:
if (!getObjects(Taihei.class).isEmpty())
Lavenger Lavenger

2018/3/31

#
Vercility wrote...
No, randomNumber(5) gives numbers from 0 to 4, the 5 is exclusive Where did you declare/initialize the characters? Show the Complete class code
here's the code:
import greenfoot.*;
public class MyWorld extends World
{
    // fields usually (by convention) go first
    GifImage gifImg = new GifImage("Umaru.gif");
    private Actor TimeDisplay = new SimpleActor();
    private Actor Cooldown = new SimpleActor();
    private int frames;
    private int Cframes = (3+Greenfoot.getRandomNumber(4))*3300;
    public float AYen; //precise
    public int DYen; //rounded
    public Yen YenDisplay = new Yen();
    public int getHeight = 552 ;
    public int getWidth = 1200 ;
    // class constructors usually (by convention) go next
        public void gifAnimation()
    {
        for (Object obj : gifImg.getImages()) ((GreenfootImage)obj).scale(getWidth(), getHeight());
        setBackground(gifImg.getCurrentImage());
    }
    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);
    }     
    // methods (by convention) go last
    
    //For GIF background
    public void act()
    {
        gifAnimation();
        if ((++frames)%55 == 0) updateTimeDisplay();
        if ((--Cframes)%55 == 0) updateCoolDownDisplay();
        if (Cframes == 0)
        {
            Minigame();
            Cframes += (3+Greenfoot.getRandomNumber(5)); //chooses a cooldown time between 3-7 minutes randomly     
        }
    }
    
    //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)));
        
    }
    
    //For Choosing the Minigame and 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)));
    }
    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;
        }
    }
        private void Minigame1()
    {
        //to be implemented
    }
        private void Minigame2()
    {
        //to be implemented
    }
        private void Minigame3()
    {
        //to be implemented
    }
        private void Minigame4()
    {
        //to be implemented
    }
        private void Minigame5()
    {
        //to be implemented
    }
    
    //For placing actors into the world
    private void prepare()
    {
        addObject(new Umaru(), 700, 338);
    }
}
Lavenger Lavenger

2018/3/31

#
danpost wrote...
danpost wrote...
if (<< actor >>.getWorld() != null)
Note that you need the actor, not the class. Another way might be to use this:
if (!getObjects(Taihei.class).isEmpty())
wait, so <<actor>>.class is a actor and <<actor>> is the class?
Vercility Vercility

2018/3/31

#
No. actor.class refers to the class itself as a whole while just <<actor>> will refer to either an object type (that is a class) or the class to use the following methods on (for example actor.getX() ) getObjects returns objects(that is, specific instances of a class) which is what dan meant as actor
Lavenger Lavenger

2018/3/31

#
Vercility wrote...
No. actor.class refers to the class itself as a whole while just <<actor>> will refer to either an object type (that is a class) or the class to use the following methods on (for example actor.getX() ) getObjects returns objects(that is, specific instances of a class) which is what dan meant as actor
ahh... i see, can you help me with this too? I'm having troubles resetting the timer...
danpost danpost

2018/3/31

#
Lavenger wrote...
wait, so <<actor>>.class is a actor and <<actor>> is the class?
I probably should have used <<Actor subclass>>.class to be more precise instead of what I did used there. Anytime you see me use the double angle brackets like that, you need to substitute the appropriate thing. The ".class" should indicate to you that a Class type is needed and the ".getWorld()" should indicate a reference to an Actor instance is needed.
Lavenger Lavenger

2018/3/31

#
danpost wrote...
Lavenger wrote...
wait, so <<actor>>.class is a actor and <<actor>> is the class?
I probably should have used <<Actor subclass>>.class to be more precise instead of what I did used there. Anytime you see me use the double angle brackets like that, you need to substitute the appropriate thing. The ".class" should indicate to you that a Class type is needed and the ".getWorld()" should indicate a reference to an Actor instance is needed.
ahh.. so the difference is that one requires subclass and the other requires class... but i dont quite understand the difference between class and subclass, can you teach me more about it?
Lavenger Lavenger

2018/3/31

#
Short question: If the player were to go to another world, would the world that the player is previously in continue running? if no, is it possible to make it continue running as the player is in another world?
danpost danpost

2018/3/31

#
Lavenger wrote...
ahh.. so the difference is that one requires subclass and the other requires class...
No -- one requires a class and the other requires an instance created from a class. Actor is a class, but actor, as used below is an instance of a class:
Actor actor = new SimpleActor();
You could ask:
if (actor instanceof SimpleActor)
// or
if (actor instanceof Actor)
// or even
if (actor.getWorld() instanceof MyWorld)
These check to see if the object on the left was created by the class on the right or by a subclass of that class.
There are more replies on the next page.
1
2