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

2016/2/8

Problem with collision

Manchester19 Manchester19

2016/2/8

#
Hi everyone, i am trying to make a bomberman with a friend but we have a problem with the collision. Here is the code for player 2:

int a                   = 650/13;
int b                   = a/2;

public void playerMove(){
     
    
     if(Greenfoot.isKeyDown("right")){
         Actor destrSt = getOneObjectAtOffset(b, 0, DestrSteel.class);
         Actor indestrSt = getOneObjectAtOffset(b, 0, IndestrSteel.class);
         if(destrSt == null && indestrSt == null){
             moveRight();
             animateRight();
         }
        }
        
     if(Greenfoot.isKeyDown("left")){
         Actor destrSt = getOneObjectAtOffset(-b-5, 0, DestrSteel.class);
         Actor indestrSt = getOneObjectAtOffset(-b-5, 0, IndestrSteel.class);
            if(destrSt == null && indestrSt == null){
                moveLeft();
               animateLeft();
            }
        
        }
      
     if(Greenfoot.isKeyDown("up")){
         Actor destrSt = getOneObjectAtOffset(0, -5, DestrSteel.class);
         Actor indestrSt = getOneObjectAtOffset(0, -5, IndestrSteel.class);
            if(destrSt == null && indestrSt == null){
                moveUp();
               animateUp();
            }
        }
        
     if(Greenfoot.isKeyDown("down")){
         Actor destrSt = getOneObjectAtOffset(0, a, DestrSteel.class);
         Actor indestrSt = getOneObjectAtOffset(0, a, IndestrSteel.class);
           if(destrSt == null && indestrSt == null){
               moveDown();
               animateDown();
            }
            
        }
    }
The player in green goes in the block and doesn't stop in front of it. Here you can see where it bug:
danpost danpost

2016/2/8

#
I suggest you use the 'getOneIntersectingObject' method instead of the 'getOneObjectAtOffset' method. The one you are using is generally for gridded scenario (worlds whose cellsize is greater than one and generally one whose actors are each contained within a single cell). It is either that or restrict turning to when at the center of your mapped cells and only check for object at the offsets then.
Manchester19 Manchester19

2016/2/9

#
Thanks for your answer danpost. I've tried like you said but the actor won't move. I thought it's because the head of the player2 is touching the IndestrSteel class but even if i reduce the image of the player2 it does the same. The player2 doesn't move.
danpost danpost

2016/2/9

#
Manchester19 wrote...
Thanks for your answer danpost. I've tried like you said but the actor won't move. I thought it's because the head of the player2 is touching the IndestrSteel class but even if i reduce the image of the player2 it does the same. The player2 doesn't move.
Please show what you tried or upload the scenario with source code included so that we can help.
You need to login to post a reply.