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

2021/5/4

How to Make Object Blink When It Collides With Another Object?

27WhiteLighters 27WhiteLighters

2021/5/4

#
How Do I Make an Object Blink When It Touches Another Object? I don't want the object to just blink once, but continuously until the switch gets toggled off when another object touches said switch. I know I have to use a loop, but I don't know how/where to implement it.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Obstacle that gets switched on and off by being hit.
 */
public class Switch extends Actor
{
    private boolean touched = false;
    private boolean switchedOn = false;
    private GreenfootSound sound = new GreenfootSound ("ping.wav");
    private int i = 0;
       
    public Switch()
    {

    }

    public void act() 
    {
        checkSwitch();
    }    
    
    private void checkSwitch()
    {
        if (!touched && isTouching())
        {
            touched = true;
            switchedOn = !switchedOn;
            
            if (switchedOn)
            {
                sound.play();
                
                setImage("block-light.png");
            }
            
            else
            
            {
                setImage("block.png");
                sound.stop();
                i = 0;
            }
        }
        
        if (touched && !isTouching()) 
        {
            touched = false;
        }
        
        if (switchedOn && !sound.isPlaying()) 
        {
            sound.play();            
        }
    }
    
    private boolean isTouching()
    {
        return (getOneIntersectingObject(Body.class) != null);
    }
}
Risen Risen

2021/5/4

#
Like this?
import greenfoot.*; 
/**
 * Obstacle that gets switched on and off by being hit.
 */
public class Switch extends Actor
{
    private boolean switchedOn = false;
    private GreenfootSound sound = new GreenfootSound ("ping.wav");
    private int i = 0;
    public void act() 
    {
        checkSwitch();
    }    
     
    private void checkSwitch()
    {
        if (isTouching())
        {
            switchedOn = true;
            if (switchedOn)
            {
                sound.play();
                 
                setImage("block-light.png");
            }
             switchedOn = false;
            else
             
            {
                setImage("block.png");
                sound.stop();
                i = 0;
            }
        }  
else {
      setImage("block.png");
                sound.stop();
}
    }
     
    private boolean isTouching()
    {
        return (getOneIntersectingObject(Body.class) != null);
    }
}
27WhiteLighters 27WhiteLighters

2021/5/5

#
@Risen The code you posted does not work, because under line 27, Greenfoot gets an error saying "cannot have 'else' without 'if' or something like that. Sorry I could not reply, my power went out, but thanks for trying! You actually gave me a few ideas, but I'm still open to suggestions.
Risen Risen

2021/5/5

#
27WhiteLighters wrote...
@Risen The code you posted does not work, because under line 27, Greenfoot gets an error saying "cannot have 'else' without 'if' or something like that. Sorry I could not reply, my power went out, but thanks for trying! You actually gave me a few ideas, but I'm still open to suggestions.
Sorry, my fault. Swap lines 25 and 26
27WhiteLighters 27WhiteLighters

2021/5/5

#
Risen wrote...
27WhiteLighters wrote...
@Risen The code you posted does not work, because under line 27, Greenfoot gets an error saying "cannot have 'else' without 'if' or something like that. Sorry I could not reply, my power went out, but thanks for trying! You actually gave me a few ideas, but I'm still open to suggestions.
Sorry, my fault. Swap lines 25 and 26
Yes, It works now! Thanks! Again, my power turned off again hence the late response..
You need to login to post a reply.