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.
I'd be glad if anyone could help me!
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()); } } } |