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

2016/1/6

Removing Actor

1
2
3
4
danpost danpost

2016/1/10

#
Use the code I gave and you tried before -- except change 'answerCard1 == 1' to 'answerCard1 > 1'.
danpost danpost

2016/1/10

#
The main problem is that the condition used to add the answer card into the world is valid for multiple act cycles and more than one answer card is added into the world and the 'answerCard' field is incremented multiple times.
ironphoenix20 ironphoenix20

2016/1/10

#
I already tried with answerCard1!=0. Wouldn't that work?
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
I already tried with answerCard1!=0. Wouldn't that work?
Should do equally well.
ironphoenix20 ironphoenix20

2016/1/10

#
But it didn't work.
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
But it didn't work.
Worked for me. Add another line in the removal block to remove the riddle card also.
ironphoenix20 ironphoenix20

2016/1/10

#
Worked for you? Could you copy your code and send it to me? That would be really helpful.
ironphoenix20 ironphoenix20

2016/1/10

#
I'll check it later or tomorrow. I've gotta go now.
danpost danpost

2016/1/10

#
I just changed this (lines 221 to 223 on your earlier post):
if (isTouching(AnswerCard1.class)) {
removeTouching(AnswerCard1.class);
}
to this:
if (answerCard1 != 0 && Greenfoot.isKeyDown("space"))
{
    getWorld().removeObjects(getWorld().getObjects(AnswerCard1.class));
    getWorld().removeObjects(getWorld().getObjects(RiddleCard1.class));
}
Similar changes need to be applied for the other answer card removals.
ironphoenix20 ironphoenix20

2016/1/11

#
Thanks! That worked!
ironphoenix20 ironphoenix20

2016/1/11

#
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;
    int level2over= 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) {
            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++;
            riddleCard1= 0;
            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;
            int card1y= r.nextInt(581) + 18;
            Card card1= new Card();
            while (getWorld().addObject(card1, card1x, card1y).isTouching(Card2.class)==false) {
                while (card1x>48 && card1x<654) {
                    card1x= r.nextInt(640) + 38;
                }
                while (card1y>24 && card1y<577) {
                    card1y= r.nextInt(581) + 18;
                }
            }
            getWorld().removeObjects(getWorld().getObjects(Card.class));
            getWorld().removeObjects(getWorld().getObjects(Card2.class));
            getWorld().removeObjects(getWorld().getObjects(Card3.class));
            getWorld().removeObjects(getWorld().getObjects(Card4.class));
        }
        if (riddleCard2== 1 && Greenfoot.isKeyDown("b")) {
            removeTouching (RiddleCard2.class);
            cardCount++;
            answerCard2++;
            riddleCard2=  0;
            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++;
            riddleCard3= 0;
            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++;
            riddleCard4= 0;
            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 (answerCard1!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard1.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard1.class));
        }    

        if (answerCard2!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard2.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard2.class));
        }    

        if (answerCard3!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard3.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard3.class));
        }        

        if (answerCard4!= 0 && Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObjects(getWorld().getObjects(AnswerCard4.class));
            getWorld().removeObjects(getWorld().getObjects(RiddleCard4.class));
        }     

        if (cardCount==4 && level2over==1 && Greenfoot.isKeyDown("space")) {  
            Greenfoot.setWorld(new Background3());
            StartCard3 startCard3= new StartCard3();
            getWorld().addObject(startCard3, 350, 300);
        }
    }
}
ironphoenix20 ironphoenix20

2016/1/11

#
I need to create a while loop that runs until an object is touching another object. I've started this loop in line 134, but it's not working. What am I doing wrong?
ironphoenix20 ironphoenix20

2016/1/11

#
sorry, it should be ==true, but it still doesn't work.
danpost danpost

2016/1/11

#
ironphoenix20 wrote...
I need to create a while loop that runs until an object is touching another object. I've started this loop in line 134, but it's not working. What am I doing wrong?
Starting line 126, you have this:
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;
    int card1y= r.nextInt(581) + 18;
    Card card1= new Card();
    while (getWorld().addObject(card1, card1x, card1y).isTouching(Card2.class)==false) {
        while (card1x>48 && card1x<654) {
            card1x= r.nextInt(640) + 38;
        }
        while (card1y>24 && card1y<577) {
            card1y= r.nextInt(581) + 18;
        }
    }
    getWorld().removeObjects(getWorld().getObjects(Card.class));
    getWorld().removeObjects(getWorld().getObjects(Card2.class));
    getWorld().removeObjects(getWorld().getObjects(Card3.class));
    getWorld().removeObjects(getWorld().getObjects(Card4.class));
}
Everything from line 6 to the last line of code within this block of code is irrelevant. Line 5 creates and sets active a new World object of type Background2. The Hero object executing the code is not in that new world and anything you do to the world the hero is in will not matter (there is a new Hero object created and placed within the new world when the new Background2 world is prepared).
ironphoenix20 ironphoenix20

2016/1/11

#
So, what am I supposed to do?
There are more replies on the next page.
1
2
3
4