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

2014/11/3

Holding an actor

1
2
RSGBROK RSGBROK

2014/11/3

#
Hi, I've got a net which catches actors. I've got 3 actors. When the net is at the same grid as one of the 3 actors it has to hold the actor and move it for 5 grids. The movement of the net is done with the arrow keys. Do any of you know the method for holding and moving around an actor? At the moment they actor gets eaten when the net catches the actor but that has to be changed to holding the actor and moving it around for 5 movements.
public void holdWolf()
        {
            if (canSee(Wolf.class)) 
            {
                eat(Wolf.class);
                wolfsEaten = wolfsEaten - 1;
            }
        }
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I think what you should be trying to do is make the actor move in the same way as the net. One option would be to make the actor follow the net instead of having a method in net with the statement : if(cansee(wolf.class)). So you would instead use a statement like : if(cansee(net.class)) in your actor code. But if I may, is this an assignment for school? Because I've seen something very simular come by not to long ago. Maybe it's worth going back in the discussions to try find that discussion? Anyway, if you really have to make the net hold the wolf instead of the other way around you could use a method within your actor class which gets called as long as the net holds the actor. So say your wolf has a new method called 'caught'. The new method would make the wolf move the same way as the actor because you can simply copy paste the if(keyisdown(key)) statements I expect are in your net code and paste them into the new method of wolf. Then in your if(cansee(Wolf.class))-statement you should make a reference to the caught-method. So as long as if(canSee(Wolf.class)) is true it will run the part in the wolf class that makes wolf respond to the arrow keys. I hoped this helped somewhat.
RSGBROK RSGBROK

2014/11/3

#
Thank you very much for your reply. I like the way of putting a caught method in the Wolf.class. However this raises the question where exactly do I need to put it in my public void act()? I've got this as my method:
public void act() 
        {
               if(canMove())    {
                   move();
                   eetGeit();
                   decideWinner();
               }   
               else {
                    int x = getX();
                    int y = getY();
                    int breedte = getWorld().getWidth();
                    int hoogte = getWorld().getHeight();
                    if ( x == 0 ) setLocation(breedte-1, y);
                    if (x == breedte-1) setLocation(1, y);
                    if (y == 0) setLocation(x, hoogte-1);
                    if (y == hoogte-1) setLocation (x, 1);
               }    
        }
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Hehe, Nederlander of niet?
eetgeit
Anyway, if you want the caught in the Wolf.class you would probably put the call inside the act and you might even be able to use the canMove variable. For instance if the wolf can't move i assume it's caught by the net right? So in your else statement just add a line caught(); and then put the method outside of the act method
public void act()
{
   if(canmove()
   {
      //whatever you want in here
   }

   else
   {
      //whatever you allready have in here
      caught();
   }
}

public void caught()
{
   //here the code for reacting to the arrow keys
}
It would look something like that. That does mean canMove is only true if the wolf isn't touching the net. If that's how your canMove boolean works this should work just fine. If not use something like this
public void act()
{
  if(getOneObjectAtOffset(0, 0, net.class)!=null) // if touching an object from the net class
   {
      caught();
   }

   else if(canMove)
  {
      //the code you allready had
   }

  else
  {
     //the code you allready had
  }
}
Please let me know if this answeres you question. Ik antwoord trouwens niet in het nederlands omdat andere programmeurs dan ook kunnen helpen als ik ernaast zit.
RSGBROK RSGBROK

2014/11/3

#
Nederlander inderdaad! I've edited this in but the Wolf isn't captured inside the net it can still escape. Do I need to add something to the canSee method in the net.class?
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I tried looking up the canSee method you're using but It doesn't seem to be a normal method. How does the method work? Does it respond with a boolean or is it a method you made yourself? I you have the code in the method that should allso help me help you
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Just in case you didn't see the edit as a new message. I posted something
RSGBROK RSGBROK

2014/11/3

#
The canSee method is a standard method that I got from one of the examples on this site:
public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
Does this help?
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
I see, So it does returns a true/false value. Hm.... Ow, I think i found it. You're calling cansee in your wolf class. But you're calling cansee(wolf.class). In other words can the wolf see the wolf. It should probably be cansee(net.class)... wait don't react yet
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Could you poste your code from wolf again? So from beginning to end just poste the entire class plz
RSGBROK RSGBROK

2014/11/3

#
This is the canSee method in the Wolf class
        /**
         * Wolf Eet de geit, en haal 1 geit van de teller af.
         */
        public void eetGeit()
        {
            if ( canSee(Geit.class) ) 
            {
                eat(Geit.class);
                geitenEaten = geitenEaten - 1;
            }
        }
danpost danpost

2014/11/3

#
The 'canSee' and 'eat' methods are obsolete. The Actor class, since Greenfoot version 3.2 (I believe) has contained 'isTouching' (equivalent to 'canSee') and 'removeTouching' (equivalent to 'eat') methods.
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
In other words what danpost it trying to say is you can use the same code, but replace cansee by itTouching and replace eat by removeTouching. Allthough I don't see the use of doing so....
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
By the way isn't it weird to make a counter called gegetengeiten and then take of one... you can't uneat a goat.... Just saying it might be clearer if you change it to a +1 so you add to the amount of goats that have been eaten
RSGBROK RSGBROK

2014/11/3

#
So, when I change those words the actors should be captured by the net and move around?
There are more replies on the next page.
1
2