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

2018/2/18

Object Switch

JosephTheVA JosephTheVA

2018/2/18

#
If I wanted two objects to switch positions with each other, what kind of code would be needed for that?
danpost danpost

2018/2/18

#
You would just need to save the location coordinates of one of them in local variables (declared within the method). Then move it to the location of the other and then move the other to the saved coordinates.
JosephTheVA JosephTheVA

2018/2/19

#
        if(Greenfoot.mouseClicked(Jewel.class))
        {
            int firstX = getX();
            int firstY = getY();
            if(Greenfoot.mouseClicked(this))
            {
                setLocation(firstX,firstY);
            }
        }
This is my current code that doesn't work
danpost danpost

2018/2/19

#
Of course that will not work. The Jewel object clicked must be this if both 'if' conditions are to be true and therefore you are setting the actor's location to where it already is. You need another object to switch it with -- not the same object. What exactly are you attempting to implement? How are the two objects that are to switch to be identified by the user?
You need to login to post a reply.