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

2017/2/21

Does Greenfoot.mouseClicked() only work if the parameter is the object itself (this)?

1
2
3
4
Super_Hippo Super_Hippo

2017/2/21

#
However, if you add a call to changeBackground after line 357, it may do what it should.
SallySanban SallySanban

2017/2/21

#
I tried that. Still, nothing happens.
Super_Hippo Super_Hippo

2017/2/21

#
Um... the most stupid idea to press F5 instead of 5 in the middle of the post -.- Ok, again... I thought of something like this:
private int type;
private Actor player;
private int changeLeft=91, changeRight=519;
private static final GreenfootImage[] bgImages =
{
    new GreenfootImage("Alleyway 4.PNG"),
    new GreenfootImage("Kalachuchi Walk 1.JPG"),
    new GreenfootImage("School Entrance.JPG")
    //....
};

public MyWorld()
{
    this(new UserWalk1(), 0, -1);
}

public MyWorld(Actor p, int worldType, int lastWorldType)
{
    super(..........);
    player = p;
    type = worldType;
    setBackground(bgImages[type]);
    int x, y=243; //where to add the player
    if (lastWorldType < worldType) //moved right
    {
        x=125;
    }
    else //moved left
    {
        x=500;
    }
    
    switch (type)
    {
        case 2: changeRight=464; break;
        case 5: addObject(new CafeteriaDoor(), 429, 126); break;
    }
    addObject(player, x, y);
}



public void changeBackground(int change) //1 for right, -1 or left
{
    Greenfoot.setWorld(new MyWorld(player, type+change, type));
}

public void act()
{
    int x = player.getX();
    if (x<changeLeft && type!=0) changeBackground(-1);
    else if (x>changeRight && type!=25) changeBackground(1);
}
In the CafeteriaDoor class:
public void act()
{
    if (Greenfoot.mouseClicked(this)) //I usually use mousePressed but that is obviously your choice
    {
        ((MyWorld) getWorld()).changeBackground(1);
    }
}
For the costume thing, you can simplify it to this:
private static final GreenfootImage[] costumeImages =
{
    new GreenfootImage("User Walk 1.gif"),
    new GreenfootImage("User Walk 2.gif"),
    new GreenfootImage("User Walk 3.gif"),
    new GreenfootImage("User Walk 4.gif")
};

//when moving right
setImage(costumeImages[Greenfoot.getRandomNumber(2)]);
//when moving left
setImage(costumeImages[Greenfoot.getRandomNumber(2)+2]);
Super_Hippo Super_Hippo

2017/2/21

#
SallySanban wrote...
I tried that. Still, nothing happens.
I thought that the location change happens, but not the background change... If really nothing happens (=no background change and no location change), then there was no click detected on the "door" object. :S
SallySanban SallySanban

2017/2/22

#
Thanks! I'll get back to you if I need any more help on this. :)
SallySanban SallySanban

2017/2/22

#
I tried something new in my original code. It still doesn't work and I don't exactly know why. (I just need to know what I'm doing wrong so I won't make the same mistake.) I placed the mouseClicked in the CafeteriaDoor class and passed the variable 'background' to it from the UserWalk1 class. When it's clicked, I made it so that background will increment by 1. It does this, but the background doesn't change even though the code in the UserWalk1 class says:
int bgNumber = background % totalBG;
else if(bgNumber == 6)
        {
            getWorld().setBackground("Cafeteria 1.JPG");
        }
Also, when I made int bgNumber an instance variable instead of a local variable in method changeBackground(), the background does not change at all and nothing happens to the UserWalk1 actor in the scenario. It's like the changeBackground() method doesn't work. Would anyone know why this happens?
Super_Hippo Super_Hippo

2017/2/22

#
If you pass the background to the CafeteriaDoor and change that passed background variable, nothing will happen. You have to change the background variable in the UserWalk1 class. (You pass the value of the variable, not the variable itself.)
SallySanban SallySanban

2017/2/22

#
Super_Hippo wrote...
If you pass the background to the CafeteriaDoor and change that passed background variable, nothing will happen. You have to change the background variable in the UserWalk1 class. (You pass the value of the variable, not the variable itself.)
Thanks! I tried your code and it worked! I don't really understand it that much though, hehe. How will I be able to add EloraWalk1 into the world and have her follow UserWalk1? I already made a code for that and it actually worked the way I wanted it to, but now that the code's changed and I don't understand it, I'm having trouble.
Super_Hippo Super_Hippo

2017/2/22

#
Glad that it worked. Usually I mess something up when I post code without testing it ^^ Understanding the code will make you understand how to add the Elora into it (even though I am not sure what it is). It looks like it could be done similar to how I added the player depending on which world it is and from which side it was entered. If you have questions about some specific lines, just ask about it.
SallySanban SallySanban

2017/2/22

#
How did you add an Actor into the world without using addObject?
this(new UserWalk1(), 0, -1);
I'm wondering how this works if the x and y coordinates aren't what I set it to in my original code. The EloraWalk1 class is another character in the game that simply follows UserWalk1 around. In my original code, I passed EloraWalk1 to the UserWalk1 class in the World.
public Alleyway4() //the World in my original code
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    
    public void prepare()
    {
        EloraWalk1 elorawalk1 = new EloraWalk1();
        addObject(elorawalk1, 95, 270);
        UserWalk1 userwalk1 = new UserWalk1(elorawalk1); //I passed EloraWalk1 to UserWalk1
        addObject(userwalk1, 125, 249);
        userwalk1.setLocation(245, 243);
        Inventory inventory = new Inventory();
        addObject(inventory, 45, 361);
        Text2 text2 = new Text2();
        addObject(text2, 304, 283);
        Color transparent = new Color(0, 0, 0, 0);
        text2.setImage(new GreenfootImage(" ", 22, Color.BLACK, transparent));
        text2.setLocation(304, 283);
        setPaintOrder(Texts.class, TextBox.class, Props.class, Characters.class, Walk.class, Doors.class);
    }
In UserWalk1, here is what I did so that Elora would be able to follow her.
public void followUser()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            if(elorawalk1.getX() < this.getX())
            {
                elorawalk1.move(1);
                costumeElora = Greenfoot.getRandomNumber(2) + 1;
                if(costumeElora == 1)
                {
                    switchCostumeElora(2);
                }
                else if(costumeElora == 2)
                {
                    switchCostumeElora(1);
                }
            }
        }
        if(Greenfoot.isKeyDown("left"))
        {
            if(elorawalk1.getX() > this.getX())
            {
                elorawalk1.move(-1);
                costumeElora = Greenfoot.getRandomNumber(2) + 3;
                if(costumeElora == 3)
                {
                    switchCostumeElora(4);
                }
                else if(costumeElora == 4)
                {
                    switchCostumeElora(3);
                }
            }
        }
    }

    /**
     * Changes the costume of Elora to make her look like she's walking.
     */
    public void switchCostumeElora(int costumeNumber)
    {
        GreenfootImage costume1 = new GreenfootImage("Elora Walk 1.gif");
        GreenfootImage costume2 = new GreenfootImage("Elora Walk 2.gif");
        GreenfootImage costume3 = new GreenfootImage("Elora Walk 3.gif");
        GreenfootImage costume4 = new GreenfootImage("Elora Walk 4.gif");
        if(costumeNumber == 1)
        {
            elorawalk1.setImage(costume2);
        }
        else if(costumeNumber == 2)
        {
            elorawalk1.setImage(costume1);
        }
        else if(costumeNumber == 3)
        {
            elorawalk1.setImage(costume4);
        }
        else if(costumeNumber == 4)
        {
            elorawalk1.setImage(costume3);
        }
    }
I just copy pasted what I did for UserWalk1's costume changing for Elora, but I made it so that Elora would only walk behind User and in a distance by making her move slower than UserWalk1. In the new code, I don't know how to pass EloraWalk1 to UserWalk1, because I'm unable to put it inside the parameter since it says that 'this' has to be the starting line of the World constructor.
SallySanban SallySanban

2017/2/22

#
Is there a limit to lists? I'm adding up to 25 background images in the list bgImages and it fails to compile (It says that there are no syntax errors but the striped lines in the scenario won't go away and I can't run it) at my 14th background image.
danpost danpost

2017/2/22

#
SallySanban wrote...
Is there a limit to lists? I'm adding up to 25 background images in the list bgImages and it fails to compile (It says that there are no syntax errors but the striped lines in the scenario won't go away and I can't run it) at my 14th background image.
There is virtually no limit to lists; however, images are memory hogs. You should probably only load the images when needed. You can create a list of String filenames which would not require so much memory, if needed.
SallySanban SallySanban

2017/2/22

#
danpost wrote...
SallySanban wrote...
Is there a limit to lists? I'm adding up to 25 background images in the list bgImages and it fails to compile (It says that there are no syntax errors but the striped lines in the scenario won't go away and I can't run it) at my 14th background image.
There is virtually no limit to lists; however, images are memory hogs. You should probably only load the images when needed. You can create a list of String filenames which would not require so much memory, if needed.
How would you do that?
Super_Hippo Super_Hippo

2017/2/22

#
Putting 25 images into an array and save that in the class for the entire game (instead of creating a new image every time a new world is loaded) shouldn't be any problem at all.
this(new UserWalk1(), 0, -1);
This line does not add the UserWalk1 object into the world. It just creates one and passes it to the other world constructor. It is only executed once when the game starts. When changing worlds later, the same UserWalk1 object is passed to the next world. The object is added to the world at the end of the second constructor:
addObject(player, x, y);
I didn't know that you passed the EloraWalk object like this. You still should be able to do the same now.
this(new UserWalk1(new EloraWalk1()), 0, -1);
Make sure you have a method to get the EloraWalk1 object from the UserWalk1 object.
private EloraWalk1 elorawalk;

public UserWalk1(EloraWalk1 e)
{
    elorawalk = e;
    //...
}

public EloraWalk1 getEloraWalk() {return elorawalk;}
Then, in the second world constructor, you can also add the EloraWalk1 object depending on which type of world is created and what the last world was.
SallySanban SallySanban

2017/2/22

#
How did you get to assign UserWalk1 to the variable player? How do I also do that for EloraWalk1 so that I can add the object? (I'm sorry for all the questions. I really don't get it.)
There are more replies on the next page.
1
2
3
4