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

2020/12/9

Greenfoot.Bee scenario

1
2
Username123456789 Username123456789

2020/12/9

#
I am trying to make it where when a "Spider" eats a "Bee" the bee is deleted and then respawned at a precise location on the map. what I have right now only made it where the "Spider" spawns in the location. also, I am trying to make it where once the Bee has no more lives the game ends. here is the code if(isTouching(Bee.class)) { Bee.setLocation(20,20); lives--; Greenfoot.playSound("uDie.wav"); if(lives==0) { Greenfoot.stop(); } }
Username123456789 Username123456789

2020/12/9

#
this is all in the "Spider" code
Username123456789 Username123456789

2020/12/9

#
when I compile the code the error that is shone is "non-static method setLocation(int, int) cannot be referenced from a static context"
danpost danpost

2020/12/9

#
Username123456789 wrote...
when I compile the code the error that is shone is "non-static method setLocation(int, int) cannot be referenced from a static context"
Use removeTouching. Have world act method add a new Bee object if none in world.
RcCookie RcCookie

2020/12/9

#
This is because you have to set the location of a specific instance, not the class. Try this:
Bee bee = getOneIntersectingObject(Bee.class);
bee.setLocation20, 20);
Username123456789 Username123456789

2020/12/10

#
RcCookie, I will have to test that tomorrow, I am doing this as part of the school and only have one chance to do it. what does "getOneIntersectingObject(Bee.class)" do? and would the 2nd line look like "bee.setLocation(20, 20);" not "bee.setLocation20, 20);"? danpost, There is already a bee in the world, the player controls the bee, and when they touch the spider, they are supposed to lose a life and respawn in the corner of the world. The issue is that either the lives never go down or the game doesn't end. Tomorrow I will post everything I have for both the Bee and Spider if that would help?
danpost danpost

2020/12/10

#
Username123456789 wrote...
There is already a bee in the world, the player controls the bee, and when they touch the spider, they are supposed to lose a life and respawn in the corner of the world. The issue is that either the lives never go down or the game doesn't end.
Okay. Add field in world to hold bee and have world place it back in world (if lives still remain) when found not in world:
private Bee bee = new Bee();

public void act()
{
    if (bee != null && bee.getWorld() == null)
    {
        bee.lives--;
        if (bee.lives == 0)
        {
            Greenfoot.stop();
            bee = null;
        }
        else addObject(bee, 20, 20);
    }
}
In Spider class:
if (isTouching(Bee.class))
{
    removeTouching(Bee.class);
    Greenfoot.playSound("uDie.wav");
}
RcCookie RcCookie

2020/12/10

#
Username123456789 wrote...
RcCookie, what does "getOneIntersectingObject(Bee.class)" do? and would the 2nd line look like "bee.setLocation(20, 20);" not "bee.setLocation20, 20);"?
Yes, second line was a typo. getOneIntersectingObject returns you one object of the specified class that graphically intersects the actor, or returns null. In this case, as you have checked for collision before, it will not return null and we can savely use it.
danpost danpost

2020/12/10

#
RcCookie wrote...
Yes, second line was a typo
First line fails also.
Username123456789 Username123456789

2020/12/10

#
ok, I will try that.
Username123456789 Username123456789

2020/12/10

#
I put it in and it returned "lives has private access in Bee" does that mean I just need to go and delete the code in the Bee
Username123456789 Username123456789

2020/12/10

#
I have this in Bee code private void Stats() { score = 0; lives = 3; }//endact would I need to delete this inorder for the code to work?
Username123456789 Username123456789

2020/12/10

#
ok, I deleted everything I had to do with the Bee's lives and now it says "cannot find symbol - variable lives" not sure what I do to correct
Username123456789 Username123456789

2020/12/10

#
here is the code that I have in the three without anything that yall said, there is a ton, "BeeWorld" private int score; /** * Constructor for objects of class BeeWorld. * */ public BeeWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 1000, 1); 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() { Greenfoot.playSound("GameStart.wav"); Bee bee = new Bee(); addObject(bee,468,193); bee.setLocation(553,239); Fly fly = new Fly(); addObject(fly,467,286); bee.setLocation(252,163); bee.setLocation(319,169); Spider spider = new Spider(); addObject(spider,87,85); Fly fly2 = new Fly(); addObject(fly2,451,73); Fly fly3 = new Fly(); addObject(fly3,172,255); Fly fly4 = new Fly(); addObject(fly4,160,50); bee.setLocation(100,177); fly3.setLocation(141,312); Spider spider2 = new Spider(); addObject(spider2,273,331); Spider spider3 = new Spider(); addObject(spider3,312,59); spider.setLocation(524,353); spider.setLocation(605,217); spider.setLocation(527,347); removeObject(spider); spider2.setLocation(229,701); spider3.setLocation(574,210); fly.setLocation(867,927); fly3.setLocation(820,414); bee.setLocation(452,488); fly2.setLocation(165,322); fly4.setLocation(400,881); removeObject(bee); spider2.setLocation(499,492); spider3.setLocation(500,488); Spider spider4 = new Spider(); addObject(spider4,921,264); spider4.setLocation(930,255); Spider spider5 = new Spider(); addObject(spider5,930,255); spider4.setLocation(927,272); Spider spider6 = new Spider(); addObject(spider6,927,272); Spider spider7 = new Spider(); addObject(spider7,894,260); spider4.setLocation(940,272); Spider spider8 = new Spider(); addObject(spider8,940,272); Spider spider9 = new Spider(); addObject(spider9,893,286); spider4.setLocation(911,276); Spider spider10 = new Spider(); addObject(spider10,911,276); spider7.setLocation(891,266); Spider spider11 = new Spider(); addObject(spider11,891,266); spider7.setLocation(488,495); spider4.setLocation(505,491); spider7.setLocation(493,490); spider4.setLocation(476,495); spider4.setLocation(488,493); spider4.setLocation(500,493); spider9.setLocation(493,491); spider4.setLocation(497,500); fly3.setLocation(551,427); fly2.setLocation(427,448); fly4.setLocation(435,547); fly.setLocation(568,440); fly2.setLocation(594,471); fly4.setLocation(597,496); Fly fly5 = new Fly(); addObject(fly5,831,195); Fly fly6 = new Fly(); addObject(fly6,903,218); Fly fly7 = new Fly(); addObject(fly7,928,228); Fly fly8 = new Fly(); addObject(fly8,869,210); fly6.setLocation(908,202); Fly fly9 = new Fly(); addObject(fly9,908,202); Fly fly10 = new Fly(); addObject(fly10,930,183); fly10.setLocation(946,172); Fly fly11 = new Fly(); addObject(fly11,946,172); fly10.setLocation(946,202); Fly fly12 = new Fly(); addObject(fly12,946,202); Fly fly13 = new Fly(); addObject(fly13,923,161); fly13.setLocation(905,157); Fly fly14 = new Fly(); addObject(fly14,905,157); fly10.setLocation(924,163); Fly fly15 = new Fly(); addObject(fly15,924,163); fly13.setLocation(908,162); Fly fly16 = new Fly(); addObject(fly16,908,162); fly9.setLocation(897,186); Fly fly17 = new Fly(); addObject(fly17,897,186); fly5.setLocation(851,195); Fly fly18 = new Fly(); addObject(fly18,851,195); Fly fly19 = new Fly(); addObject(fly19,857,174); fly19.setLocation(867,158); Fly fly20 = new Fly(); addObject(fly20,867,158); fly6.setLocation(555,494); fly4.setLocation(548,495); fly.setLocation(547,511); fly.setLocation(556,548); fly.setLocation(515,533); fly5.setLocation(499,537); fly6.setLocation(478,539); fly13.setLocation(473,529); fly8.setLocation(470,526); fly6.setLocation(455,513); fly8.setLocation(454,509); fly6.setLocation(452,486); fly13.setLocation(453,470); fly19.setLocation(463,457); fly19.setLocation(470,457); fly10.setLocation(499,452); fly10.setLocation(490,446); fly14.setLocation(507,454); fly10.setLocation(522,442); fly10.setLocation(533,453); fly10.setLocation(539,461); Fly fly21 = new Fly(); addObject(fly21,539,461); fly10.setLocation(545,473); Fly fly22 = new Fly(); addObject(fly22,545,473); removeObject(spider5); removeObject(spider6); removeObject(spider8); removeObject(spider11); removeObject(spider10); removeObject(fly12); removeObject(fly7); removeObject(fly15); removeObject(fly11); removeObject(fly16); removeObject(fly17); removeObject(fly9); removeObject(fly20); removeObject(fly18); removeObject(fly2); removeObject(fly3); removeObject(fly22); removeObject(fly21); removeObject(fly4); removeObject(fly10); removeObject(spider9); removeObject(spider7); removeObject(spider4); removeObject(spider3); spider2.setLocation(699,430); removeObject(fly14); removeObject(fly19); removeObject(fly13); removeObject(fly6); removeObject(fly8); fly.setLocation(299,182); fly.setLocation(375,605); spider2.setLocation(771,207); fly.setLocation(261,720); removeObject(fly5); Fly fly23 = new Fly(); addObject(fly23,231,186); Fly fly24 = new Fly(); addObject(fly24,836,692); Fly fly25 = new Fly(); addObject(fly25,527,156); Fly fly26 = new Fly(); addObject(fly26,296,411); Fly fly27 = new Fly(); addObject(fly27,551,774); removeObject(fly23); removeObject(fly27); removeObject(fly26); Fly fly28 = new Fly(); addObject(fly28,843,396); Fly fly29 = new Fly(); addObject(fly29,259,338); Fly fly30 = new Fly(); addObject(fly30,490,850); } "Spider" private int lives; /** * Act - do whatever the Spider wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(1); if(isTouching(Fly.class)) { removeTouching(Fly.class); Greenfoot.playSound("HeHe.wav"); } if (isTouching(Bee.class)) { removeTouching(Bee.class); Greenfoot.playSound("uDie.wav"); } if(isAtEdge()) { turn(180); } if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(90)-45); } } "Bee" private int score; /** * Act - do whatever the Bee wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private void Stats() { score = 0; }//endact public void act() { if(Greenfoot.isKeyDown("w")) { move(10); }//endif else if (Greenfoot.isKeyDown("s")) { move(-5); }//endelse if(Greenfoot.isKeyDown("a")) { turn(-7); }//endif else if(Greenfoot.isKeyDown("d")) { turn(7); }//endelse if(isAtEdge()) { turn(180); }//endif if(isTouching(Fly.class)) { removeTouching(Fly.class); Greenfoot.playSound("Die.wav"); score++; getWorld().showText("Score : " + score, 60, 390); getWorld().addObject(new Fly(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); }//endif }//endoperation
danpost danpost

2020/12/11

#
All you had to do was remove the "private" modifier from the field.
There are more replies on the next page.
1
2