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

2018/4/1

Buttons

Lavenger Lavenger

2018/4/1

#
Does button 1, 2 and 3 need to be created indiviually to add in Actor 1, 2 and 3 respectively or i can just use SimpleActor to add in button 1, 2 and 3 to add in Actor 1, 2 and 3 repectively Example: Button 1 adds in Actor1 Button 2 adds in Actor2 Button 3 adds in Actor3 ect.
danpost danpost

2018/4/1

#
Lavenger wrote...
Does button 1, 2 and 3 need to be created indiviually to add in Actor 1, 2 and 3 respectively or i can just use SimpleActor to add in button 1, 2 and 3 to add in Actor 1, 2 and 3 repectively
Both ways are possible. Using SimpleActor to create all buttons is easier to code.
Lavenger Lavenger

2018/4/1

#
danpost wrote...
Lavenger wrote...
Does button 1, 2 and 3 need to be created indiviually to add in Actor 1, 2 and 3 respectively or i can just use SimpleActor to add in button 1, 2 and 3 to add in Actor 1, 2 and 3 repectively
Both ways are possible. Using SimpleActor to create all buttons is easier to code.
but if i were to press button 1, button 2 and 3 won't be triggered even when they're all under the SimpleActor class right?(I want it to be that way)
danpost danpost

2018/4/1

#
Lavenger wrote...
but if i were to press button 1, button 2 and 3 won't be triggered even when they're all under the SimpleActor class right?(I want it to be that way)
Mouse clicks are on instances, not on classes.
Lavenger Lavenger

2018/4/9

#
it seems like I couldn't get the object reference right while using the mouseClicked() method... how do I refer the object for the mouseClicked() method? this is the code I'm using for the mouseClicked() Method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MyWorld extends World
{
    private 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();
    Actor Buttons[] = {MKButton, HButton, BButton, AButton, SButton, KButton, EButton, TButton};
    String ButtonImages[] = {"MKButton", "HButton", "BButton", "AButton", "SButton", "KButton", "EButton", "TButton"};
    private int ButtonsArraySize = Buttons.length;
    public MyWorld()
    {
        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)));
        }
    }
    public void act()
    {
        switch(8)
        {
            case 0: if(mouseClicked(MKButton) && AYen >= 100 && (getObjects(MgrKanau.class).isEmpty())) AYen -= 100; addObject(new MgrKanau(), 600, 600); break;
            case 1: if(mouseClicked(HButton) && AYen >= 200 && (getObjects(Hikari.class).isEmpty())) AYen -= 100; addObject(new Hikari(), 600, 600); break;
            case 2: if(mouseClicked(AButton) && AYen >= 400 && (getObjects(Alex.class).isEmpty())) AYen -= 100; addObject(new Alex(), 600, 600); break;
            case 3: if(mouseClicked(SButton) && AYen >= 800 && (getObjects(Sylphynford.class).isEmpty())) AYen -= 100; addObject(new Sylphynford(), 600, 600); break;
            case 4: if(mouseClicked(BButton) && AYen >= 1600 && (getObjects(Bomba.class).isEmpty())) AYen -= 100; addObject(new Bomba(), 600, 600); break;
            case 5: if(mouseClicked(KButton) && AYen >= 3200 && (getObjects(Kirie.class).isEmpty())) AYen -= 100; addObject(new Kirie(), 600, 600); break;
            case 6: if(mouseClicked(EButton) && AYen >= 6400 && (getObjects(Ebina.class).isEmpty())) AYen -= 100; addObject(new Ebina(), 600, 600); break;
            case 7: if(mouseClicked(TButton) && AYen >= 12800 && (getObjects(Taihei.class).isEmpty())) AYen -= 100; addObject(new Taihei(), 600, 600); break;
        }
    }
}
danpost danpost

2018/4/10

#
Remove the switch in the act method. Just keep the if statements.
Lavenger Lavenger

2018/4/10

#
May I ask what is the switch statement mostly used for?
danpost danpost

2018/4/10

#
Lavenger wrote...
May I ask what is the switch statement mostly used for?
It is used to take only one of many possible options (or none). For example, if you had multiple type actors and you wanted to put only one, but randomly picked into the world, you could have this:
1
2
3
4
5
6
7
8
9
10
11
Actor actor = null;
switch(Greenfoot.getRandomNumber(800)
{
    case 0:  actor = new ActorA(); break;
    case 1:  actor = new ActorB(); break;
    case 2:  actor = new ActorC(); break;
    case 3:  actor = new ActorD(); break;
    case 4:  actor = new ActorE(); break;
    case 5:  actor = new ActorF(); break;
}
if (actor != null) addObject(actor, Greenfoot.getRandomNumber(getWidth(), 0);
This would add a random actor somewhere at the top edge of the world an average of maybe one every two seconds or so. Most of the time the switch block will end up doing nothing, keeping actor set to null.
Lavenger Lavenger

2018/4/10

#
oh okay, thanks for explaining
You need to login to post a reply.