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

2014/6/7

Can not longer place my units.

1
2
3
CalzoneCannon CalzoneCannon

2014/6/11

#
Thanks, I got that all fixed. I have a new question. Do you know how to make an actor move to a certain location on the screen? If you have played Plants Vs Zombies, you know that suns will fall from the sky, and when you tap them, they will move across the screen to the "sun bank". The suns are at random X locations when they spawn, but I want them to move to one certain spot when they are clicked on.
danpost danpost

2014/6/11

#
You will need a boolean field in the Sun class to indicate whether it was clicked on or not (I will use 'clickedOn'). Then:
if (!clickedOn && Greenfoot.mouseClicked(this)) clickedOn = true;
if (clickedOn && (getX() != targetX || getY() != targetY))
{
    if (/*within one move from target */)
    {
        setLocation(targetX, targetY);
    }
    else
    {
        // move toward target
    ]
}
I included the 'within one move' thing in case you were moving the suns more than one pixel per move and, therefore, they could just jump right passed the target location (never actually be at it).
CalzoneCannon CalzoneCannon

2014/6/12

#
Ok, got that all working. Now my code keeps crashing when it spawns a sun, saying there is a nullpointer exception on setImage(gif.getCurrentImage()); I'm not sure why it is doing that though
danpost danpost

2014/6/12

#
You need to post your error message (copy/paste it) and show the Sun class (if that is indeed where the error is occurring).
You need to login to post a reply.
1
2
3