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

2020/10/15

Object won't get removed.

ItzLukeStorm ItzLukeStorm

2020/10/15

#
So my object won't leave the screen after it does its animation. This is just the problem part of the code. Could someone help me?
if (getWorld() !=null)
        {
            if(isTouching(Mine.class))
            {
                if(count % 5 == 0)
                {
                    death++;
                    if (death == 5)
                    {
                        death = 1;
                        getWorld().removeObject(this);
                    }
                }
                setImage("Death" + death + ".png");
                count++;
            }
        }
danpost danpost

2020/10/15

#
More scope (code) needed. Please provide entire class (full page). May also need Mine class.
ItzLukeStorm ItzLukeStorm

2020/10/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    private int runner = 1;
    
    private int death = 1;
    
    private int still = 1;
    
    private int count = 0;
    
    public void act() 
    {
        border();
        death();
        menu();
        changeWorld();
    }
    
    public void changeWorld()
    {
        if (getWorld() !=null)
        {
            if (isTouching(Border.class))
            {
                Lose lose = new Lose();
                Greenfoot.setWorld(lose);
            }
        }
    }
    
    public void menu()
    {
        if (getWorld() !=null)
        {
            if(isTouching(MainMenu.class))
            {
                getWorld().removeObject(this);
            }
        }
    }
    
    public void death()
    {
        if (getWorld() !=null)
        {
            if(isTouching(Mine.class))
            {
                if(count % 5 == 0)
                {
                    death++;
                    if (death == 5)
                    {
                        death = 1;
                        getWorld().removeObject(this);
                    }
                }
                setImage("Death" + death + ".png");
                count++;
            }
        }
    }

    public void border()
    {
        if(isTouching(Border.class))
        {
            if(count % 5 == 0)
            {
                still++;
                if (still == 5)
                {
                    still = 1;
                }
            }
            setImage("Still" + still + ".png");
            count++;
        }
    }
   
    
    public void move()
    {
            if(getX() < 650)
        {
           setLocation(getX() + 1, getY());
            
            if(count % 5 == 0)
           {
               runner++;
               if (runner == 8)
               {
                   runner = 1;
               }
           }
           setImage("Move" + runner + ".png");
           count++;
        }
    }
}
danpost danpost

2020/10/15

#
danpost wrote...
May also need Mine class.
^^ Will ^^
ItzLukeStorm ItzLukeStorm

2020/10/15

#
import greenfoot.*; public class Mine extends Actor { public void act() { touch(); } public void touch() { if (isTouching(Zombie.class)) { Greenfoot.playSound("Blast.mp3"); getWorld().removeObject(this); } else { Greenfoot.playSound("Blast.mp3"); getWorld().removeObject(this); } } }
ItzLukeStorm ItzLukeStorm

2020/10/15

#
I just call it mine, it will be a shotgun/knife.
danpost danpost

2020/10/15

#
Mine will only touch zombie for one act cycle. So, nothing inside if block starting line 54 in Zombie class will execute more than one time (meaning count will only count mines touched -- that is, it will have to touch 5 mines before zombie is removed).
ItzLukeStorm ItzLukeStorm

2020/10/15

#
Does that mean I have to make Mine stay in for all 5 cycles?
danpost danpost

2020/10/15

#
It is always best to only have one type check for touching another. Have only zombie check for touching mine. Use removeTouching and play sound there.
danpost danpost

2020/10/15

#
ItzLukeStorm wrote...
Does that mean I have to make Mine stay in for all 5 cycles?
No. You can increment count when found touching and use:
if (count > 0 && ++count == 5)
to test for removing dead zombie (in death method, outside isTouching block).
danpost danpost

2020/10/15

#
Actually, you cannot use count that way as it is used for moving. Use the death field value instead.
danpost danpost

2020/10/15

#
Try this:
public void death()
{
    if (getWorld() != null)
    {
        if (death > 0)
        {
            death++;
            if (death < 6) setImage("Death"+death+".png");
            else getWorld().removeObject(this);
        }
        else if (isTouching(Mine.class))
        {
            death++;
            removeTouching(Mine.class);
            setImage("Death1.png");
            Greenfoot.playSound("Blast.mp3");
        }
    }
}
ItzLukeStorm ItzLukeStorm

2020/10/15

#
So I tried using the code you gave me and the zombies just die instantly without me using Mine. Also I frogot to mention that multiple zombies come at random spots on the y axis.
danpost danpost

2020/10/15

#
ItzLukeStorm wrote...
So I tried using the code you gave me and the zombies just die instantly without me using Mine..
Change line 13 to:
private int death = 0;
// or just
private int death;
ItzLukeStorm ItzLukeStorm

2020/10/16

#
It worked, thanks.
You need to login to post a reply.