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

2016/5/21

Killing Off Mobs (a.k.a. Making the Stupid Enemy Sprites Go Away)

ZacBuzzsaw ZacBuzzsaw

2016/5/21

#
Me. Again. Surprise. Like I haven't had to ask for help three other times for the same stupid game. If there was ever a doubt that technology hates me, I'll show them every single problem I've had on this scenario that has forced me to "phone a friend." This time, though, it's even more infuriating, because I don't even know which problem to start with. I've got a Shooter class that utilizes the following code:
public void act(){
if(Greenfoot.isKeyDown("space"))
        {
            timer = System.currentTimeMillis();//Time in ms
            hasShot = true;
            if(usingPistol)
            {
                if(bulletsShot == 1)
                {
                    this.setImage(shootPistol);
                    Background.addObject(new Bullet(), this.getX(), this.getY());
//rest of code follows
} } } The problem is that addObject, apparently, can't be referenced from a static context, because it's a non-static method. Furthermore, when I try to make a Bullet actor go away after it hits an opponent, Greenfoot says that it can't find the Bullet variable, even though Bullet is a class (or Actor). Here's the code I'm using:
/*all previous code does NOT relate to the current problem*/
public void act() 
       if(this.isTouching(Bullet))
        {
            this.removeTouching(Bullet);
            health--;
         }
//rest of code follows
Final question in this discussion: I've got three spawning locations for both mobs and weapon pickups (makes programming a first-person flash shooter much easier), but the actors LOVE to spawn on each other. How do I ensure that nothing spawns on top of something else?
boolean spriteOnLeft = false;
boolean spriteInMid = false;
boolean spriteOnRight = false; //All false because nothing spawns immediately

if(count >= 100)
        {
            int spriteSpawned = Greenfoot.getRandomNumber(6); //Decides what to spawn: some type of enemy or a weapon pickup
            int spawnLocation = Greenfoot.getRandomNumber(3); //chooses one of the three locations to place said actor
            if(spriteSpawned == 0) {
                if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            else if(spriteSpawned == 1){
               if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            else if (spriteSpawned == 2){
               if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            else if (spriteSpawned == 3){
                 if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            else if(spriteSpawned == 4){
                 if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            else
            {
               if(spawnLocation == 0 && spriteOnLeft == false)
                {
                    this.addObject(new Grunt(), 125, 175);
                    spriteOnLeft = true;
                }
                else if(spawnLocation == 1 && spriteInMid == false)
                {
                    this.addObject(new Grunt(), 300, 175);
                    spriteInMid = true;
                }
                else if(spawnLocation == 2 && spriteOnRight == false)
                {
                    this.addObject(new Grunt(), 475, 175);
                    spriteOnRight = true;
                }
            }
            count = 0;
        }
danpost danpost

2016/5/21

#
ZacBuzzsaw wrote...
The problem is that addObject, apparently, can't be referenced from a static context, because it's a non-static method.
The Actor class has a 'getWorld' method which returns the World object the shooter is in (the Background world object). So you can just use:
getWorld().addObject(...)
Bullet is a class (or Actor)
Bullet is indeed a class -- and Actor is a class. The Actor class is not a Bullet class -- and Bullet is not Actor. The Bullet class can be a subclass of the Actor class -- but none of this is related to any actors (or Actor objects) in your world. An Actor object is any object created from the Actor class or any class that extends from it and you cannot refer to any object just by referencing the name of the class it was created from. At any rate, what is required in the 'isTouching' and 'removeTouching' parameters is a reference to a class. Not the class name by itself; but, the class name followed by '.class'. For example:
if (isTouching(Bullet.class))
danpost danpost

2016/5/22

#
As far as actors spawning atop one another, the code given above looks awfully bulky; although, it would appear to do the trick as far as keeping them from spawning atop one another. Where exactly are you declaring the variables given ('spriteOnLeft', 'spriteInMid' and 'spriteOnRight')? With a little organization, the bulkiness can be practically eliminated. Using a 'switch' structure (or one-line 'if' statements) to create the appropriate actor to spawn would eliminate the need for multiple 'if' structures for placing the actor properly. Using an int array to hold the x-coordinates that an actor can be spawned at would help to cycle through them as each location is found to be occupied. These two thing alone will eliminate all seeming repeating code structures in your listing above. Also, the boolean fields are being used for something that can be determined without them (and there is chance for incorrect values in the variables as compared to actual state of the world). The following example show how reduced the code could be:
// instance field
private int[] spawnX = { 125, 300, 475 };

// in act
count++;
if (count == 100)
{
    int actorID = Greenfoot.getRandomNumber(6);
    Actor actor = null;
    if (actorID == 0) actor = new Grunt();
    if (actorID == 1) actor = new Grunt();
    if (actorID == 2) actor = new Grunt();
    if (actorID == 3) actor = new Grunt();
    if (actorID == 4) actor = new Grunt();
    if (actorID == 5) actor = new Grunt();
    int locations = spawnX.length;
    int locactionID = Greenfoot.getRandomNumber(locations);
    for (int i=0; i<locations; i++)
    {
        int x = spawnX[(locationID+i)%locations];
        if (getObjectsAt(x, 175).isEmpty())
        {
            addObject(actor, x, 175);
            break;
        }
    }
    count = 0;
}
You need to login to post a reply.