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

2020/3/26

Stopping music if Link dies

Zyrtex1 Zyrtex1

2020/3/26

#
Hello yet again. I have a problem with my Sound. I want to stop the Sound is my "isGameOver= true". The thing is the OverWorld theme plays in my normal "MyWorld" and I don't know how to stop the OverWorld theme in my actor. here is the code for "Link" and the 2nd one is for "MyWorld" Thanks for the help in advance.
Link:
public class Link extends Actor 
{  
    /**
     * Act - do whatever Link wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    // Pictures for Links movement and sword attack.
    private GreenfootImage Down0 = new GreenfootImage("linkDown0.png");
    private GreenfootImage Down1 = new GreenfootImage("linkDown1.png");
    private GreenfootImage Left0 = new GreenfootImage("linkLeft0.png");
    private GreenfootImage Left1 = new GreenfootImage("linkLeft1.png");
    private GreenfootImage Right0 = new GreenfootImage("linkRight0.png");
    private GreenfootImage Right1 = new GreenfootImage("linkRight1.png");
    private GreenfootImage Up0 = new GreenfootImage("linkUp0.png");
    private GreenfootImage Up1 = new GreenfootImage("linkUp1.png");
    private GreenfootImage swordDown = new GreenfootImage("linkSwordDown.png");
    private GreenfootImage swordLeft = new GreenfootImage("linkSwordLeft.png");
    private GreenfootImage swordUp = new GreenfootImage("linkSwordUp.png");
    private GreenfootImage swordRight = new GreenfootImage("linkSwordRight.png");
    
    //Sounds for Link
    private GreenfootSound Sword = new GreenfootSound("LOZ_Sword.wav");
    private GreenfootSound Die = new GreenfootSound("LOZ_Die.wav");
    
    
    
    //Links health
    public static int health = 5;
    
    //Variable for Animation
    private static int spin = 4;
    
    //Links attack
    public static int dmg = 1;
    
    private boolean pressed = false; //checks if attack button got pressed
    
    public static boolean isAttacking = false;
    public static boolean isGameOver = false;
    public static boolean isKnockback = false;
     
    public void act() 
    {
        keyMove();
        attack();
        wall();
        
 
       //Checks if Link has ran out of hearts --> Leading to GameOver
        if(isGameOver = true){
            
            Die.play();
            Die.setVolume(75);
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            setImage("deadLink.png");
            //move(Direction.DOWN, 500);
            this.getImage().setTransparency(0);
            Greenfoot.setWorld(new GameOverScreen());
            
             
            
        }
        
    }   
     
    public void keyMove()
    {
      if(Greenfoot.isKeyDown("Right")){
           setLocation(getX()+5, getY());
            if (getImage() == Right0) {
            setImage (Right1);
        }   else {
            setImage(Right0);
        }   
        }
          
        if(Greenfoot.isKeyDown("Left")){
           setLocation(getX()-5, getY());
            if (getImage() == Left0) {
            setImage (Left1);
        }   else {
            setImage(Left0);
             
        }   
        }
         
         
        if(Greenfoot.isKeyDown("Up")){
           setLocation(getX(), getY()-5);
            if (getImage() == Up0) {
            setImage (Up1);
        }   else {
            setImage(Up0);
        }   
        }
          
        if(Greenfoot.isKeyDown("Down")){
           setLocation(getX(), getY()+5);
            if (getImage() == Down0) {
            setImage (Down1);
        }   else {
            setImage(Down0);
            
        }
        }
        
        
    }
    
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    
    public void wall() {
        if(canSee(Wall.class) || canSee(Tree.class)) {
            if(getImage() == Right0 || getImage() == Right1 || getImage() == swordRight) {
                setLocation(getX() - 5 , getY());
            }
            if(getImage() == Left0 || getImage() == Left1 || getImage() == swordLeft) {
                setLocation(getX() + 5, getY());
            }
            if(getImage() == Up0 || getImage() == Up1 || getImage() == swordUp) {
                setLocation(getX() , getY() + 5);
            }
            if(getImage() == Down0 || getImage() == Down1 || getImage() == swordDown) {
                setLocation(getX(), getY() - 5);
            }
        }
    }
    
    public void attack() {
            if(Greenfoot.isKeyDown ("space")){
                if(!pressed) {
                if (getImage() == Right0 || getImage() == Right1) {
               setImage(swordRight);
            }
            else {
               if (getImage() == Left0 || getImage() == Left1) {
                   setImage (swordLeft);
                }
               if (getImage() == Up0 || getImage() == Up1) {
                   setImage (swordUp);
                }
               if (getImage() == Down0 || getImage() == Down1) {
                   setImage (swordDown);
                }
            }
            Octorock enemy = (Octorock) getOneIntersectingObject(Octorock.class);
            if (enemy != null)
            {
                enemy.takeDamage(dmg);
            }
            Sword.play();
            Sword.setVolume(75);
            pressed = true;
            
             if (pressed = false) {
            if (getImage() == swordUp) {
                setImage (Up0);
            }
            if (getImage() == swordDown) {
                setImage (Down0);
            }
            if (getImage() == swordLeft) {
                setImage (Left0);
            }
            if (getImage() == swordRight) {
                setImage (Right0);
            }
        }
        }
        
    }
    }   
    
    public void takeDamage(int dmg) {
        health -= dmg;
        if (health < 1) {getWorld().removeObject(this);
        isGameOver = true;
    }
    }
    
    public void removeHearts() {
        if(isTouching(Projectile.class)) {
            health -=dmg;
            if (health < 1) {
                isGameOver = true;
            }
        }
    }
    
    
    
    
}   


MyWorld:
public class MyWorld extends World
{
    public int abstand = 120;
    GreenfootSound OverWorld = new GreenfootSound("Overworld.mp3");
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        
        setBackground(new GreenfootImage("background.png"));
        
        Link link = new Link();
        addObject (link, 400, 300);
        
        Octorock Octo = new Octorock();
        addObject (Octo, 100, 100);
        
        Octorock Octo2 = new Octorock();
        addObject (Octo2, 400, 200);
        
        Octorock Octo3 = new Octorock();
        addObject (Octo3, 600, 500);
        
        
        health Heart = new health();
        addObject (Heart, 770,30);
        
        health Heart1 = new health();
        addObject (Heart1, 750, 30);
        
        health Heart2 = new health();
        addObject (Heart2, 730, 30);
       
        health Heart3 = new health();
        addObject (Heart3, 710, 30);
        
        health Heart4 = new health();
        addObject (Heart4, 690, 30);
        for(int i=1;i<6;i++){
        Wall Wall_i = new Wall();
        addObject (Wall_i,737 - abstand, 537 - abstand);
    }
    
        Wall Wall_i = new Wall();
        addObject (Wall_i, 737,537);
        
        
        /* For inserting the UI Bar. */
        /*
        Bar UIBar = new Bar();
        addObject (UIBar,400,20);
        */
        Life Life = new Life();
        addObject (Life,732,15);
        
        
        OverWorld.play();
        OverWorld.setVolume(25);
        
        
        
        
        setPaintOrder (health.class, Life.class, Bar.class,Link.class, Wall.class,Tree.class); 
    }
    
    
    
}
danpost danpost

2020/3/26

#
Line 53 in Link:
((MyWorld)getWorld()).OverWorld.stop();
Zyrtex1 Zyrtex1

2020/3/26

#
nice thank you again danpost!
You need to login to post a reply.