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

2013/3/13

Why won't this work?

henrYoda henrYoda

2013/3/13

#
My enemy is supposed to wait before each shot but for some reason it doesn't, here is the code for my enemy, shooting is in the shoot method.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)  
import java.util.List;
public class Key extends Actor  
{  
private int wait = 0;
private int shootWait = 0;    
private final int waitTime = 35;
private final int shootWaitTime = 40;  
private boolean touchingPlayer = false;  
private boolean canShoot = false;
   /** 
     * Create a new key. 
     */  
public void act()  
{   
    checkShootingStatus();
    checkTouchingStatus();
    hitPlayer(); 
    die();
    if (getWorld() == null)return;
    movementAi();
    shoot();
    
}

  public void die()
    {
        menu menuWorld = (menu) getWorld();  
        Bar scoreBar = menuWorld.getScoreBarValue(); 
        
        if (getHealth() <= 0)
        {
        scoreBar.add(50);
        getWorld().removeObject(this);
    } 
        
       
        
        
        
        }
          
    
   private void checkTouchingStatus()    
     {    
    if (wait > 0) wait--;    
    if (wait == 0) touchingPlayer = false;  
}  
private void checkShootingStatus()    
     {    
    if (wait > 0) shootWait--;    
    if (wait == 0) canShoot = false;  
}      
//movement    
    
  public void movementAi()   
  {  
        
    move(2);  
    if(Greenfoot.getRandomNumber(100) < 10)  
    {  
        turn(Greenfoot.getRandomNumber(90));     
      
    }  
    if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)  
    {  
        turn(180);  
      
    }  
    if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)  
    {  
       turn(180);  
      
    }  
}  
//move to player
 
    
   
    
  
//Enemy health  
    private int health = 3;//or any other starting value;    
        
    public void setHealth(int points) {    
        health += points;    
    }    
    public int getHealth() {    
        return health;    
    }    
      
         public void hitPlayer() {    
    key2 key2 = (key2) getOneObjectAtOffset(0, 0, key2.class);    
    if (key2 != null && !touchingPlayer) {    
         touchingPlayer = true;    
            wait += waitTime;    
        menu menuWorld = (menu) getWorld(); //Do this every time to call method!!!  
          Bar healthBar = menuWorld.getHealthBarValue();  
          healthBar.subtract(1);//decrements the health of your player  
         
          
          
        
      
  
}  
}
//shoot
    public void shoot()
    {
    List key2 = getObjectsInRange(200, key2.class);  
    if (! key2.isEmpty() && !canShoot)
    {
        canShoot = false;    
            shootWait += shootWaitTime;  
     
           
        menu menuWorld = (menu) getWorld();
           key2 player = menuWorld.getPlayerPos();
            enemyBullet enemyBullet = new enemyBullet();
            getWorld().addObject(enemyBullet, getX(), getY());
            enemyBullet.turnTowards(player.getX(), player.getY());
      
   }
        
        
        
        
        
            
          
            
}  
}
 
  
      
henrYoda henrYoda

2013/3/13

#
I'm not sure if anybody missed this, but does anyone know why it won't work? I can't fix it..
MatheMagician MatheMagician

2013/3/13

#
I am slightly confused as to what you are trying to accomplish with the four variables wait, shootWait, waitTime, and shootWaitTime. Could you briefly explain? If you want to make your actor wait, say 42 act cycles, you would store a time variable and have code like this:
time++;
if(time>42){
       canShoot = true;
       time = 0;
}
You would call this code every act cycle and it would set your shoot variable true whenever 42 more cycles had elapsed.
henrYoda henrYoda

2013/3/13

#
Basically what I want it to do is for it to have a delay between each shot, I tried implementing your code but for some reason it still doesn't have a delay, could you please show me how its done? Thanks :D
henrYoda henrYoda

2013/3/13

#
Never mind, I fixed it, You are the best man, your code really helped me :)
MatheMagician MatheMagician

2013/3/14

#
You're welcome!
You need to login to post a reply.