A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: AS3: sound_channel.stop(); -> Doesn't work with ENTER_FRAME handler!

  1. #1
    Junior Member
    Join Date
    May 2015
    Posts
    2

    AS3: sound_channel.stop(); -> Doesn't work with ENTER_FRAME handler!

    Hello.

    I'm working on a platform game.
    When the character falls, I want to play sound.
    When the character touches the platform, I want this sound to be stopped.

    Here is the code concerned:
    Code:
    // "ed" is the name of the instance for the character object
    
    //Main listener
    stage.addEventListener(Event.ENTER_FRAME, gameloop);
    
    //Functions Caller
    function gameloop(e:Event) {
    	SoundFallStart();
    	SoundFallStop();
    } 
    
    //Boolean for contact detection, I traced it a lot, it's working well (by the way, I use simple hitTestPoint class)
    var edPlatformContact:Boolean=false;
    
    //Sound Variable
    var SoundFall:Sound = new Fall_Sound(); 
    
    //Sound Channel
    var ChannelFall:SoundChannel= new SoundChannel();
    
    //Sound Transformer
    var TransformFall = new SoundTransform();
    	
    //Function that start sound when character falls (frame 9 and 10 of the movie-clip correspond to falling animations)
    function SoundFallStart(){
    	if (ed.currentFrame==(9) && edPlatformContact==false || ed.currentFrame==(10) && edPlatformContact==false){
    	ChannelFall = SoundFall.play();
    	TransformFall.volume=0.2;
    	ChannelFall.soundTransform = TransformFall;
    	}
    }
    
    //Function that is trying to stop the sound channel when the character touches the platform.
    function SoundFallStop(){
    	if(edPlatformContact==true){
    		ChannelFall.stop();
    	}
    }
    When I run, I have no issues.
    The sound (long scream) starts but doesn't stop when the character touches the platform.
    I traced the boolean variable, it's working well.

    Does someone has an idea ?
    Thanks for reading

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Without having access to your files, I think you might be getting slightly confused.

    PHP Code:
    // "ed" is the name of the instance for the character object

    //Main listener
    stage.addEventListener(Event.ENTER_FRAMEgameloop);

    //Boolean for contact detection, I traced it a lot, it's working well (by the way, I use simple hitTestPoint class)
    var edPlatformContact:Boolean false;

    //Sound Variable
    var SoundFall:Sound = new Fall_Sound();

    //Sound Channel
    var ChannelFall:SoundChannel= new SoundChannel();

    //Sound Transformer
    var TransformFall:SoundTransform = new SoundTransform();

    //Functions Caller
    function gameloop(e:Event)
    {
        if (
    ed.currentFrame == || ed.currentFrame == 10 && ! edPlatformContact)
        {
            
    edPlatformContact true;
            
    SoundFallStart();
        }
    }

    //Function that start sound when character falls (frame 9 and 10 of the movie-clip correspond to falling animations)
    function SoundFallStart()
    {
        
    trace("hit");
        
    ChannelFall SoundFall.play();
        
    TransformFall.volume 0.2;
        
    ChannelFall.soundTransform TransformFall;
        
    ChannelFall.addEventListener(Event.SOUND_COMPLETESoundFallStop);
    }

    //Function that is trying to stop the sound channel when the character touches the platform.;
    function SoundFallStop(evt:Event)
    {
        
    evt.target.removeEventListener(Event.SOUND_COMPLETESoundFallStop);
        
    evt.target.stop();
        
    edPlatformContact false;

    Last edited by fruitbeard; 05-31-2015 at 05:55 AM.

  3. #3
    Junior Member
    Join Date
    May 2015
    Posts
    2
    Quote Originally Posted by fruitbeard View Post
    Hi,

    Without having access to your files, I think you might be getting slightly confused.

    PHP Code:
    // "ed" is the name of the instance for the character object

    //Main listener
    stage.addEventListener(Event.ENTER_FRAMEgameloop);

    //Boolean for contact detection, I traced it a lot, it's working well (by the way, I use simple hitTestPoint class)
    var edPlatformContact:Boolean false;

    //Sound Variable
    var SoundFall:Sound = new Fall_Sound();

    //Sound Channel
    var ChannelFall:SoundChannel= new SoundChannel();

    //Sound Transformer
    var TransformFall:SoundTransform = new SoundTransform();

    //Functions Caller
    function gameloop(e:Event)
    {
        if (
    ed.currentFrame == || ed.currentFrame == 10 && ! edPlatformContact)
        {
            
    edPlatformContact true;
            
    SoundFallStart();
        }
    }

    //Function that start sound when character falls (frame 9 and 10 of the movie-clip correspond to falling animations)
    function SoundFallStart()
    {
        
    trace("hit");
        
    ChannelFall SoundFall.play();
        
    TransformFall.volume 0.2;
        
    ChannelFall.soundTransform TransformFall;
        
    ChannelFall.addEventListener(Event.SOUND_COMPLETESoundFallStop);
    }

    //Function that is trying to stop the sound channel when the character touches the platform.;
    function SoundFallStop(evt:Event)
    {
        
    evt.target.removeEventListener(Event.SOUND_COMPLETESoundFallStop);
        
    evt.target.stop();
        
    edPlatformContact false;

    Hi,
    Thank you for your input.

    First, I noticed you have used the edPlatformContact boolean in the code.
    I know it might be difficult to solve my problem without the full code, but the hole game engine is approximately 700 code lines....

    So, let's just consider that the edPlatformContact boolean is working well already. I think we don't need to deal with it. I'm not an expert, but I think that it is what you call a "public" variable (?). In other words, the values of this variable (true, false) are defined via the gameloop (30 fps), so when you trace it, it is indeed true when the character is on the platform, and false when the character is in the air.

    So I tried your code.
    Initially, I left the edPlatformContact codes as you did. Unfortunately, as I expected, the result generated small conflicts with the rest of my code. The physic behavior was not exactly the same anymore (because I used this boolean a lot to develop the physic behavior, animations, etc..)

    So I did it again without the EdPlatformContact (without "edPlatformConctact=true" and "edPlatformConctact=false"). The "hit" tracer works, so it appears when the sound starts, it disappear when the character touches the platform. Unfortunately, the channel doesn't stop. The sound keeps on being played till the end of the sample.

    Here is another part of my code that might help you to understand how my physic engine works:
    PHP Code:
    // "platforms" is the instance name of the platform object on stage

    //Other variables
    var vy:Number=0// Y axis speed
    var gv:Number=1// Gravity
    var jumped:Boolean=false//Detect Jump (does the same job than edPlatformContact variable if you want)

    //Function called by the gameloop
    function jumpgravity () { 
        
        
    // making the character fall (gravity and jumping)
        
    vy+=gv// this will make the falling speed up, constantly adds one to velocity
        
    if (!platforms.hitTestPoint(ed.x,ed.y,true)) { 
            
    ed.y+=vy;
            
    edPlatformContact=false;
        }
        
    // moves the character so it is just above the platform, rather than halfway in it
        
    for (var i=0;i<platforms.height;i++) // platforms.height represents the height of the platform
                    

            if (
    platforms.hitTestPoint(ed.x,ed.y,true)) {
                
    ed.y--;
                
    vy=0;
                
    jumped=false;
                
    edPlatformContact=true;
            }    
        }

    Does anyone has an idea ?
    Thanks for reading.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Attach your *.fla or make it available for download.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center