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

2014/4/13

Help with automatic moving enemies??

jlee2014 jlee2014

2014/4/13

#
I'm trying to make a game that is a baby sea turtle trying to get to the sea while avoid sea gulls. But I have no idea how to make seagulls (multiple at different sizes and spots) generate automatically at the right end of the screen and move towards the left and then disappear once they hit the worlds edge. Any help is needed :/ it's for a project This is my seagull class so far: import greenfoot.*; import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Seagull extends Objects { Counter counter; public Seagull(Counter pointCounter) { counter = pointCounter; } public void act() { movement(); makeSeagull(); removeSeagull(); } private void movement() { int x = getX() - 5; int y = getY(); setRotation(getRotation() + 0); setLocation(x, y); checkCollision(); } public void makeSeagull() { World world = getWorld(); Seagull newSeagull = new Seagull(counter); int worldHeight = world.getHeight(); int worldWidth = world.getWidth(); if(Greenfoot.getRandomNumber(9999) < 9) { world.addObject(newSeagull, worldWidth, Greenfoot.getRandomNumber(worldHeight)); } } public void removeSeagull() { World world = getWorld(); Seagull newSeagull = new Seagull(counter); int worldHeight = world.getHeight(); int worldWidth = world.getWidth(); if(atWorldEdge()&& this != null) { world.removeObject(this); //world.addObject(newSeagull, worldWidth, Greenfoot.getRandomNumber(worldHeight)); if(Greenfoot.getRandomNumber(9999) < 9) world.addObject(newSeagull, worldWidth, Greenfoot.getRandomNumber(worldHeight)); } } public void checkCollision() { Actor a = getOneIntersectingObject(Turtle.class); if (a != null) { Greenfoot.stop(); } } }
danpost danpost

2014/4/13

#
One major flaw is that you are trying to control the number of Seagull objects from the Seagull class. A seagull should only move, check for collision and check for world edge. The world class should control the spawning of seagulls in your world (and it should be regulated by an 'if' statement or else your world will quckly become over-populated with them). The condition could be a random or set timer reaching zero; and it could limit the minimum and/or maximum number of seagulls in the world.
jlee2014 jlee2014

2014/4/15

#
Then how should I go about putting this code into my world class? I've tried but all I get is error after error
danpost danpost

2014/4/15

#
jlee2014 wrote...
Then how should I go about putting this code into my world class? I've tried but all I get is error after error
What have you tried? what errors are you getting?
jlee2014 jlee2014

2014/4/16

#
private void prepare() { Counter counter = new Counter(); //counter = pointCounter; addObject(counter, 24, 21); counter.setLocation(715, 566); Seagull newSeagull = new Seagull(); addObject(newSeagull, getWidth(), Greenfoot.getRandomNumber(560)); addObject(counter, 24, 21); counter.setLocation(715, 566); Seagull2 newSeagull2 = new Seagull2(counter); addObject(newSeagull2, getWidth(), Greenfoot.getRandomNumber(560)); if(Greenfoot.getRandomNumber(9999) < 9) { addObject(new Seagull(), 800, Greenfoot.getRandomNumber(560)); } if(atWorldEdge()&& this != null) { removeObject(Seagull); //world.addObject(newSeagull, worldWidth, Greenfoot.getRandomNumber(worldHeight)); if(Greenfoot.getRandomNumber(9999) < 9) addObject(newSeagull(), 800, Greenfoot.getRandomNumber(560)); }
jlee2014 jlee2014

2014/4/16

#
I've tried something like this but I keep getting errors. The atWorldsEdge is also giving me trouble in the world class
danpost danpost

2014/4/16

#
danpost wrote...
A seagull should only move, check for collision and check for world edge.
Please remove all duplicate code from your world class and move the 'atWorldEdge' checking to the Seagull and Seagull2 classes. Next, put your random spawn chance code in an 'act' method in the world class. Then, post your new world class using the 'code' link below the 'Post a reply' box to insert your code and ask your questions.
jlee2014 jlee2014

2014/4/16

#
First of all where should I move the 'atWorldEdge' and the Seagull and Seagull2 classes??
danpost danpost

2014/4/16

#
jlee2014 wrote...
First of all where should I move the 'atWorldEdge' and the Seagull and Seagull2 classes??
You mean 'where should I move the calling of 'atWorldEdge' to in the Seagull and Seagull2 classes?'. The act method of those two classes should have the code to remove them if 'atWorldEdge'.
1
2
3
4
5
6
public void act()
{
    // move
    // check collision
    if (atWorldEdge()) getWorld().removeObject(this);
}
You need to login to post a reply.