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

2016/1/6

Removing Actor

1
2
3
4
ironphoenix20 ironphoenix20

2016/1/10

#
I need to finish this project soon so can you help me ASAP? Thanks.
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
I did this: if (answerCard1== 1 && Greenfoot.isKeyDown("space")) { World world = getWorld(); world.removeObjects(world.getObjects(AnswerCard1.class)); } It still doesn't work.
Are you getting an error message (if so, what does it say)? or is it just not removing the AnswerCard1 object when you press the space key without error?
ironphoenix20 ironphoenix20

2016/1/10

#
There's no error. It's just not removing it.
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
There's no error. It's just not removing it.
It appears to be a puzzle. From what you have shown, it should be getting removed -- the value of 'answerCard1' is '1' and the object you wish to remove is an 'AnswerCard1' object that is in the world. Unless the world is immediately being restarted (a new Background2 world is set active at the same time) or another AnswerCard1 object is immediately replacing the one removed. For further help, you may have to upload the scenario on the site with the source code published.
ironphoenix20 ironphoenix20

2016/1/10

#
how do i do that?
ironphoenix20 ironphoenix20

2016/1/10

#
This is my hero code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;

/**
 * Write a description of class Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero extends Actor
{
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    int keyCount= 0;
    int cardCount= 0;
    public int riddleCard1= 0;
    public int riddleCard2= 0;
    public int riddleCard3= 0;
    public int riddleCard4= 0;
    int answerCard1= 0;
    int answerCard2= 0;
    int answerCard3= 0;
    int answerCard4= 0;

    public void act() 
    {
        if (Greenfoot.isKeyDown("left")) {
            move (-1);
        }
        if (Greenfoot.isKeyDown("right")) {
            move(1);
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY()-1);
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY()+1);
        }

        if (isTouching(Monster.class)){
            setLocation (359,298);
            System.out.println("Oh no! You have lost all your keys! Try again!");
            Greenfoot.setWorld(new Background());
            keyCount= 0;
        }

        //if (getX()== 300) {
        //GreenfootImage store = new GreenfootImage("Store.jpg");
        //store.scale(getWorld().getWidth(), getWorld().getHeight());
        //getWorld().setBackground(store);

        //Create loop
        if (isTouching(Key.class)) {
            keyCount++;
            removeTouching(Key.class);
            if (keyCount<4) {
                System.out.println("Good job. You have collected " + keyCount + " of 4 keys");
            } else if (keyCount==4)
                System.out.println("Good job! You have collected all of the keys! Head to the door to proceed to Level 2."); 
        }

        if (getX()>180 && getX()<260 && getY()>380 && getY()<600 && keyCount==4||getX()==700) {
            Greenfoot.setWorld(new Background2());
            StartCard startCard= new StartCard();
            getWorld().addObject(startCard, 350, 300);
        }

        if (Greenfoot.isKeyDown("h")) {
            StartCard startCard= new StartCard();
            getWorld().addObject(startCard, 350, 300);
        }

        if (Greenfoot.isKeyDown("space")) {
            removeTouching(StartCard.class);
        }

        if (isTouching(Bat.class) && Greenfoot.isKeyDown("space")) {
            removeTouching(Bat.class);
        }   else if (isTouching(DoomBat.class) && Greenfoot.isKeyDown("space")) {
            Greenfoot.setWorld(new Background());
        }

        if (isTouching(Card.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card.class);
            riddleCard1= 1;
            RiddleCard1 riddle1= new RiddleCard1();
            getWorld().addObject(riddle1, 350, 300);
        }   else if (isTouching(Card2.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card2.class);
            riddleCard2= 1;
            RiddleCard2 riddle2= new RiddleCard2();
            getWorld().addObject(riddle2, 350, 300);
        }   else if (isTouching(Card3.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card3.class);
            riddleCard3= 1;
            RiddleCard3 riddle3= new RiddleCard3();
            getWorld().addObject(riddle3, 350, 300);
        }   else if (isTouching(Card4.class) && Greenfoot.isKeyDown("enter")) {
            removeTouching (Card4.class);
            riddleCard4= 1;
            RiddleCard4 riddle4= new RiddleCard4();
            getWorld().addObject(riddle4, 350, 300);
        }

        if (riddleCard1== 1 && Greenfoot.isKeyDown("c")) {
            removeTouching(RiddleCard1.class);
            answerCard1++;
            cardCount++;
            AnswerCard1 answer1= new AnswerCard1();
            getWorld().addObject(answer1, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 0; i<3; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat14= new Bat();
            getWorld().addObject(bat14, 436, 20);
            Bat bat15= new Bat();
            getWorld().addObject(bat15, 516, 20);
            Bat bat16= new Bat();
            getWorld().addObject(bat16, 39, 61);
        }   else if (riddleCard1== 1 && Greenfoot.isKeyDown("a")||
        riddleCard1== 1 && Greenfoot.isKeyDown("b") ||
        riddleCard1== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2()); 
            Random r= new Random();
            int card1x= r.nextInt(640) + 38;
            while (card1x>48 && card1x<654) {
                card1x= r.nextInt(640) + 38;
            }
            int card1y= r.nextInt(581) + 18;
            while (card1y>24 && card1y<577) {
                card1y= r.nextInt(581) + 18;
            }

        }
        if (riddleCard2== 1 && Greenfoot.isKeyDown("b")) {
            removeTouching (RiddleCard2.class);
            cardCount++;
            answerCard2++;
            AnswerCard2 answer2= new AnswerCard2();
            getWorld().addObject(answer2, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 3; i<6; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat17= new Bat();
            getWorld().addObject(bat17, 39, 184);
            Bat bat18= new Bat();
            getWorld().addObject(bat18, 39, 225);
            Bat bat19= new Bat();
            getWorld().addObject(bat19, 39, 347);
        }   else if (riddleCard2== 1 && Greenfoot.isKeyDown("a")||
        riddleCard2== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard2== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (riddleCard3== 1 && Greenfoot.isKeyDown("a")) {
            removeTouching (RiddleCard3.class);
            cardCount++;
            answerCard3++;
            AnswerCard3 answer3= new AnswerCard3();
            getWorld().addObject(answer3, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 6; i<9; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat20= new Bat();
            getWorld().addObject(bat20, 39, 468);
            Bat bat21= new Bat();
            getWorld().addObject(bat21, 119, 581);
            Bat bat22= new Bat();
            getWorld().addObject(bat22, 438, 581);
        }   else if (riddleCard3== 1 && Greenfoot.isKeyDown("b")||
        riddleCard3== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard3== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (riddleCard4== 1 && Greenfoot.isKeyDown("b")) {
            removeTouching (RiddleCard4.class);
            cardCount++;
            answerCard4++;
            AnswerCard4 answer4= new AnswerCard4();
            getWorld().addObject(answer4, 350, 300);
            Background2 world= (Background2) getWorld();
            for (int i= 9; i<12; i++) {
                world.removeObject (world.doombat[i]);
            }
            Bat bat23= new Bat();
            getWorld().addObject(bat23, 677, 581);
            Bat bat24= new Bat();
            getWorld().addObject(bat24, 663, 544);
            Bat bat25= new Bat();
            getWorld().addObject(bat25, 663, 383);
        }   else if (riddleCard4== 1 && Greenfoot.isKeyDown("a")||
        riddleCard4== 1 && Greenfoot.isKeyDown("c") ||
        riddleCard4== 1 && Greenfoot.isKeyDown("d")) {
            Greenfoot.setWorld(new Background2());
        }

        if (isTouching(RiddleCard1.class) && answerCard1!=0) {
            removeTouching (RiddleCard1.class);
        }
        
         if (isTouching(RiddleCard2.class) && answerCard2!=0) {
            removeTouching (RiddleCard2.class);
        }
        
         if (isTouching(RiddleCard3.class) && answerCard3!=0) {
            removeTouching (RiddleCard3.class);
        }
        
         if (isTouching(RiddleCard4.class) && answerCard4!=0) {
            removeTouching (RiddleCard4.class);
        }
        
        if (isTouching(AnswerCard1.class)) {
            removeTouching(AnswerCard1.class);
        }    

        if (isTouching(AnswerCard2.class)) {
            removeTouching(AnswerCard2.class);
        }   

        if (isTouching(AnswerCard3.class)) {
            removeTouching(AnswerCard3.class);
        }     

        if (isTouching(AnswerCard4.class)) {
            removeTouching(AnswerCard3.class);
        }   
    }   
}
ironphoenix20 ironphoenix20

2016/1/10

#
Here's my background2 (world) class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background2 extends World
{
    Bat[] bat= new Bat[30];
    DoomBat[] doombat= new DoomBat[14];
    AnswerCard1[] answer1= new AnswerCard1[1];
    AnswerCard2[] answer2= new AnswerCard2[1];
    AnswerCard3[] answer3= new AnswerCard3[1];
    AnswerCard4[] answer4= new AnswerCard4[1];

    /**
     * Constructor for objects of class Background2.
     * 
     */
    public Background2()
    {     
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700,600,1); 
        GreenfootImage cave = new GreenfootImage("Cave.jpg");
        cave.scale(getWidth(),getHeight());
        setBackground(cave);

        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Hero hero = new Hero();
        addObject(hero, 388,438);    
        Card card1 = new Card();
        addObject(card1, 4, 518);
        Card2 card2 = new Card2();
        addObject(card2, 699,160);
        Card3 card3 = new Card3();
        addObject(card3, 310,3);
        Card4 card4 = new Card4();
        addObject(card4, 292,598);

        StartCard startCard= new StartCard();
        addObject(startCard, 350, 300);
        if (Greenfoot.isKeyDown("space")) {
            removeObject (startCard);
        }

        bat[0]= new Bat();
        addObject(bat[0], 38, 20);
        bat[1]= new Bat();
        addObject(bat[1], 118, 20);
        bat[2] = new Bat();
        addObject(bat[2], 197, 20);
        bat[3] = new Bat();
        addObject(bat[3], 276, 20);
        bat[4] = new Bat();
        addObject(bat[4], 356, 20);
        doombat[0] = new DoomBat();
        addObject(doombat[0], 436, 20);
        doombat[1] = new DoomBat();
        addObject(doombat[1], 516, 20);
        bat[5] = new Bat();
        addObject(bat[5], 596, 20);
        bat[6] = new Bat();
        addObject(bat[6], 676,20);
        doombat[2] = new DoomBat();
        addObject(doombat[2], 39, 61);
        bat[7] = new Bat();
        addObject(bat[7], 39, 102);
        bat[8] = new Bat();
        addObject(bat[8], 39, 143);
        doombat[3] = new DoomBat();
        addObject(doombat[3], 39, 184);
        doombat[4] = new DoomBat();
        addObject(doombat[4], 39, 225);
        bat[9] = new Bat();
        addObject(bat[9], 39, 265);
        bat[10] = new Bat();
        addObject(bat[10], 39, 306);
        doombat[5] = new DoomBat();
        addObject(doombat[5], 39, 347);
        bat[11] = new Bat();
        addObject(bat[11], 39, 387);
        bat[12] = new Bat();
        addObject(bat[12], 39, 428);
        doombat[6] = new DoomBat();
        addObject(doombat [6], 39, 468);
        bat[13] = new Bat();
        addObject(bat[13], 39, 509);
        bat[14] = new Bat();
        addObject(bat[14], 39, 550);
        bat[15] = new Bat();
        addObject(bat[15], 39, 590);
        doombat[7] = new DoomBat();
        addObject(doombat[7], 119, 581);
        bat[16] = new Bat();
        addObject(bat[16], 199, 581);
        bat[17] = new Bat();
        addObject(bat[17], 279, 581);
        bat[18] = new Bat();
        addObject(bat[18], 358, 581);
        doombat [8] = new DoomBat();
        addObject(doombat[8], 438, 581);
        bat[19] = new Bat();
        addObject(bat[19], 517, 581);
        bat[20] = new Bat();
        addObject(bat[20], 597, 581);
        doombat[9] = new DoomBat();
        addObject(doombat[9], 677, 581);
        doombat[10] = new DoomBat();
        addObject(doombat[10], 663, 544);
        bat[21] = new Bat();
        addObject(bat[21], 663, 503);
        bat[22] = new Bat();
        addObject(bat[22], 663, 463);
        bat[23] = new Bat();
        addObject(bat[23], 663, 424);
        doombat[11] = new DoomBat();
        addObject(doombat[11], 663, 383);
        bat[24] = new Bat();
        addObject(bat[24], 663, 343);
        bat[25] = new Bat();
        addObject(bat[25], 663, 302);
        doombat[12] = new DoomBat();
        addObject(doombat[12], 663, 261);
        bat[26] = new Bat();
        addObject(bat[26], 663, 221);
        bat[27] = new Bat();
        addObject(bat[27], 663, 181);
        bat[28] = new Bat();
        addObject(bat[28], 663, 141);
        bat[29] = new Bat();
        addObject(bat[29], 663, 100);
        doombat[13] = new DoomBat();
        addObject(doombat[13], 663, 61);

    }
}
ironphoenix20 ironphoenix20

2016/1/10

#
Need anything else?
ironphoenix20 ironphoenix20

2016/1/10

#
I need to replace the removeTouching in answercard methods in the hero class with the removeObject method, which isn't working.
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
how do i do that?
Click the 'Share' button at the top-right corner of the greenfoot environment; check to 'Publish source code' checkbox, type your greenfoot info, optionally add title, description, etc., and click 'Share'.
ironphoenix20 ironphoenix20

2016/1/10

#
i shared it.
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
i shared it.
Please inspect line 234 of the Hero class to find your mistake.
ironphoenix20 ironphoenix20

2016/1/10

#
OK
ironphoenix20 ironphoenix20

2016/1/10

#
Oh, I fixed that mistake already. But the problem is, I want to remove those objects without the hero having to touch them.
ironphoenix20 ironphoenix20

2016/1/10

#
Did you look at my scenario yet?
There are more replies on the next page.
1
2
3
4