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

2013/2/23

I need some guidance on this Crab scenario.

1
2
danpost danpost

2013/2/25

#
Without seeing your code, I could not even guess as to why you are having this problem. And even then, I think it may be something other than your code. You could post your code (all classes) and see if anyone else has the same problem, if you so desire.
sometechyguy01 sometechyguy01

2014/11/26

#
after stumbling around on various sites looking for a edge detection method, I found this, although the if ( atWorldEdge() ) doesn't seem to work for me, and says "cannot find symbol- method atWorldEdge()". if you have any suggestions, I am all ears. Thanks.
sometechyguy01 sometechyguy01

2014/11/26

#
or do I need to put this in the world class? just wondering...
danpost danpost

2014/11/26

#
@sometechyguy01, If you noticed, the class that 'atWorldEdge' is located in is a subclass of (or extends) the Animal class, which is where the method is located.
danpost danpost

2014/11/26

#
All the method does is compare the coordinates of the location of the actor with the constant coordinate value along each edge of the world. For example, along the left edge of the world, all points have an x-coordinate of zero. So:
if (getX() == 0)
would be used to do something if the actor was along the left edge of the world. The 'something' that the 'atWorldEdge' method will do is return a 'true' value for this condition. It would do the same for the other three edges of the world. If none of them executed a return, then it would unconditionally return a 'false' value. EDIT: actually the method does put a little offset from each edge in an attempt to compensate for the width of the actor (so half of the image of the actor does not go off the screen before the method returns a true value). So, maybe something like this:
if (getX() < 5)
It should be named 'isNearEdge' to be more precise.
sometechyguy01 sometechyguy01

2014/11/26

#
ok, thanks... been on green foot for a couple of hours, at most. I'm thirteen. The term noob comes to mind. next thing - I have a subclass called Victory, and I am trying to add it in to the map when the crab has collected more than 6 worms, I have been using this: public void scoreCheck() { if (score > 6) { world.addObject(Victory, 250, 250); anyway, sorted this now with new and brackets after Victory, but now get this: world is not public in greenfoot.actor; cannot be accessed from outside package. Suggestion: as this is a public void, could I put this in world but get it to be 'called' by the crab? } } but, I get this syntax message: cannot find symbol- variable Victory what does this mean and how to fix it? thanks...
danpost danpost

2014/11/26

#
You can use:
World world = getWorld();
to assign 'world' its reference. Or, you can just replace 'world' with 'getWorld()' in the current code.
sometechyguy01 sometechyguy01

2014/11/26

#
next thing: how do I set the variable score to zero only once instead of it resetting it to 0 all the way through? I've put it directly beneath 'public class Crab extends Actor'. Basically, how do I reset the score only once?
danpost danpost

2014/11/26

#
What other code do you have in the class that deals with the score (particularly in the 'act' method and method that are called from it).
sometechyguy01 sometechyguy01

2014/11/27

#
ok..
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Crab extends Actor
{
 int score  ;
 /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        eat();
        scoreCheck();

    }
 public void movement() 
  {
      if (Greenfoot.isKeyDown("left"))
        {
           turn(-10);
        }
      if (Greenfoot.isKeyDown("right"))
        {
           turn(10);
        }
      if (Greenfoot.isKeyDown("up"))
        {
           move(5);
        }
      if (Greenfoot.isKeyDown("down"))
        {
           move(-5);
        }
  }
        
 public void eat() 
 {
        Actor worm;
        worm = getOneObjectAtOffset(0, 0, Worm.class);
        if (worm != null)
        {
        World world;
        world = getWorld();
        world.removeObject(worm);
        score =+ 1 ;
        Greenfoot.playSound("eating.wav");
        }
 }  
 public void scoreCheck()
    {
     if (score > 6)
     {
        World world = getWorld();
        world.addObject(new Victory(), 250, 250);   
     }    
    }
}
trying to add a score mechanism to the crab game, but coming into issues when resetted the score to 0 at the start of the game
danpost danpost

2014/11/27

#
The score will be zero at the start of your game; you do not need to set it to zero -- actually at the time you create your Crab object (or call 'new Crab()'), that is the time your score field is created and applied to your new Crab object (with an initial value of zero). So, if you create more than one Crab object, you will have multiple score fields (one with each object). When any of the objects is removed from the world and no longer referenced in your scenario, the score field it holds, and its value, will be junked. If you believe this is what is happening, then maybe your score field should be in your world subclass instead of in the Crab class. In your world subclass, the field will be held by your world object (which is created when you reset the scenario) and again will not need to be programmatically set to zero; for its default initial value is zero.
sometechyguy01 sometechyguy01

2014/11/29

#
fixed it. turns out, the reason the score wasn't working was because I had the score += 1 code the wrong way round. to stop 'variable does not exist' I haven't given the score variable a value, so, I assume, means its set to '0' or null
danpost danpost

2014/11/29

#
sometechyguy01 wrote...
to stop 'variable does not exist' I haven't given the score variable a value, so, I assume, means its set to '0' or null
'int' instance and class fields will always be given a default value of zero if one is not assigned. They will never be 'null' and 'variable does not exist' will not happen for them if declared as in line 5 above.
sometechyguy01 sometechyguy01

2014/12/1

#
ok, thanks
You need to login to post a reply.
1
2