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

2017/1/11

If(getImage()==Image)...

wolfyze wolfyze

2017/1/11

#
public void act() {  
GreenfootImage dude=new GreenfootImage("Dude.png");
if(Greenfoot.isKeyDown("p")){
Blueblock bb1=new Blueblock();
if(getImage()==dude){
getWorld().addObject(bb1,getX()+5,getY());
}
}
    }   
(left out some of the code cause it was completely irrelevant) This was supposed to add a block everytime i press P and the character is going right ( i have another image for when he's going left) well it does nothing
wolfyze wolfyze

2017/1/11

#
I just added something: nevermind look at the whole thing
 int countjump=0;
    boolean shouldicount=false;
    public void act() 
    {
        boolean jumps=false;
        int g=0;
        GreenfootImage dude=new GreenfootImage("Dude.png");
        GreenfootImage copydude= new GreenfootImage("DudeCopy.png");
        if(Greenfoot.isKeyDown("Right")){
        setImage(dude);
            setLocation(getX()+2,getY());
        }
        if(Greenfoot.isKeyDown("Left")){
            setImage(new GreenfootImage("DudeCopy.png"));
        setLocation(getX()-2,getY());
        }
        if(Greenfoot.isKeyDown("Up")&&shouldicount!=true){
     shouldicount=true;
    }
        if(shouldicount==true){
        countjump++;
        
        }
        if(countjump>0&&countjump<=20){
        setLocation(getX(),getY()-2);
        }
        if(countjump>20&&countjump<41){
        setLocation(getX(),getY()+2);
        }
        if(countjump==41){
        countjump=0;
        shouldicount=false;
        }
if(Greenfoot.isKeyDown("p")){
Blueblock bb1=new Blueblock();
if(getImage()==dude){
getWorld().addObject(bb1,getX()+5,getY());
}
//this is new>
if(getImage()==copydude){
getWorld().addObject(bb1,getX()-5,getY());
}
}
    }   
   
}
Now the first (old) part works (only when hes walking) and the new one doesnt. (the second image is for when he turns left)
danpost danpost

2017/1/11

#
I would first suggest that you move lines 7 and 8 outside the method so that you are not creating the images every single act cycle. Also, if you use those images to set the image of the actor when necessary (including the initial image, which should be set in the constructor of the class), then your comparison will actually work. The following will always be false:
if (getImage() == new GreenfootImage(imageName)
No matter what 'imageName' is. Even this should always be false (believe it or not):
if (getImage() == new GreenfootImage(getImage())
wolfyze wolfyze

2017/1/11

#
It works now. Thank you!
You need to login to post a reply.