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

2012/8/4

Enemie code?

1
2
3
4
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
if you've played my new scenario ( http://www.greenfoot.org/scenarios/5762 ), then you know that ive been working on a new game. Ive encountered a issue pretty early in the dev stage. the plan is to have a 1000 by 1000 travelable world and enemies randomly spawn in the world. i cannot figure out how to make the enemies shoot ONLY when they see my character.
MatheMagician MatheMagician

2012/8/4

#
You could replace the place in your code where you tell the enemy to shoot from something like this:
shoot();
With:
if(getObjectsInRange(100,true,Rocket.class))
{
           shoot();
}
This code makes it so that the enemy will only shoot when the spaceship is within 100 cells of it.
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
thanks. ive been waiting all morning for someone to answer this. and i was wondering, should i make the enemies fly randomly or towards the player?
SPower SPower

2012/8/4

#
@MatheMagician this:
if(getObjectsInRange(100,true,Rocket.class))  
{  
           shoot();  
} 
won't work, since you have to do something like this:
if(!getObjectsInRange(100,true,Rocket.class).isEmpty()) {  
           shoot();  
} 
but still, I don't think that will work @CrazyGamer1122 What do you use for making the world actually 1000 by 1000 points big? Do you use the Greenfoot API, or have you done it yourself?
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
the beggining of world code should look like this for 1000 by 1000.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(1000, 1000, 1);  //originally 600 by 400, changed.
     }
MatheMagician MatheMagician

2012/8/4

#
Thank you SPower for the correction. @crazy gamer, a combination of the two would be nice. i.e. if the rocket gets really close, then the enemy could start following him, i.e. the code would look something like.
if(getObjectsInRange(100,false,Rocket.class).size() > 0)
    {
        List<Rocket> ships = getObjectsInRange(100,false,Rocket.class);
        Actor thing = ships.get(0);
        turnTowards(thing.getX(),thing.getY());
    }
else 
{
       moveRandom();
}
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
i tried both of those codes and i got the same error.
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
CrazyGamer1122 wrote...
i tried both of those codes and i got the same error.
nevermind, mathemagician's new code hadn't loaded yet.
MatheMagician MatheMagician

2012/8/4

#
Now CrazyGamer, I am also curious about the scrolling effect. If you are using something like my scrolling demo where you set the world to something like (600,400,false), then it should work fine. However, it might not work in other methods of scrolling.
SPower SPower

2012/8/4

#
I can explain you if you need more help, but davmac has a useful scenario: http://www.greenfoot.org/scenarios/1841 you might also look at that, for 'hiding' enemies outside the world.
SPower SPower

2012/8/4

#
Shoot, forgot it has no source :(. What you need to do is change this:
super(1000, 1000, 1);
to this:
super(1000, 1000, 1, false);
That will make it possible to set the location of an actor, to one which is outside the world.
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
MatheMagician wrote...
Thank you SPower for the correction. @crazy gamer, a combination of the two would be nice. i.e. if the rocket gets really close, then the enemy could start following him, i.e. the code would look something like.
if(getObjectsInRange(100,false,Rocket.class).size() > 0)
    {
        List<Rocket> ships = getObjectsInRange(100,false,Rocket.class);
        Actor thing = ships.get(0);
        turnTowards(thing.getX(),thing.getY());
    }
else 
{
       moveRandom();
}
i tried this and it says "Method getObjectInRange in class greenfoot.Actor cannot be applied to given types;"
SPower SPower

2012/8/4

#
@CrazyGamer Try to import:
import java.util.List;
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
into the enemie or world?
SPower SPower

2012/8/4

#
Where this code is:
if(getObjectsInRange(100,false,Rocket.class).size() > 0)  
    {
//....
There are more replies on the next page.
1
2
3
4