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

2014/11/13

Mouse Click Game: Keys and Doors

meazalplaq meazalplaq

2014/11/13

#
I am trying to get a door to open after the player has acquired a key. I originally used the following code provided by danpost: if ( getWorld().getObjects(Key.class).isEmpty() && Greenfoot.mouseClicked(this) ) This did the trick; however, I have noticed that the code only seems to work if the key.class and the location.class are populated from the World.class at the start of the game. Is there any way to adjust the code so the door will unlock if the key is added from anywhere in the game? For example, there is a door on Location_03, but the player has to go to Location_04 or 05 to get the key and then go back to Location_03 to unlock the door. How would I approach creating code for that scenario? Any ideas? Thank you,
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class Key extends Actor
{
 
     
    /**
     * Act - do whatever the Key wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         
    {
       MouseInfo mouse = Greenfoot.getMouseInfo();
        {
            if (Greenfoot.mouseClicked(this))
            {
               
                 getWorld().removeObject(this);
                 
            }
        }
    }   
     
    }   
    
 
}
-----------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class Location_02 extends Actor
{
    public Location_02()
    {
        GreenfootImage myPic = new GreenfootImage(1024, 640);
        myPic.fill();
        myPic.drawImage(new GreenfootImage("Location_02.jpg"),0,0);
        setImage(myPic);
 
    }
 
   public void act()
    {
       MouseInfo mouse = Greenfoot.getMouseInfo();
        {
            if ( !getWorld().getObjects(Key.class).isEmpty()&& Greenfoot.mouseClicked(this) && mouse.getX() > 644 && mouse.getX() < 900 && mouse.getY() > 75 && mouse.getY() < 524)
        {
                 getWorld().addObject(new Dialog_01(), 509,577);
                
                 getWorld().addObject(new Direction_01(), 90,267);
                  
//                  getWorld().removeObject(this);
                }
                 
             else if ( getWorld().getObjects(Key.class).isEmpty() && Greenfoot.mouseClicked(this) && mouse.getX() > 644 && mouse.getX() < 900 && mouse.getY() > 75 && mouse.getY() < 524)
            {
               
                  getWorld().addObject(new Location_03(), 512,320);
//                   getWorld().addObject(new Box(), 244,260);
                  getWorld().removeObject(this);
                 
            }
        }
    }   
}
-------------------
danpost danpost

2014/11/14

#
If you want the player to be at the door to unlock it instead of a mouse click on the door, then change 'Greenfoot.mouseClicked(this)' to 'getOneIntersectingObject(Player.class) != null'. 'Player' may need to be changed to the name of the class of your player. You can do the same for the key, if you want your player to touch the key instead of a mouse click on it to remove it.
meazalplaq meazalplaq

2014/11/14

#
Thank you danpost for your reply; however, I do not have a player character in the scene. This is a point and click style game. I am trying to get the key from another actor class called Location_01 that will open the door at actor class Location_02. Can I still use your suggestion in that type of scenario? I am assuming that I am going to have to use a boolean variable that will tell Greenfoot when I have the key to open the door, but I am pretty knew to programming and the Greenfoot environment. Am I on the right track with that idea? Do you have any suggestions or examples? Thank you for your help,
danpost danpost

2014/11/14

#
It may not be as difficult as you think. It really depends on exactly what you are trying to do. If I am not mistaken on what you are trying to do, you could use the World class 'setPaintOrder' method to hide and show the key at the location -- so the key will be in the world at all times, it just will not be able to be clicked on until the location is clicked on first (which then shows to key so it can be clicked). Would something like that work for you? (you would have code in the location the key is at whereby when clicked the paint order is altered)
meazalplaq meazalplaq

2014/11/14

#
How would I approach coding that? Would I use getWorld().addObject?
danpost danpost

2014/11/14

#
I really do not know what you are trying to do exactly -- and when. Your explanation in your first post was a bit generalized. If you wanted to set the paint order, then you would use 'getWorld().setPaintOrder( /* classes from front to back */ );'.
meazalplaq meazalplaq

2014/11/15

#
Well, I want the player to find the locked door first. After he or she discovers the door is locked, the player has to go find the key in some other location. Right now, the key has to be populated before the door, and the paint order does not seem to allow actors to move around. Each class has to flow in a strict order: one class in front of the next. Is there a way to move classes around in the paint order or hide an actor so the if statement still works?
danpost danpost

2014/11/15

#
Please refrain from calling an 'actor' a 'class'. An actor goes into a world, not a class. In fact, many actors created from the same Actor subclass can be added into a world. Also, you keep referring to a player, when I think you mean the user. Either that or you are referring to the mouse pointer as being the player. You can change the paint order within your code as often as you want. If you place the key at the same place as the location that must be arrived at first (I assume you click on that location and the key should show up). Then in the act method of that location:
1
if (Greenfoot.mouseClicked(this)) getWorld().setPaintOrder(Key.class);
When populating the location and the key (of course, before the door), use
1
setPaintOrder(Location_04.class, Location_05.class);
meazalplaq meazalplaq

2014/11/15

#
I will give that a try. Thank you for your help.
meazalplaq meazalplaq

2014/11/15

#
Thank you for your help danpost. It worked!
You need to login to post a reply.