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

2012/4/16

How do I make an object follow another?

h123n h123n

2012/4/16

#
I am making a game where you control a bee and when you press the up key a butterfly apears to wherever the bee is. but it doesn't know the method Greenfoot.getX() or Greenfoot.getY(). Here is my code; import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class bee here. * * @author (your name) * @version (a version number or a date) */ public class bee extends Actor { /** * Act - do whatever the bee wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(4); if (Greenfoot.isKeyDown ("left")) { turn(-4); } if (Greenfoot.isKeyDown ("right")) { turn(4); } if (Greenfoot.isKeyDown ("up")) { World world; world = getWorld(); world.addObject (new butterfly(), Greenfoot.getX(), Greenfoot.getY()); } } }
nccb nccb

2012/4/16

#
getX() and getY() are methods of the bee actor, so you don't need the Greenfoot. prefix. Change your code to:
world.addObject (new butterfly(), getX(), getY());
h123n h123n

2012/4/16

#
Thanks so much!!! The only reason i did greenfoot. and then getY/X() was because of a friend. Well I proved him wrong.
You need to login to post a reply.