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

2017/2/1

Set target location for an Actor in a Scrolling World

Super_Hippo Super_Hippo

2017/2/1

#
Hi danpost, I downloaded your Scrolling SuperWorld scenario and I am building some game. I have a unit which I want to move with my mouse. I tried to save the coordinates of the mouse when it is clicked and let the unit move to the target location. The problem is that when the world is scrolled, the unit's target location does not move with it. I just started, so this is the code so far. (No un-selecting and no un-targeting, but that doesn't matter right now.) Before I mess around and complicate your neat code when trying to implement that, I wanted to ask you if you already know an easy method to make it work.
    private boolean selected;
    private boolean hasATarget = false;
    private int targetX = 0, targetY = 0;

    public void act() 
    {
        if (Greenfoot.mousePressed(this))
        {
            selected = true;
        }
        
        if (selected)
        {
            if (Greenfoot.mousePressed(getWorld()))
            {
                MouseInfo me = Greenfoot.getMouseInfo();
                targetX = me.getX();
                targetY = me.getY();
                hasATarget = true;
            }
        }
        
        if (hasATarget)
        {
            turnTowards(targetX, targetY);
            move(2);
        }
    }
danpost danpost

2017/2/2

#
I think you will need to save the universal coordinates of the target -- not the screen coordinates. and then turn towards the screen coordinates minus the amount scrolled (which could lead to an off-screen location after some scrolling. I will be back to my normal routine here starting Saturday. Hope this helped.
Super_Hippo Super_Hippo

2017/2/2

#
Great, I just needed to change line 17, 18 and 25 without adding any method or anything. Thank you very much!
targetX = me.getX()+((Welt)getWorld()).getScrolledX();
targetY = me.getY()+((Welt)getWorld()).getScrolledY();

turnTowards(targetX-((Welt)getWorld()).getScrolledX(), targetY-((Welt)getWorld()).getScrolledY());
You need to login to post a reply.