I'm trying to get a baby to kick a ball into the goal. So far I have managed to get the ball to go into the goal when the baby touches it by using move(-300); to go into the desired position, but that isn't technically getting the baby to hit the ball into the goal, because the baby can hit it from any direction and it will go into the goal.
I would just like to know the right code to use, I don't need to know the exact solution; I can learn the code and work it out myself but I have literally no idea where to start. I struggle quite a lot with this particular type of language so my last resort is to ask online.
Thanks for any help and please comment for any additional information.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ball extends Actor
{
/**
* Act - do whatever the ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
playerHitIt();
}
public void playerHitIt()
{
Actor player;
player=getOneObjectAtOffset(0,0,player.class);
if (player !=null)
{
setLocation(getX()-4, getY());
}
}
}
You could start by asking what a
Ball
is. It's probably not something that shouldact()
* and haveplayerHitIt()
at least unqualifed by which way it was hit. It probably has a field(s) representing position, and probably a velocity. If you consult theActor
API, you'll see the ball has the built-in capacity to do probably all you want because of extendingActor
. This and other greenfoot documentation is the place to start. Surely the player is or is controlling anActor
object with its own direction as well, and kicking happens when they intersect.act(){}