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

2017/12/17

Need help with enemy self-stunning (like getting tired)

1
2
iminblue iminblue

2017/12/17

#
Hello ! I'm relatively new to Greenfoot and I'm trying to create a big game . For the first level I want to have a battle between Theseus and the Minotaur and I thought of the following combat mechanic : The Minotaur randomly runs through the room and can kill Theseus and there is a chance he gets tired and stops moving and Theseus can kill him (or at least damage him we'll see) .
    private int stunTimer;

    public void act() 
    {
        // Add your action code here.
        Actor tezeu;
        
        if (Greenfoot.getRandomNumber(50)>45)
        { stunTimer = 200000000;
         if (stunTimer>0)   {stunTimer--;
             tezeu = getOneObjectAtOffset(0,0,Tezeu.class);
        if (tezeu!=null) Greenfoot.setWorld (new MyWorld());
        
      }}
      //while (stunTimer>0) stunTimer--;
     
      else   {
       move ();
        tezeu = getOneObjectAtOffset(0,0,Tezeu.class);
        if (tezeu!=null) {
            World world;
      world = getWorld();
    world.removeObject(tezeu);}
    }
    
}
public void move ()
{ move (Greenfoot.getRandomNumber(10)*5);
        turn(10-Greenfoot.getRandomNumber(20));
        if (isAtEdge()) turn (180);
}
That's the code I wrote so far the thing is the Minotaur doesn't really ever stop and if I change the settings and make him stop I can't move Theseus. What should I do ?
danpost danpost

2017/12/17

#
As written, it will take about 3 million seconds for 'stunTimer' to reach zero; but, on average, you are resetting it to 200 million about 4-5 times a second. It has such an infinitesimally small chance of reaching zero, it probably will not happen in your lifetime.
iminblue iminblue

2017/12/21

#
Ok but if I change the settings it would work I mean the Minotaur would eventually stop. The problem is that while he is stunned I can't move Theseus either.
danpost danpost

2017/12/21

#
iminblue wrote...
Ok but if I change the settings it would work I mean the Minotaur would eventually stop. The problem is that while he is stunned I can't move Theseus either.
The code given I presume to be part of the Minotaur class. If the behavior of Theseus is an issue, then its class will probably need looked at.
iminblue iminblue

2017/12/21

#
I don't think so . There's just the code required for moving the Theseus class .
danpost danpost

2017/12/21

#
iminblue wrote...
I don't think so . There's just the code required for moving the Theseus class .
What does your class structure tree look like? That is, from Actor, what extends what?
iminblue iminblue

2017/12/21

#
danpost wrote...
What does your class structure tree look like? That is, from Actor, what extends what?
Well everything else is a subclass of Actor . Is that the problem ?
danpost danpost

2017/12/21

#
iminblue wrote...
everything else is a subclass of Actor . Is that the problem ?
No -- that is fine (just making sure). I doubt I will be able to help without first seeing what you have in your Theseus class.
iminblue iminblue

2017/12/21

#
public void move ()
   {       if (Greenfoot.isKeyDown("up"))
        
           { setRotation(270);
          move(10);  }
        if (Greenfoot.isKeyDown("down"))
        {setRotation(90);
          move(10);  
    }    
    if (Greenfoot.isKeyDown("left"))
        {setRotation(180);
          move(10);  
    }  
     if (Greenfoot.isKeyDown("right"))
        {setRotation(0);
          move(10);  
    }  }
That would be everything that I've done until that point . I'm trying other ways to make this done but I'm still struggling so if you can help me with the original problem it would be amazing.
Super_Hippo Super_Hippo

2017/12/21

#
danpost wrote...
As written, it will take about 3 million seconds for 'stunTimer' to reach zero; but, on average, you are resetting it to 200 million about 4-5 times a second. It has such an infinitesimally small chance of reaching zero, it probably will not happen in your lifetime.
Actually, as it is right now, the stunTimer only decreases when there was the reset in this act cycle, so it will never reach anything below '200000000-1'. (The if-block starting from line 8 ends in 14.)
iminblue wrote...
Ok but if I change the settings it would work I mean the Minotaur would eventually stop. The problem is that while he is stunned I can't move Theseus either.
If your solution is line 15, then this happens, yes. Maybe this helps:
stunTimer--;
if (Greenfoot.getRandomNumber(20*55)==0) //randomly about every 20 seconds
{
    stunTimer = 5*55; //stun for the next 5 seconds
}

Actor tezeu = getOneIntersectingObject(Tezeu.class);
if (tezeu != null) //if minotaur is touching tezeu
{
    getWorld().removeObject( stunTimer>0 ? this : tezeu); //remove minotaur if it is stunned or tezeu if not
}
If you want the 5 seconds (in this case) to be fixed (so no reset in those 5 seconds which will increase the 5 seconds):
if (--stunTimer<0 && Greenfoot.getRandomNumber(20*55)==0) //randomly about every 20 seconds
{
    stunTimer = 5*55; //stun for the next 5 seconds
}
//... and the rest
And if you want a not-stunned time (for example 10 seconds) after it was stunned:
if (--stunTimer<-10*55 && //... the rest
iminblue iminblue

2017/12/21

#
stunTimer--;
if (Greenfoot.getRandomNumber(20*55)==0) //randomly about every 20 seconds
{
    stunTimer = 5*55; //stun for the next 5 seconds
}
 
Actor tezeu = getOneIntersectingObject(Tezeu.class);
if (tezeu != null) //if minotaur is touching tezeu
{
    getWorld().removeObject( stunTimer>0 ? this : tezeu); //remove minotaur if it is stunned or tezeu if not
}
So is this all that should be written in the act() method or do I have to make some changes to fit my program ? I'm a little confused.
danpost danpost

2017/12/21

#
iminblue wrote...
< Code Omitted > So is this all that should be written in the act() method or do I have to make some changes to fit my program ? I'm a little confused.
There is no turning or moving in this block of code -- so, it will have to replace some part of what code you already had (mainly lines 8 through 15).
iminblue iminblue

2017/12/23

#
danpost wrote...
There is no turning or moving in this block of code -- so, it will have to replace some part of what code you already had (mainly lines 8 through 15).
Yeah , I don't think I know how to do that . I tried something but it doesn't work. If anyone could help me further I would appreciate it.
danpost danpost

2017/12/24

#
iminblue wrote...
I tried something but it doesn't work.
What did you try? Show tried code.
iminblue iminblue

2017/12/26

#
danpost wrote...
What did you try? Show tried code.
  public void act() 
    {
        // Add your action code here.
        stunTimer--;
             if (--stunTimer<0*55 && Greenfoot.getRandomNumber(20*55)==0) //randomly about every 20 seconds
  {
    stunTimer = 5*55; //stun for the next 5 seconds
  }
 else move ();
  Actor tezeu = getOneIntersectingObject(Tezeu.class);
  if (tezeu != null) //if minotaur is touching tezeu
  {
    getWorld().removeObject( stunTimer>0 ? this : tezeu); //remove minotaur if it is stunned or tezeu if not
  }
     
}
Yeah I can't really figure it out unfortunately.
There are more replies on the next page.
1
2