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

2016/1/27

Is it possible to transfer a object reference through a method?

TomazVDSN TomazVDSN

2016/1/27

#
Hi friends, I just shared my last simulation called Bumptest03 , and a don't know how to transfer a object reference using a method. In my code I check the collision of the actor against the other Block, however I would like to define what Block it should check the collision. So instead of this...
public void checkCollision()
    {
        Actor actor = this.getOneIntersectingObject(GreenBlock.class); 
        if (actor != null)
        {
            moveWhenPushedBy();
        }
    }
I would like to have something like this...
public void checkCollision(<NAME OF THE OBJECT>)
    {
        Actor actor = this.getOneIntersectingObject(<NAME OF THE OBJECT>); 
        if (actor != null)
        {
            moveWhenPushedBy();
        }
    }
Thanks in advance ! Tomaz
TomazVDSN TomazVDSN

2016/1/27

#
Friends, i think I found it.
public void checkCollision(Class myObject)
    {
        Actor actor = this.getOneIntersectingObject(myObject); 
        if (actor != null)
        {
            moveWhenPushedBy();
        }
    }
TomazVDSN TomazVDSN

2016/1/27

#
Now I have other problem,
public void moveWhenPushedBy()
    {
       Object blockInTheWorld= getWorld().getObjects(GreenBlock.class).get(0);
       GreenBlock gB = (GreenBlock) blockInTheWorld;
       ....
public void moveWhenPushedBy(myObject)
    {
       Object blockInTheWorld= getWorld().getObjects(myObject).get(0);
       // GreenBlock gB = (GreenBlock) blockInTheWorld;
           myObject aB = (myObject) blockInTheWorld; 
       ....
Anybody can help me please ? Regards, Tomaz
danpost danpost

2016/1/27

#
I think you want to pass the actor to the 'moveWhenPushedBy' method (not the class):
// called with
moveWhenPushedBy(actor);

// start of method
public void moveWhenPushedBy(Actor actor)
{
    ...
TomazVDSN TomazVDSN

2016/1/27

#
danpost, I have the code working as below but I am not happy about it. Could you help me please, to improve it?
public void checkCollision(Class myObject)
    {
        Actor actor = this.getOneIntersectingObject(myObject); 
        if (actor != null)
        {
            moveWhenPushedBy(myObject);
        }

    }

    public void moveWhenPushedBy(Class myObject)
    {
        Object blockInTheWorld= getWorld().getObjects(myObject).get(0);

        int otherBlockCenterX;
        int otherBlockCenterY;
        
        if(myObject == GreenBlock.class)
        {
            GreenBlock gB = (GreenBlock) blockInTheWorld;
            otherBlockCenterX = gB.getX();
            otherBlockCenterY = gB.getY();
        }
        else
        {
            RedBlock rB = (RedBlock) blockInTheWorld;
            otherBlockCenterX = rB.getX();
            otherBlockCenterY = rB.getY();
        }

        int otherBlockLeftEdge = otherBlockCenterX-35;
        int otherBlockRightEdge = otherBlockCenterX +35;
        int otherBlockUpperEdge = otherBlockCenterY-35;
        int otherBlockBottomEdge = otherBlockCenterY +35;

        int thisBlockCenterX = this.getX();
        int thisBlockCenterY = this.getY();
        int thisBlockLeftEdge = thisBlockCenterX-35;
        int thisBlockRightEdge = thisBlockCenterX +35;
        int thisBlockUpperEdge = thisBlockCenterY-35;
        int thisBlockBottomEdge = thisBlockCenterY +35;

        if(otherBlockRightEdge < thisBlockLeftEdge )
        {this.moveIt("right");}

        if(otherBlockLeftEdge > thisBlockRightEdge )
        {this.moveIt("left");}

        if(otherBlockUpperEdge < thisBlockBottomEdge )
        {this.moveIt("down");}

        if(otherBlockBottomEdge > thisBlockUpperEdge )
        {this.moveIt("up");}

    }
}
I don't know what to do with the Actor you suggested. Regards, Tomaz
TomazVDSN TomazVDSN

2016/1/27

#
I would like a way to make this part of the code able to deal with any object that is passed.
public void moveWhenPushedBy(Class myObject)
    {
        Object blockInTheWorld= getWorld().getObjects(myObject).get(0);
 
        int otherBlockCenterX;
        int otherBlockCenterY;
         
        if(myObject == GreenBlock.class)
        {
            GreenBlock gB = (GreenBlock) blockInTheWorld;
            otherBlockCenterX = gB.getX();
            otherBlockCenterY = gB.getY();
I want to instantiate and cast the object with the "class"that is passed.
TomazVDSN TomazVDSN

2016/1/27

#
danpost, I think with your help, I figured it out.
public void moveWhenPushedBy(Actor actor)
    {
        int otherBlockCenterX=actor.getX();
        int otherBlockCenterY=actor.getY();
It is working ! - Thanks !
You need to login to post a reply.