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

2017/11/7

Issue with a function

Beamo Beamo

2017/11/7

#
I've been testing this function:
   public void turnr(){
       GreenfootImage left = new GreenfootImage("enemyidle1left.png");
       GreenfootImage right = new GreenfootImage("enemyidle1.png");
       left.scale(69,124);
       right.scale(69,124);
       ttimer++;
       if(ttimer==30){

           if(getImage()==left){
               setImage(right);
           }
           if(getImage()==true){
               setImage(left);
           }
           ttimer = 0;
       }
   }
However, the method only runs once. I inspected the class after waiting 20 seconds, and it is setting the variable ttimer back to 0, yet it doesn't seem to be recognising that the image is set to the image class "left". Any ideas?
Super_Hippo Super_Hippo

2017/11/7

#
In line 12, you need to use "right" instead of "true". If that is still not working, try to move lines 2 and 3 out of the method and scale them once in the constructor.
Beamo Beamo

2017/11/8

#
Oh! The true was used when I was testing it against a condition in a boolean. Whoops! I fixed it and it didn't seem to turn at all. After that I tried moving lines two and three out of that function and placing them in the constructor. However, it didn't recognise the variables that were in the constructor. Not sure whats going wrong. Here's the classes code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DonGrunt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DonGrunt extends Actor
{
    /**
     * Act - do whatever the DonGrunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int timer = 0;
    int ttimer = 0;
    private boolean lookingLeft;
   public DonGrunt(){
       GreenfootImage idleOne = new GreenfootImage("enemyidle1.png");
       idleOne.scale(getImage().getWidth() + 50, getImage().getHeight() + 75);
       setImage(idleOne);
       GreenfootImage left = new GreenfootImage("enemyidle1left.png");
       GreenfootImage right = new GreenfootImage("enemyidle1.png");
       left.scale(69,124);
       right.scale(69,124);
    }
   public void act() 
    {
        turnr();
        shoot();
        idle();
    }    
   private void shoot(){
       /*int herox = getWorld().getObjects(Hero.class).get(0).getX();
       int heroy = getWorld().getObjects(Hero.class).get(0).getY();
       if(herox<getX()&&lookingLeft==true&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }
       if(herox>getX()&&lookingLeft==false&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }*/
   }
   public void turnr(){
       ttimer++;
       GreenfootImage left = new GreenfootImage("enemyidle1left.png");
       GreenfootImage right = new GreenfootImage("enemyidle1.png");
       if(ttimer==30){
           if(getImage()==left){
               setImage(right);
               lookingLeft = false;
           }
           if(getImage()==right){
               setImage(left);
               lookingLeft = true;
           }
           ttimer = 0;
       }
   }
   private boolean checkLeft(){
       GreenfootImage left = new GreenfootImage("enemyidle1left.png");       
       boolean check;
       if(getImage()==left){check=true;}
       else{check=false;}
       return check;
   }
   private void idle(){
       
   }
}
Any ideas?
Super_Hippo Super_Hippo

2017/11/8

#
Lines 2 and 3 (now 22 and 23) should outside be any method. And then you can remove all other lines white create images (49, 50, 64). Line 19 creates another image which is basically the same as "right". Line 20 uses 'getImage' to scale that image. 'getImage' is not idleOne there.
GreenfootImage left = new GreenfootImage("enemyidle1left.png"),
               right = new GreenfootImage("enemyidle1.png");

public DonGrunt()
{
    left.scale(69,124);
    right.scale(69,124);
    setImage(right);
}
Then, when checking, you can use 'getImage()==left' (or right) without creating a new image.
Beamo Beamo

2017/11/9

#
I've attempted this but the condition still isn't being met.
danpost danpost

2017/11/9

#
Beamo wrote...
I've attempted this but the condition still isn't being met.
Did you remove lines 19, 22, 23, 49, 50 and 64? Oh, and you should have an 'idleOne' reference outside the method also.
Beamo Beamo

2017/11/9

#
I've tried to store a reference to the current image in an Image variable, however it still doesn't work. Here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DonGrunt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DonGrunt extends Actor
{
    /**
     * Act - do whatever the DonGrunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   GreenfootImage left = new GreenfootImage("enemyidle1left.png");
   GreenfootImage right = new GreenfootImage("enemyidle1.png");
   int timer = 0;
   int ttimer = 0;
   private boolean lookingLeft;

   public DonGrunt(){
       left.scale(69,124);
       right.scale(69,124);
       setImage(left);
    }
   public void act() 
    {
        turning();
        shoot();
        idle();
    }    
   private void shoot(){
       /*int herox = getWorld().getObjects(Hero.class).get(0).getX();
       int heroy = getWorld().getObjects(Hero.class).get(0).getY();
       if(herox<getX()&&lookingLeft==true&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }
       if(herox>getX()&&lookingLeft==false&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }*/
   }
   public void turning(){
       ttimer=ttimer+1;
       GreenfootImage currentImage = getImage();
       if(ttimer==30){
           if(currentImage!=right){
               setImage(right);
               lookingLeft = false;
           }
           if(currentImage!=left){
               setImage(left);
               lookingLeft = true;
           }
           ttimer = 0;
       }
   }
   private void idle(){
       
   }
}
Thanks for the help!
danpost danpost

2017/11/9

#
Remove line 48 and change remaining 'currentImage' to 'getImage()'. You probably do not need the 'lookingLeft' field at all. In fact, currently the value of that field is not being questioned anywhere. In the commented area, you can always replace the question with 'getImage() == left' (or 'right').
Beamo Beamo

2017/11/9

#
Thanks for your input danpost! However the character still doesn't turn on a cycle. I probably should have represented all of the code, so here is the latest updated version:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DonGrunt here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DonGrunt extends Actor
{
    /**
     * Act - do whatever the DonGrunt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   GreenfootImage left = new GreenfootImage("enemyidle1left.png");
   GreenfootImage right = new GreenfootImage("enemyidle1.png");
   int timer = 0;
   int ttimer = 0;
   private boolean lookingLeft;

   public DonGrunt(){
       left.scale(69,124);
       right.scale(69,124);
       setImage(left);
    }
   public void act() 
    {
        turning();
        shoot();
        idle();
    }    
   private void shoot(){
       /*int herox = getWorld().getObjects(Hero.class).get(0).getX();
       int heroy = getWorld().getObjects(Hero.class).get(0).getY();
       if(herox<getX()&&lookingLeft==true&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }
       if(herox>getX()&&lookingLeft==false&&heroy<getY()+50&&heroy>getY()-50){
           briefcase proj = new briefcase();
           getWorld().addObject(proj,getX(),getY());
           proj.setRotation(getRotation());
       }*/
   }
   public void turning(){
       ttimer=ttimer+1;
       if(ttimer==30){
           if(getImage()==left){
               setImage(right);
               lookingLeft = false;
           }
           if(getImage()==right){
               setImage(left);
               lookingLeft = true;
           }
           ttimer = 0;
       }
   }
   private void idle(){
       
   }
}
Thanks!
danpost danpost

2017/11/9

#
On line 53, use 'else if'.
Beamo Beamo

2017/11/9

#
YES! SUCCESS! THANKS SO MUCH!
You need to login to post a reply.