Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Teleporting relative to current position reveals that getX(), getY() not queued properly #1

Open
Zachary3352 opened this issue Sep 14, 2018 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Zachary3352
Copy link

Rather than teleporting from the location where the turtle will be in the queue, the turtle teleports relative to its original position.

@battis battis changed the title Teleports are not queued properly for AnimatedTurtles Teleporting relative to current position reveals that getX(), getY() not queued properly Sep 15, 2018
@battis
Copy link
Member

battis commented Sep 15, 2018

So... this proves to be a more subtle problem than @Zachary3352 and I initially thought. Let me start by embedding his code that caused the error:

import org.gannacademy.cdf.turtlelogo.AnimatedTurtle;
import org.gannacademy.cdf.turtlelogo.Turtle;

public class AnimatedTurtlesBotchThis {
    public static void triangle(Turtle t) {
        t.turn(-60);
        t.fd(20);
        t.turn(120);
        t.fd(20);
        t.turn(120);
        t.fd(20);
        t.turn(180);
        t.teleport(t.getX()+50,t.getY());
    }

    public static void main(String[] args) {
        Turtle john = new AnimatedTurtle();
        john.teleport(30,30);
        triangle(john);
        triangle(john);
    }
}

The desired outcome (which appears if using a regular Turtle) is two triangles side-by-side in the top-left corner of the canvas. The result, with the AnimatedTurtle is one triangle in the top-left corner (the first call to triangle()) and one triangle in the center of the canvas, the second call to turtle().

The issue is that the coordinates that are passed to teleport are based on a call to Turtle.getX(), and, because the queue is built before the turtle starts moving, the coordinates returned are those of the turtle in its initial position. Which means that the call to t.teleport(t.getX()+50,t.getY() in the final line of triangle(), rather than being relative to the turtle location at the end of the triangle drawing, is relative to the turtle's original position.

The first call to teleport() in main() works as expected because it uses literal coordinate values.

I shall have to ponder how best to address this problem!

@battis battis self-assigned this Sep 15, 2018
@battis battis added the bug Something isn't working label Sep 15, 2018
battis added a commit that referenced this issue Oct 14, 2020
As we are not shocked to discover, sorting through the AnimatedTurtle teleport bug #1 is an exercise in concurrency. It surfaced some bad behavior on my part, since cleaned up, in terms of instantiating the app frame. Empirically, things seem to be working, but there's a FIXME on `getX()` that I want to reason with before committing to being done with this problem.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants