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

2015/12/19

checking for edge of object

davemib123 davemib123

2015/12/19

#
Hi guys, trying to check for edge of objects, this is what I have:
    private void checkEdge()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2);

        Actor edge = getOneObjectAtOffset(0, lookForGround, Brick.class);
        if (edge == null)
        {
            if (shared.facingRight == false) shared.facingRight = true;       
            else if(shared.facingRight == true) shared.facingRight = false;  
            System.out.println("no ground");
        }
        else
        {
            if (shared.facingRight == false) shared.facingRight = true;       
            else if(shared.facingRight == true) shared.facingRight = false; 
            System.out.println("ground");
        }
    }
Edit: I can get it to say ground or no ground but I am trying to switch direction based on the shared.facingRight variable. Where am I going wrong with it?
danpost danpost

2015/12/19

#
With the code given, it appears the 'facingRight' field will change its value when the actor has a brick at that offset. What other information can you give to help narrow it down? Like, this deals with movement (direction), but I do not see any code that deals with the actual movement of the actor here.
davemib123 davemib123

2015/12/20

#
this is what I have for movement in the act method of the current class:
if (shared.facingRight)
        {
            shared.moveRight(this);
        } 
        else 
        {  
            shared.moveLeft(this);
        }        
in the shared class I have:
    public int speed;
    public boolean facingRight;

    public void moveRight(Actor actor)
    {
        actor.setLocation(actor.getX() + speed, actor.getY());
    }

    public void moveLeft(Actor actor)
    {
        actor.setLocation(actor.getX() - speed, actor.getY());
    }
danpost danpost

2015/12/20

#
What class does the shared class extend and how does it relate to the actors?
davemib123 davemib123

2015/12/20

#
shared class does not extend anything. it relates to the other classes purely to share generic variables and methods at least thats my intention.
danpost danpost

2015/12/20

#
davemib123 wrote...
shared class does not extend anything. it relates to the other classes purely to share generic variables and methods at least thats my intention.
The methods are okay -- provided you make them 'static'. The fields, however, will not be given to each actor object like you intend unless you create a separate 'Shared' object for each actor.
davemib123 davemib123

2015/12/20

#
didnt think static is what is needed in this instance ... if I add this - it works for the left side
   private void checkEdge()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int)(spriteHeight/2);

        Actor edge = getOneObjectAtOffset(0, lookForGround, Brick.class);
        if (edge != null && shared.facingRight == false)
        {
            System.out.println("ground left");
            shared.facingRight = true; 
        }
//         else if (edge != null && shared.facingRight == true)
//         {
//             System.out.println("ground right");
//             shared.facingRight = false; 
//         }
       
    }
if I uncomment the right side code it just get stuck between the distance of 1 brick. I've uploaded a new version of the scenario here: http://www.greenfoot.org/scenarios/15669
danpost danpost

2015/12/21

#
danpost wrote...
The fields, however, will not be given to each actor object like you intend unless you create a separate 'Shared' object for each actor.
It appears you are creating seperate instances of SharedMembers objects for your actors. Remove the 'final' from your left facing images. Mirroring an image actually creates a different image object and cannot replace a constant field value.
davemib123 davemib123

2015/12/21

#
I can remove final. My fault, I wasn't clear. The object it relates to is goomba and brick.I was thinking of adding a transparent box, so when it intersects it, it will change direction. As the code that I have written checks individual objects not a group that is placed together. What do you think?
danpost danpost

2015/12/21

#
davemib123 wrote...
The object it relates to is goomba and brick.I was thinking of adding a transparent box, so when it intersects it, it will change direction. As the code that I have written checks individual objects not a group that is placed together. What do you think?
I think it would be better to have an intermediate subclass of ScrollingActor called Obstacle and have those "groups" subclass it. Then you can just check for 'Obstacle.class' objects.
davemib123 davemib123

2015/12/21

#
Makes sense I'll try that out :)
You need to login to post a reply.