按下键时停止功能

时间:2014-04-24 18:17:10

标签: actionscript-3 flash-cs6

我有行星绕中心点旋转,如果按下'g'键一次,当行星经过一个平面时,hittestobject触发每个行星的单独声音。按下“e”键时,它会为每个行星播放不同的声音。此时声音在彼此之上(在顶部)播放。如何在按下g或e键时检查是否正在播放其中一种声音并将其关闭? 我感谢任何帮助。

import flash.media.SoundChannel;
import flash.events.Event;

var sunmc:sun = new sun(); 
var mcp1:mc1 = new mc1();
var mcp2:mc2 = new mc2(); 
var mcp3:mc3 = new mc3();
var mcp4:mc4 = new mc4(); 
var mcp5:mc5 = new mc5();
var mcp6:mc6 = new mc6(); 
var mcp7:mc7 = new mc7();
var mcp8:mc8 = new mc8(); 

//------------soundsGrav
var mercGrav:Sound = new gravMerc();
var gravMercChannel:SoundChannel;
var mercSoundIsPlaying:Boolean = true;

var venusGrav:Sound = new gravVenus();
var gravVenusChannel:SoundChannel;
var venusSoundIsPlaying:Boolean = true;

var earthGrav:Sound = new gravEarth();
var gravEarthChannel:SoundChannel;
var earthSoundIsPlaying:Boolean = true;

var marsGrav:Sound = new gravMars();
var gravMarsChannel:SoundChannel;
var marsSoundIsPlaying:Boolean = true;

var jupGrav:Sound = new gravJup();
var gravJupChannel:SoundChannel;
var jupSoundIsPlaying:Boolean = true;

var satGrav:Sound = new gravSat();
var gravSatChannel:SoundChannel;
var satSoundIsPlaying:Boolean = true;

var uraGrav:Sound = new gravUra();
var gravUraChannel:SoundChannel;
var uraSoundIsPlaying:Boolean = true;

var neptGrav:Sound = new gravNept();
var gravNeptChannel:SoundChannel;
var neptSoundIsPlaying:Boolean = true;

var hitTest:Boolean = false;
//------------

//------------soundsOrbital Eccentricity
var mercOrbEcc:Sound = new orbEccMerc();
var orbEccMercChannel:SoundChannel;
var mercEccSoundIsPlaying:Boolean = true;

var venusOrbEcc:Sound = new orbEccVenus();
var orbEccVenusChannel:SoundChannel;
var venusEccSoundIsPlaying:Boolean = true;

var earthOrbEcc:Sound = new orbEccEarth();
var orbEccEarthChannel:SoundChannel;
var earthEccSoundIsPlaying:Boolean = true;

var marsOrbEcc:Sound = new orbEccMars();
var orbEccMarsChannel:SoundChannel;
var marsEccSoundIsPlaying:Boolean = true;

var jupOrbEcc:Sound = new orbEccJup();
var orbEccJupChannel:SoundChannel;
var jupEccSoundIsPlaying:Boolean = true;

var satOrbEcc:Sound = new orbEccSat();
var orbEccSatChannel:SoundChannel;
var satEccSoundIsPlaying:Boolean = true;

var uraOrbEcc:Sound = new orbEccUra();
var orbEccUraChannel:SoundChannel;
var uraEccSoundIsPlaying:Boolean = true;

var neptOrbEcc:Sound = new orbEccNept();
var orbEccNeptChannel:SoundChannel;
var neptEccSoundIsPlaying:Boolean = true;

var hitTestOrb:Boolean = false;
//------------


this.addChild(sunmc);
sunmc.x = 400;
sunmc.y = 400;

this.addChild(mcp1);
this.addChild(mcp2);
this.addChild(mcp3);
this.addChild(mcp4);
this.addChild(mcp5);
this.addChild(mcp6);
this.addChild(mcp7);
this.addChild(mcp8);

var allSpeed:Number=1.1;

var p1Speed:Number=.0479;   //   88.0 /5 =    17.6  
var p1Radius:Number=70;     // 57.9
var p1Orbit:Number=0;

var p2Speed:Number=.035;    //  224.7 /5 =    44.94
var p2Radius:Number=100;    //10.8.2
var p2Orbit:Number=0; 

var p3Speed:Number=.0298;   //  365.2 /5 =    73.04
var p3Radius:Number=130;    //14.9.6
var p3Orbit:Number=0; 

var p4Speed:Number=.0241;     //687.0 /5 =   137.4
var p4Radius:Number=160;    //22.7.9
var p4Orbit:Number=0; 

var p5Speed:Number=.0131;   // 4331.0 /5 =   866.2
var p5Radius:Number=190;    //77.8.6
var p5Orbit:Number=0;

var p6Speed:Number=.0097;   //10747.0 /5 =  2149.4
var p6Radius:Number=230;    //143.3.5
var p6Orbit:Number=0; 

var p7Speed:Number=.0068;   //30589.0 /5 =  6117.8
var p7Radius:Number=270;    //287.2.5
var p7Orbit:Number=0; 

var p8Speed:Number=.0054;   //59800.0 /5 = 11960.0
var p8Radius:Number=360;    //449.5.1
var p8Orbit:Number=0; 
//-----------


//-----------changing speed of orbit
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown (e:KeyboardEvent):void{

if (e.keyCode == Keyboard.LEFT){
p1Speed /= allSpeed;
p2Speed /= allSpeed;
p3Speed /= allSpeed;
p4Speed /= allSpeed;
p5Speed /= allSpeed;
p6Speed /= allSpeed;
p7Speed /= allSpeed;
p8Speed /= allSpeed;
} 

if (e.keyCode == Keyboard.RIGHT){
p1Speed *= allSpeed;
p2Speed *= allSpeed;
p3Speed *= allSpeed;
p4Speed *= allSpeed;
p5Speed *= allSpeed;
p6Speed *= allSpeed;
p7Speed *= allSpeed;
p8Speed *= allSpeed;
} 
}
//----------

this.addEventListener(Event.ENTER_FRAME, planetOrbits);  
function planetOrbits(myEvent:Event):void {

    p1Orbit+=p1Speed;//*allSpeed;
    mcp1.x=Math.cos(p1Orbit)*p1Radius+sunmc.x;
    mcp1.y=Math.sin(p1Orbit)*p1Radius+sunmc.y;

    p2Orbit+=p2Speed;
    mcp2.x=Math.cos(p2Orbit)*p2Radius+sunmc.x;
    mcp2.y=Math.sin(p2Orbit)*p2Radius+sunmc.y;

    p3Orbit+=p3Speed;
    mcp3.x=Math.cos(p3Orbit)*p3Radius+sunmc.x;
    mcp3.y=Math.sin(p3Orbit)*p3Radius+sunmc.y;

    p4Orbit+=p4Speed;
    mcp4.x=Math.cos(p4Orbit)*p4Radius+sunmc.x;
    mcp4.y=Math.sin(p4Orbit)*p4Radius+sunmc.y;

    p5Orbit+=p5Speed;
    mcp5.x=Math.cos(p5Orbit)*p5Radius+sunmc.x;
    mcp5.y=Math.sin(p5Orbit)*p5Radius+sunmc.y;

    p6Orbit+=p6Speed;
    mcp6.x=Math.cos(p6Orbit)*p6Radius+sunmc.x;
    mcp6.y=Math.sin(p6Orbit)*p6Radius+sunmc.y;

    p7Orbit+=p7Speed;
    mcp7.x=Math.cos(p7Orbit)*p7Radius+sunmc.x;
    mcp7.y=Math.sin(p7Orbit)*p7Radius+sunmc.y;

    p8Orbit+=p8Speed;
    mcp8.x=Math.cos(p8Orbit)*p8Radius+sunmc.x;
    mcp8.y=Math.sin(p8Orbit)*p8Radius+sunmc.y;
} 


function setup() {
    stage.addEventListener(KeyboardEvent.KEY_DOWN, displayKey);
}
setup();

function displayKey(keyEvent:KeyboardEvent) {

    if (keyEvent.keyCode == 71) {

        addEventListener(Event.ENTER_FRAME, hitTestmerc);

        function hitTestmerc(evt:Event){

            if(mcp1.hitTestObject(mchit)){

                if(!hitTest){
                gravMercChannel = mercGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp2.hitTestObject(mchit)){

                if(!hitTest){
                gravVenusChannel = venusGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp3.hitTestObject(mchit)){

                if(!hitTest){
                gravEarthChannel = earthGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp4.hitTestObject(mchit)){

                if(!hitTest){
                gravMarsChannel = marsGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp5.hitTestObject(mchit)){

                if(!hitTest){
                gravJupChannel = jupGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp6.hitTestObject(mchit)){

                if(!hitTest){
                gravSatChannel = satGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp7.hitTestObject(mchit)){

                if(!hitTest){
                gravUraChannel = uraGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }

        if(mcp8.hitTestObject(mchit)){

                if(!hitTest){
                gravNeptChannel = neptGrav.play();
                hitTest = true;
                }
                else{
                hitTest = false;
                }
            }
        }

    }


    else if (keyEvent.keyCode == 69) {  

        addEventListener(Event.ENTER_FRAME, hitTestmercEcc);

        function hitTestmercEcc(evt:Event){

            if(mcp1.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccMercChannel = mercOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp2.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccVenusChannel = venusOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp3.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccEarthChannel = earthOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp4.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccMarsChannel = marsOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp5.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccJupChannel = jupOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp6.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccSatChannel = satOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp7.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccUraChannel = uraOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        if(mcp8.hitTestObject(mchit)){

                if(!hitTestOrb){
                orbEccNeptChannel = neptOrbEcc.play();
                hitTestOrb = true;
                }
                else{
                hitTestOrb = false;
                }
            }

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您不会在按键检测时删除任何一个enterframe侦听器。请注意,您有addEventListener(Event.ENTER_FRAME, hitTestmercEcc);但没有调用相应的removeEventListener(Event.ENTER_FRAME, hitTestmercEcc);。因此,两个功能同时工作,产生两组声音。在你的情况下,你最好有一个标志,告诉你想要播放什么样的声音,并从一个事件监听器调用相应的函数。像这样:

var ecc:Boolean=false; // if calling Ecc function
function onEnterFrame(e:Event):void {
    if (ecc) hitTestmercEcc(e);
    else hitTestmerc(e);
}

现在,只要按下这些键,就可以像这样更改ecc的值:

function displayKey(keyEvent:KeyboardEvent) {
    if (keyEvent.keyCode == 71) { 
        ecc = false;
    } else if (keyEvent.keyCode == 69) { 
        ecc = true;
    }
}

仍然需要您的最热门功能,但请确保它们位于任何其他功能体内 NOT 。你现在他们都在function displayKey()大括号下 - 这种做法并不好。主要是因为你没有从其他地方访问嵌套函数,它可能会让你面临丑陋的问题。