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

2017/7/19

Help with swapping the location of two actors

ParkoPlays ParkoPlays

2017/7/19

#
I am attempting to make a program and I'm going well, but have run into an issue. At some point I would like the user to be able to swap the postitions of two different images. I am attempting a simplified version of the issue now and have gotten stuck The following code is in the actor "one". What I want it to do is to get the location of itself as well as an actor named "two", and swap them.
 if (Greenfoot.mouseClicked(this))
        {
            Actor two = getWorld().getObjects(two.class).get(0);
            int oneX = this.getX();
            int oneY = this.getY();
            int twoX = two.getX();
            int twoY = two.getY();
            two.setLocation(oneX,oneY);
            this.setLocation(twoX,twoY);
        }
As of now all the code does is move actor "one" to actor "two", so the two are on top of each other.
danpost danpost

2017/7/20

#
The given code should do exactly what you want. My suspicion is that some other code, probably in the two class, is causing the unwanted behavior. If you cannot solve the issue, maybe posting the entire code for both the one and two classes would allow someone here to figure it out.
ParkoPlays ParkoPlays

2017/7/20

#
Thanks for replying so quickly! I found about 3 lines of code in the "two" class left over from a previous attempt. I removed the code and it works perfectly! Once again thank you!
You need to login to post a reply.