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

2022/1/4

How to remove actor from another actor class ?

greenwood greenwood

2022/1/4

#
I used getWorld().removeObject to remove FullBar actor class in my Wraith actor class which can be seen in line 13 but it doesn't remove the actor at all. How can i remove my FullBar ?
    public void youLose()
    {
        Zombie1 zombie1eat = (Zombie1)getOneIntersectingObject(Zombie1.class);
        Zombie1R zombie1eatR = (Zombie1R)getOneIntersectingObject(Zombie1R.class);
        Zombie2 zombie2eat = (Zombie2)getOneIntersectingObject(Zombie2.class);
        Zombie2R zombie2eatR = (Zombie2R)getOneIntersectingObject(Zombie2R.class);
        HalfBar halfbar = new HalfBar();
        FullBar fullbar = new FullBar();

        if((zombie1eat != null && getNeighbours(getImage().getWidth()/2 , false, Zombie1.class).contains(zombie1eat) && zombie1eat.Eat(this)) || (zombie1eatR != null && getNeighbours(getImage().getWidth()/2 , false, Zombie1R.class).contains(zombie1eatR) && zombie1eatR.Eat(this)) || (zombie2eat != null && getNeighbours(getImage().getWidth() , false, Zombie2.class).contains(zombie2eat) && zombie2eat.Eat(this)) || (zombie2eatR != null && getNeighbours(getImage().getWidth() , false, Zombie2R.class).contains(zombie2eatR) && zombie2eatR.Eat(this)))
        {
            health--;
            getWorld().removeObject(fullbar);
            getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
        }
            if(health==60)
            {
            if(direction==1)
            {
            setImage("Wraith_01_Dying_000.png");
            getImage().scale(107,160);
            getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
            }
            else
            {
            setImage("Wraith_01_Dying_000.png");
            getImage().scale(107,160);
            getImage().mirrorHorizontally();
            getWorld().addObject(halfbar, getWorld().getWidth()/3, getWorld().getHeight()/3);
            }

            return;
        }
            if(health==30)
            {
            if(direction==1)
            {
            setImage("Wraith_01_Dying_000.png");
            getImage().scale(107,160); 
            }
            else
            {
            setImage("Wraith_01_Dying_000.png");
            getImage().scale(107,160);
            getImage().mirrorHorizontally();  
            }
            return;
        }
            if(health==0)
            {
            if(direction==1)
            {
            setImage("Wraith_01_Dying_014.png");
            getImage().scale(260,210);                
            }
            else
            {
            setImage("Wraith_01_Dying_014.png");
            getImage().scale(260,210);  
            getImage().mirrorHorizontally();
            }
            getWorld().showText("You Lose! - You lasted " +(gameTime/60) + " seconds", getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
            return;
        }
        
    }
danpost danpost

2022/1/4

#
You have line 8 creating a new FullBar object -- one that is NOT in any world. And if you placed it in the world, you would then have 2 FullBar objects to remove. Replace line 8 with the following:
java.util.List list = getWorld().getObjects(FullBar.class);
if ( ! list.isEmpty())
{
    fullBar  = (Actor) list.get(0);
}
Then, change line 13 to:
if (fullBar != null) getWorld().removeObject(fullBar);
greenwood greenwood

2022/1/4

#
danpost wrote...
You have line 8 creating a new FullBar object -- one that is NOT in any world. And if you placed it in the world, you would then have 2 FullBar objects to remove. Replace line 8 with the following:
java.util.List list = getWorld().getObjects(FullBar.class);
if ( ! list.isEmpty())
{
    fullBar  = (Actor) list.get(0);
}
Then, change line 13 to:
if (fullBar != null) getWorld().removeObject(fullBar);
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
danpost danpost

2022/1/4

#
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
Actor fullBar = null;
greenwood greenwood

2022/1/5

#
danpost wrote...
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
Actor fullBar = null;
Thank you so much dan! its worked :)
greenwood greenwood

2022/1/6

#
danpost wrote...
greenwood wrote...
i got an error which the fullBar is an undeclared variable. What should i declare for the variable ?
Sorry. Start the code with the following line:
Actor fullBar = null;
hai dan, i have another question. How to bounce back my projectile to the opposite direction when the projectile hit the world edge?
public void act()
{
if(Greenfoot.isKeyDown("j")
{
specialattacks();
}
}

    public void specialattacks()
    {
        SpecialAttack specialattack = new SpecialAttack();
        
        if(direction ==1)
        {
            getWorld().addObject(specialattack, getX()+100, getY());        
            if(atWorldEdge())
            {
                specialattack.setRotation(180);
            }
        }
        
        if(direction==2)
        {
            getWorld().addObject(specialattack, getX()-100, getY());      
            specialattack.setRotation(180);
            
            if(atWorldEdge())
            {   
                specialattack.setRotation(0);
            }
        }


    }

    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            return true;
        }
        
        else
        return false;
    }
danpost danpost

2022/1/6

#
How about using the following to change direction:
direction = 3-direction:
greenwood greenwood

2022/1/6

#
danpost wrote...
How about using the following to change direction:
direction = 3-direction:
is this the right line to put the code ? because it doesn't work
    public void specialattacks()
    {
        SpecialAttack specialattack = new SpecialAttack();
        
        if(direction ==1)
        {
            getWorld().addObject(specialattack, getX()+100, getY());        
            if(atWorldEdge())
            {
                direction = 3-direction;
            }
        }
        
        if(direction==2)
        {
            getWorld().addObject(specialattack, getX()-100, getY());      
            specialattack.setRotation(180);
            
            if(atWorldEdge())
            {   
                direction = 3-direction;
            }
        }


    }
danpost danpost

2022/1/6

#
greenwood wrote...
is this the right line to put the code ? because it doesn't work
Of course, I cannot be totally sure (especially since you have not provided any actual movement codes. However, using "else if" on line 14 might do the trick.
greenwood greenwood

2022/1/7

#
danpost wrote...
greenwood wrote...
is this the right line to put the code ? because it doesn't work
Of course, I cannot be totally sure (especially since you have not provided any actual movement codes. However, using "else if" on line 14 might do the trick.
sorry for the late reply dan. My wraith class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Wraith extends Actor
{
    int animateImage = 0;   //for walking animation image (frame)
    int animateSpeed = 2;   //lower the animate speed for faster walking animation //3
    int animateSpeed2 = 6;  //lower the animate speed for faster idle animation
    int animateSpeed3 = 1; //speed for shooting animation
    int count;
    int direction =1; //direction which actor facing
    SimpleTimer spaceTimer = new SimpleTimer();  //timer for when the next space can be tekan lagi
    private int timer;
    int gameTime =0;  //
    int health = 90; //wraith health
    Actor fullBar = null;
    
    public Wraith()
    {
       setImage("Wraith_01_Idle Blinking_000(try).png");
       getImage().scale(107,160);
    }
    
    public void act()
    {
        gameTime++;
        count++;
        checkKeys();
        
        //for movement restrict of wraith in the world
        if(getX()>1200) setLocation(1200,getY());
        if(getY()>550) setLocation(getX(), 550);
        if(getY()<250) setLocation(getX(),250);
        
        youLose();
        
    }
    //test
    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("w")) //for moveup
        {
            setLocation(getX(), getY()-5);
        }
        
        if(Greenfoot.isKeyDown("s")) //for movedown
        {
            setLocation(getX(), getY()+5);
        }
        
        if(Greenfoot.isKeyDown("d")) //for move right
        {
            if((health !=60) && (health !=30) && (health!=0))
            {setLocation (getX() +5, getY());
            walkinganimateRight();
            }
            else
            {
                
            }
            direction =1;
        }
        
        if(Greenfoot.isKeyDown("a")) //for move  left
        {
            setLocation (getX() -5, getY());
            walkinganimateLeft();
            direction =2;
        }
        
        if(Greenfoot.isKeyDown("space") && spaceTimer.millisElapsed() > 600)  
        {
            if(direction==1)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                shoot();
            }
            
            else if(direction==2)
            {
                setImage("Wraith_01_Casting Spells_006.png");
                getImage().scale(260,210);
                getImage().mirrorHorizontally();
                shoot(); 
            }
            spaceTimer.mark();   //reset balik timer kepada 0
        }
        
        if(Greenfoot.isKeyDown("j")&& spaceTimer.millisElapsed() >600) //special attack power
        {
            specialattacks();
            spaceTimer.mark();
            
        }
        
        //declare for no button pressed
        boolean up = Greenfoot.isKeyDown("w");
        boolean down = Greenfoot.isKeyDown("s");
        boolean right = Greenfoot.isKeyDown("d");
        boolean left = Greenfoot.isKeyDown("a");
        boolean space = Greenfoot.isKeyDown("space");
        boolean j = Greenfoot.isKeyDown("j");
        
        if(!up && !down && !left && !right && !space && !j)  //no button is pressed
        {
            if(direction ==1)
            {
                idleanimationRight();
            }
            
            if(direction ==2)
            {
                idleanimationLeft();
            }
        }
    }
    
    public void specialattacks()
    {
        SpecialAttack specialattack = new SpecialAttack();
        
        if(direction ==1)
        {
            getWorld().addObject(specialattack, getX()+100, getY());        
            if(atWorldEdge())
            {
                setRotation(specialattack.getRotation()*-1);
            }
        }
        
        if(direction==2)
        {
            getWorld().addObject(specialattack, getX()-100, getY());      
            specialattack.setRotation(180);
            
            if(atWorldEdge())
            {   
                setRotation(specialattack.getRotation()*-1);
            }
        }


    }
    
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            return true;
        }
        
        else
        return false;
    }
    
    public void walkinganimateRight()
    {
        if(count % animateSpeed == 0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);  // 260,210
            
        }
    }
    
    public void walkinganimateLeft()
    {
        if(count % animateSpeed ==0)
        {
            if(animateImage >11)
            {
                animateImage = 0;
            }
            setImage("Wraith_01_Moving Forward_00" + animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);   //260,210
            getImage().mirrorHorizontally();
        }
    }
    
    public void shoot()
    {
        Fireball fireball = new Fireball();
        Greenfoot.playSound("Fireball Sound.wav");
        if(direction==1)
        {
            getWorld().addObject(fireball, getX()+100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
        }
        
        else if(direction==2)
        {
            getWorld().addObject(fireball, getX()-100, getY());        //masukkan fireball ke dalam world dan berjarak 100 pixels dari wraith
            fireball.setRotation(180);
        }
    }
    
    public void idleanimationRight()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160);  //260,210
        }
    }
    
        public void idleanimationLeft()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >11)
            {
                animateImage =0;
            }
            setImage("Wraith_01_Idle Blinking_00" +animateImage + "(try).png");
            animateImage++;
            getImage().scale(107,160); //260,210
            getImage().mirrorHorizontally();
        }
    }
    
SpecialAttack class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class SpecialAttack extends Actor
{
    int animateImage =1;
    int animateSpeed2 = 3; //animation speed
    int count;
    
    public SpecialAttack()
    {
        //setImage("frame_0_delay-0.1ss.png");
        setImage("blue-projectile1.png");
        getImage().scale(116,119);
    }
    
    public void act()
    {
        count++;
        animate();
        move(5);
    }
    
    public void animate()
    {
        if(count % animateSpeed2 ==0)
        {
            if(animateImage >2)
            {
                animateImage =1;
            }
            setImage("blue-projectile" + animateImage + ".png");
            animateImage++;
            getImage().scale(116,119);
            //getImage().scale(150,150);
        }     
    }

}
danpost danpost

2022/1/7

#
The following SpecialAttack act method should suffice:
public void act()
{
    count++;
    animate();
    move(5);
    if (isAtEdge()) turn(180);
}
greenwood greenwood

2022/1/8

#
danpost wrote...
The following SpecialAttack act method should suffice:
public void act()
{
    count++;
    animate();
    move(5);
    if (isAtEdge()) turn(180);
}
Thank you it finally worked, but i have one more question, how to remove the object after it bounces back at another edge of the world ?
    public void act()
    {
        count++;
        animate();
        move(3);
        if(getX()>1190 || getX()<5 && !checkEdge)
        {
            checkEdge = true;
            turn(180);
            if(isAtEdge() && checkEdge)            
            {
                getWorld().removeObject(this);
                checkEdge = false;
            }

        }
    }
danpost danpost

2022/1/8

#
greenwood wrote...
how to remove the object after it bounces back at another edge of the world ? << Code Omitted >>
It is not a checkEdge boolean that you need -- it is a bounced boolean. The condition to bounce is when the boolean is false. Set the boolean to true when bouncing. Then, remove the actor when the boolean is true (when at edge). It would be like this:
private boolean bounced;

public void act()
{
    count++;
    animate();
    move(3);
    if (isAtEdge())
    {
        if (bounced) getWorld().removeObject(this);
        else
        {
            turn(180);
            bounced = true;
        }
    }
}
You need to login to post a reply.