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

2012/5/11

How to perform a task once and only once.

Loopy Loopy

2012/5/11

#
I have an actor performing the following: if ( canSee(Dog.class) ) { dance(); } it works but, it will not stop working. I understand that it continues because the actor is still in contact with the dog but cannot figure out how to stop the contact or action. Any suggestions?
SPower SPower

2012/5/11

#
What must happen when 'the contact stops'?
erdelf erdelf

2012/5/11

#
try it with a boolean something like this.
private boolean dancing = false;

public void act()
{
      if ( canSee(Dog.class) ) 
      { 
          if(!dancing)
          {
              dance(); 
              dancing = true;
          }
      }
}
Loopy Loopy

2012/5/11

#
When the contact stops the actor just stops and stands again as it was before.
Loopy Loopy

2012/5/11

#
erdelf, Along with the code you supplied and a little work else where, it now works. I also ended up adjusting these numbers: doSomething( 14, 25, "dog-dance-1.png", "dog-dance-2.png"); to 8 and 25. I think I was told that 14 was the times the images were switched and the 25 was the time between the switches. Is this correct? I will keep looking at your code to try to understand it. Thank you.
Duta Duta

2012/5/11

#
To help you understand erdelf's code it might be useful to know that
if(!dancing) /*is the same as*/ if(dancing == false) /*or*/ if(dancing != true)
Loopy Loopy

2012/5/11

#
Thanx Duta but what does the ! mean?
trash1000 trash1000

2012/5/12

#
The exclamation mark translates to 'not'. Like in "if not dancing".
You need to login to post a reply.