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

2014/12/6

Making objects move problem

bigdaddyrooster2 bigdaddyrooster2

2014/12/6

#
Basically what I'm trying to do is make a simple football game where the user drags the football, and when they release it the ball should move towards the goals. This is the code I have so far:
public void act() 
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        boolean s = false;
        if(Greenfoot.mouseDragged(this))
        {
            this.setLocation(mouse.getX(), mouse.getY());
        }
        if(Greenfoot.mouseDragEnded(this))
        {
            s = true;
        }
        while (s == true)
        {
            Goals g = (Goals) getWorld().getObjects(Goals.class).get(0);
            turnTowards(g.getX(), g.getY());
            shoot();
            if((this.getX() == g.getX()) && (this.getY() == g.getY()))
            {
                s = false;
            }
        }

    }

    public void shoot()  
    {  
        move(2);    
    }  

}
The problem is that when I release the ball, it just stays there for a while before suddenly jumping towards the goals. Does anyone know a way that I can make the ball look like it is moving towards the goals? Many thanks in advance.
danpost danpost

2014/12/6

#
Change 'while' to 'if' on line 13. You do not want to entire sequence of actions done in one act cycle; but through repeated act cycles.
bigdaddyrooster2 bigdaddyrooster2

2014/12/6

#
danpost wrote...
Change 'while' to 'if' on line 13. You do not want to entire sequence of actions done in one act cycle; but through repeated act cycles.
Thanks for the reply. However, when I do that it seems to just go through the code once i.e it only moves a tiny bit.
danpost danpost

2014/12/6

#
Yeah. well your boolean s field needs to be declared outside the method to make it more persistent. Right now, you are declaring it each act cycle and its initial value will be false.. Moving it outside, it will continue to stay true until the goal is reached.
bigdaddyrooster2 bigdaddyrooster2

2014/12/6

#
Thanks a million man, that fixes it.
You need to login to post a reply.