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

2016/2/15

Reference to objects in world

Lauri Lauri

2016/2/15

#
So I've been working on this game and while processing the keys I got a lil problem. I have two cuboids (Quader and QuaderL), Quader moves up and down and QuaderL moves left and right and I made little barriers so they cant go out of the selected area which 102 pixels up and 102 pixels down. The problem in this case is that I dont know how to refer to the objects in getOneObjectAtOffset so they stop before colliding with the barriers. It worked fine when I had the key processing in their classes but they couldnt move at the same time so I had to put them in World to use the up+down and left+right keys at the same time, I guess? Heres the code, I'm not sure if its right at all but it always says the getOneAtOffset is an error.
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
private void processKeys()
   {
       String key = Greenfoot.getKey();
       Quader quader = new Quader();
       QuaderL quaderL = new QuaderL();
        
       if ("down".equals(key))
       {
           if (getOneObjectAtOffset(0,51,Barrier.class) == null)
           {
               quader.setLocation (quader.getX(), quader.getY() + 51);
           }
       }
 
       if ("up".equals(key))
       {
           if (getOneObjectAtOffset(0,-51,Barrier.class) == null)
           {
               quader.setLocation (quader.getX(), quader.getY() - 51);
           }
       }
        
       if("left".equals(key))
       {
           if (getOneObjectAtOffset(-51,0,Barrier.class) == null)
           {
               quaderL.setLocation (quaderL.getX() - 51, quaderL.getY());
           }
       }  
        
       if("right".equals(key))
       {
           if (getOneObjectAtOffset(51,0,Barrier.class) == null)
           {
               quaderL.setLocation (quaderL.getX() + 51, quaderL.getY());
           }
       }
   }
I'd be glad if anyone could help me!
Super_Hippo Super_Hippo

2016/2/15

#
You should not create new Quader objects every time. This would not let you move the quaders if this code would be executed. It is not because 'getOneObjectAtOffset' is an Actor method and not a World method. The method checks for a location relative to an actor inside a world. This doesn't make sense used on a world object.
danpost danpost

2016/2/15

#
There are a couple of issues with the code given. First, if the code is in your World subclass, then you cannot use the method 'getOneObjectAtOffset' without specifying an Actor object to run the method on (there is no method with that name in the World class API -- only in the Actor class API). Besides that, the method has 'protected' access and cannot be called from any class that is not a subclass of Actor. Second, lines 4 and 5 of the method given are creating new Actor objects which are not the ones already in your world and any reference to them, 'quadar' and 'quadarL', later in the method will refer to those just created (the ones NOT in your world). There are two ways to resolve your issues. One is to continue to get the keys in the world class and then call a method in your Actor subclasses on those objects in your world depending on the key released. The other is to move all codes back into the Actor subclasses and use 'isKeyDown' method with a boolean field instead of using the 'getKey' method (the 'getKey' method can only be used once per act cycle; this means you cannot use it in two different Actor subclasses at the same time).
You need to login to post a reply.