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

2012/6/6

Actor moves to mouse-clicked position, but sometimes he stops moving.

DMKxD DMKxD

2012/6/6

#
Hey Community! I'm working on an RPG game atm, and i want the actor to move to the mouse clicked position. Sometimes it works fine, but sometimes he stops before reaching the target or he can't move after reaching the target. This is my code so far, hope you could help me. cya DMKxD
private void Steuerung()
    {   
        if (status == STATUS_NORMAL) 
        {  
            if (Greenfoot.mouseClicked(null))   
            {
                zielX = 0;
                zielY = 0;
                NeuesZiel();
                status = STATUS_BEWGUNG;  
            }    
        }    
        else if (status == STATUS_BEWGUNG) 
        {
            Bewegung();
        }
        if(Greenfoot.mouseClicked(Bots.class))
        {
            angreifen = true;
        }
        else
        {
            angreifen = false;
        }
    }

    public void NeuesZiel()  
    {  
        MouseInfo mouse = Greenfoot.getMouseInfo();  
        zielX = mouse.getX();  
        zielY = mouse.getY();  
    } 

    public void Bewegung()  
    {
        if (zielY != getY() && zielX != getY())
        {
            setRotation((int)(180*Math.atan2(zielY-getY(),zielX-getX())/Math.PI));
            move(3);
        }
        else
        {
            status = STATUS_NORMAL;
        }
    }
Screenshot:
danpost danpost

2012/6/7

#
From the code, I would say that the actor stops when either it is at the right horizontal of the target or the right vertical of the target. On line 36 replace '&&' with '||'. Test it, and report back.
danpost danpost

2012/6/7

#
Another issue may be that you are moving 3 steps at a time, so your actor could end up jumping back and forth over the target and never actually get there. Try this for your 'Bewegung' method:
public void Bewegung()
{
    if (zielX != getX() || zielY != getY())
    {
        if (Math.abs(zielX - getX()) < 3 && Math.abs(zielY - getY()) < 3)
        {
            setLocation(zielX, zielY);
        }
        else
        {
            setRotation((int) (180*Math.atan2(zielY - getY(), zielX - getX()) / Math.PI));
            move(3);
        }
        if (zielX == getX() && zielY == getY()) status = STATUS_NORMAL;
    }
}
DMKxD DMKxD

2012/6/7

#
Ty danpost, now the actor reachs the target every time, but sometimes when i click on another location after he reachs the target, he dont moves. cya DMKxD
You need to login to post a reply.