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/6

#
How do I remove an object in greenfoot? I tried doing Actor object= getOneObjectAtOffset(39, 225, Object.class); and then getWorld().removeObject (object); inside an if statement. It compiles but the objects are not being removed at the correct time. How do I do this and what am I doing wrong?
danpost danpost

2016/1/6

#
The offset values, (39, 225), seem to be quite strange. Do you really want the object to be located a bit right and well below where the actor is located? What condition(s), specifically, do you require for the object to be removed?
ironphoenix20 ironphoenix20

2016/1/6

#
I'm trying to remove like 3 objects from an array of 13 objects. How do I do this?
danpost danpost

2016/1/6

#
ironphoenix20 wrote...
I'm trying to remove like 3 objects from an array of 13 objects. How do I do this?
What array? Which 3 objects? Where is the code that needs fixed (why is it not posted)? I have noticed that you have started several discussion on this site giving very little information about what you want -- using "How do I ..." or "How do you ..." without any specifics or code to go by. We cannot help you when you are being so vague. Chances are, if any possible solution is given, it will not be what you want and you will not be any closer to fixing your code or finishing your project.
ironphoenix20 ironphoenix20

2016/1/7

#
This is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * 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; int riddleCard1= 0; int riddleCard2= 0; int riddleCard3= 0; 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); 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); } 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 Background()); } if (riddleCard2== 1 && Greenfoot.isKeyDown("b")) { removeTouching (RiddleCard2.class); cardCount++; AnswerCard2 answer2= new AnswerCard2(); getWorld().addObject(answer2, 350, 300); Background2 world= (Background2) getWorld(); for (int i= 3; i<6; i++) { world.removeObject (world.doombat); } 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 Background()); } if (riddleCard3== 1 && Greenfoot.isKeyDown("a")) { removeTouching (RiddleCard3.class); cardCount++; AnswerCard3 answer3= new AnswerCard3(); getWorld().addObject(answer3, 350, 300); Background2 world= (Background2) getWorld(); for (int i= 6; i<9; i++) { world.removeObject (world.doombat); } 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 Background()); } if (riddleCard4== 1 && Greenfoot.isKeyDown("b")) { removeTouching (RiddleCard4.class); cardCount++; AnswerCard4 answer4= new AnswerCard4(); getWorld().addObject(answer4, 350, 300); Background2 world= (Background2) getWorld(); for (int i= 9; i<12; i++) { world.removeObject (world.doombat); } 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 Background()); } if (answerCard1== 1 && Greenfoot.isKeyDown("space")) { } }
ironphoenix20 ironphoenix20

2016/1/7

#
I need to remove the AnswerCard1 in the last if statement without the hero having to touch the card. How do I do this?
ironphoenix20 ironphoenix20

2016/1/7

#
So, without the removeTouching way.
danpost danpost

2016/1/8

#
The following will have an actor in the world remove ALL AnswerCard1 objects from the world:
World world = getWorld();
world.removeObjects(world.getObjects(AnswerCard1.class));
ironphoenix20 ironphoenix20

2016/1/8

#
I did this: if (answerCard1== 1 && Greenfoot.isKeyDown("space")) { World world = getWorld(); world.removeObjects(world.getObjects(AnswerCard1.class)); } It still doesn't work.
danpost danpost

2016/1/8

#
Stop the scenario after it refused to work and do the following checks: * right click on the object you want remove and select 'Inspect'; post the header line as result of check#1 * right click on the hero and select 'Inspect'; post the value set to 'answerCard1' as result of check#2
ironphoenix20 ironphoenix20

2016/1/8

#
check #1: what is the header check #2: int answerCard1: 1
danpost danpost

2016/1/8

#
ironphoenix20 wrote...
check #1: what is the header
The top line of text in the box that displays the name of the object.
ironphoenix20 ironphoenix20

2016/1/10

#
int x: 350
danpost danpost

2016/1/10

#
ironphoenix20 wrote...
int x: 350
No -- even above that -- along the top edge of the frame -- the name of the object given by the inspector.
ironphoenix20 ironphoenix20

2016/1/10

#
answercard1: AnswerCard1
There are more replies on the next page.
1
2
3
4