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

2013/12/13

Fishing Game

BKWaka BKWaka

2013/12/13

#
I'm new to programming and am trying to figure out how make a fishing game, so far i have fish that swim back and forth and a hook with keyboard controls to make left and right, and drop down.I'm trying to make it so that when the hook touches a fish, the fish turns upward and follows the location of the hook. I'm trying to do this in the fish's code. public class yellowFish extends animal { /** * Act - do whatever the redlobster wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ //private int hookY = hook.class.getY(); //private int hookX = hook.class.getX(); public void act() { turnAtEdge(); move(4); checkCollision(); } public void turnAtEdge() { if ( atWorldEdge() ) { turn(180); } } private void checkCollision() { Actor actor = getOneIntersectingObject(null); if(actor instanceof hook) { setRotation(270); getX(); getY(); //setLocation(hookX, hookY); } } }
danpost danpost

2013/12/13

#
First off, remove these lines:
//private int hookY = hook.class.getY();  
//private int hookX = hook.class.getX();
You cannot use 'getX' and 'getY' there. Not only does the object have to already be created, but it must also be added to the world before these methods can return anything meaningful. In your checkCollision method, after the local field 'actor' is assign a valid actor (that means it cannot be null), you can use 'actor.getX()' and 'actor.getY()' to get the location of the hook. You should probably add an instance boolean field called 'hooked' to the fish class to be used to determine if the fish has been hooked or not. Then, only 'move' and 'checkCollision' if not hooked yet; and if hooked, add code to stay with the hook (the 'checkCollision' method does not have to do that part -- it only needs to set the boolean field to 'true' when hooked).
danpost danpost

2013/12/13

#
On second thought, instead of a boolean field, use an Actor field and if the field is 'null', the fish is not hooked; if the field has a hook object then follow the object. Set the field to the hook when being hooked.
// instance field
Actor hook;
// in act
if (hook == null)
{
    move(4);
    checkCollision();
}
else
{
    setLocation(hook.getX(), hook.getY());
}
// the 'checkCollision' method
private void checkCollision()
{
    hook = getOneIntersectingObject(hook.class);
    if (hook != null) setRotation(270);
}
BKWaka BKWaka

2013/12/14

#
You sir are a genious, thank you so much! This really helped
You need to login to post a reply.