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

2014/3/28

Change center of an actor for getObjectsInRange method

Jonche Jonche

2014/3/28

#
Hello, I need some help since I want to make blocks for a human actor(Meaning that I have a sprite that looks like a human). For I want to add blocks, I created a method that doesn't allow me to move through objects :
public int checkWhereCannotMoveX()    
    {
        List <Block> nearObstacle = getObjectsInRange(60, Block.class);
        int value = 0;
        
        Block nearestObstacle = null;
        double nearestDistance = 60;
        double distance; 
        for (int i = 0; i < nearObstacle.size(); i++)
        {
            distance = getDistance(nearObstacle.get(i));
            if (distance < nearestDistance)
            {
                nearestObstacle = nearObstacle.get(i);
                nearestDistance = distance;
            }
            
        }
         if(nearestObstacle != null){
         Block block = nearestObstacle;
         int blockX = block.getX() - getX();
         int blockY = block.getY() - getY();
         if(blockX < 35 && blockX > 0){if(blockY < 60 && blockY > -10){value = 1;}}
         else if(blockX > -35 && blockX < 0){if(blockY < 60 && blockY > -10){value = 2;}}
        }
        return value;
    }
I've got the same method for finding Y coordinates of the block but there is a difference : For I want to compare the distance between the actor's feet (25px beneath the original center of the sprite) I would like to change the center for the getObjectsInRange method. Thank you in advance!
davmac davmac

2014/3/28

#
One way to achieve this is to move your human actor, call getObjectsInRange, and then move your human back to the original position.
You need to login to post a reply.