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

2017/5/12

delay

Nooooob Nooooob

2017/5/12

#
I want to make an actor wait for a certain time and then be able to move again. It waits but is afterwards not able to move again.
int xSpeed = 1;
timer = 0;

public void stop()
    {
        xSpeed = 0;
        if(timer > 5)
        {
           xSpeed = 1;
           timer = 0;
        }
         timer++;
    }
danpost danpost

2017/5/12

#
Do you call the method from the act method of the class unconditionally? (show the entire class code!)
Yehuda Yehuda

2017/5/12

#
If you call this method in the act method then xSpeed will only == 1 for one act cycle at a time. The setting it to 0 is not in any 'if' statements.
danpost wrote...
Do you call the method from the act method of the class unconditionally? (show the entire class code!)
Nooooob Nooooob

2017/5/12

#
I call the method from the act method of another actor. So, one actor touches an item which makes another actor stop. Other actor looks basically like this:
boolean touchingItem = false;
public void act() 
    {
       stopActor();
    }
     public void stopActor()
    {
        Actor item = getOneIntersectingObject(Item.class);
        if(item != null)
        {
         World myWorld = getWorld();
         MyWorld myworld = (MyWorld)myWorld;
         Actor actor = myworld.getActor();
         if(touchingItem == false)
         {
            Actor.stop(); //That's where I call it
            touchingItem = true;
            if(isTouching(Item.class))
            {
              removeTouching(Item.class);
            }
         }
        }
        else 
        {
              touchingItem = false;
        }
    
Yehuda Yehuda

2017/5/12

#
You don't need to check if you're touching Item since you already have item != null. You can replace lines 18 - 21 with:
getWorld().removeObject(item);
The reason the actor stops but doesn't start again is because when you call the method you set xSpeed to 0 but you don't set it back to 1. The actor will only start moving again after the sixth time you call the method. (The 'A' on line 16 should be lowercase.) You need something in the first actor to start the timer when xSpeed == 0 then when the timer went for long enough make xSpeed = 1 again.
Nooooob Nooooob

2017/5/13

#
Yehuda wrote...
You need something in the first actor to start the timer when xSpeed == 0 then when the timer went for long enough make xSpeed = 1 again.
Like this?
int timer = 0;
int xSpeed = 1;

public void stop()
    {
       xSpeed = 0;
       if(xSpeed == 0)
       {
           timer++;
       }
       if(timer > 5)
       {
          xSpeed = 1;
          timer = 0;
       }
    }
I probably did it wrong since there is no difference
danpost danpost

2017/5/13

#
You know, this is dealing with movement of the actor (or the restricting of it) and nowhere in any of the codes currently supplied do I see a single 'setLocation' or 'move' method call. Again, show the entire class code for better and faster help. I can say that your 'stopActor' method in the other class can simply be this:
public void stopActor()
{
    if (isTouching(Item.class))
    {
        removeTouching(Item.class);
        ((MyWorld)getWorld()).getActor().stop();
    }
}
I would be a good idea to post the code of the 'getActor' method in your MyWorld class as well.
Nooooob Nooooob

2017/5/13

#
Actor1:
int timer = 0;
int xSpeed = 1;
public void act() 
{
       if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - xSpeed, getY()); 
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + xSpeed, getY());
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - xSpeed);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + xSpeed);
        }
}
public void stop()
{
       xSpeed = 0;
       if(xSpeed == 0)
       {
           timer++;
       }
       if(timer > 5)
       {
          xSpeed = 1;
          timer = 0;
       }
}
public void stopActor2()
{
    if (isTouching(Item.class))
    {
        removeTouching(Item.class);
        ((MyWorld)getWorld()).getActor2().stop2();
    }
}
Actor2:
int timer = 0;
int xSpeed = 1;
public void act() 
{
       if (Greenfoot.isKeyDown("a"))
        {
            setLocation(getX() - xSpeed, getY()); 
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + xSpeed, getY());
        }
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() - xSpeed);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + xSpeed);
        }
}
public void stop2()
{
       xSpeed = 0;
       if(xSpeed == 0)
       {
           timer++;
       }
       if(timer > 5)
       {
          xSpeed = 1;
          timer = 0;
       }
}
public void stopActor()
{
    if (isTouching(Item.class))
    {
        removeTouching(Item.class);
        ((MyWorld)getWorld()).getActor().stop();
    }
}
danpost wrote...
I would be a good idea to post the code of the 'getActor' method in your MyWorld class as well.
World:
public Actor getActor()
   {
      return actor;
   }
   public Actor2 getActor2()
   {
      return actor2;
   }
Super_Hippo Super_Hippo

2017/5/13

#
Move everything of the 'stop' method - except the first line - into the act method.
Nooooob Nooooob

2017/5/13

#
Super_Hippo wrote...
Move everything of the 'stop' method - except the first line - into the act method.
That worked! Thanks y'all!
You need to login to post a reply.