【问题标题】:New array for music symbols音乐符号的新数组
【发布时间】:2022-06-19 21:57:49
【问题描述】:

我有来自图书馆的背景音乐,有什么想法可以通过单击按钮来解决此代码以更改它吗?

var BeachBall: CrashBash_BeachBall = new CrashBash_BeachBall();
var BrickDive: LEGOIsland2_BrickDive = new LEGOIsland2_BrickDive();
var BloocheepOcean: MarioHoops_BloocheepOcean = new MarioHoops_BloocheepOcean();
var Underwater: MarioTeachesTyping2_Underwater = new MarioTeachesTyping2_Underwater();
var UnderPressure: CrashBandicootNST_UnderPressure = new CrashBandicootNST_UnderPressure();
var CalmWaters: DireDireDocks_CalmWaters = new DireDireDocks_CalmWaters();
var Level1: TreasureCove_Level1 = new TreasureCove_Level1();
var Level2: TreasureCove_Level2 = new TreasureCove_Level2();
var Level3: TreasureCove_Level3 = new TreasureCove_Level3();
        
var music: Array = new Array(CalmWaters, BloocheepOcean, UnderPressure, Underwater, BeachBall, BrickDive, Level1, Level2, Level3); 
             
// Changing the Background Music
music_btn.addEventListener(MouseEvent.CLICK, on_pressMusic);
  music[Math.floor(Math.random()*music.length)].play();
}

游戏背景更新:

if(!sound){
    sound_channel = CalmWaters.play(0, int.MAX_VALUE);
    sound_channel.soundTransform = new SoundTransform(0.4); 
}

错误:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
        at BlowfishPong_fla::MainTimeline/on_pressMusic()[BlowfishPong_fla.MainTimeline::frame27:205]

【问题讨论】:

    标签: arrays actionscript-3 flash audio background-music


    【解决方案1】:

    试试这样的:

    var BeachBall: CrashBash_BeachBall = new CrashBash_BeachBall();
    var BrickDive: LEGOIsland2_BrickDive = new LEGOIsland2_BrickDive();
    var BloocheepOcean: MarioHoops_BloocheepOcean = new MarioHoops_BloocheepOcean();
    var CalmWaters: DireDireDocks_CalmWaters = new DireDireDocks_CalmWaters();
    var Level1: TreasureCove_Level1 = new TreasureCove_Level1();
    var Level2: TreasureCove_Level2 = new TreasureCove_Level2();
    var Level3: TreasureCove_Level3 = new TreasureCove_Level3();
    var Underwater: MarioTeachesTyping2_Underwater = new MarioTeachesTyping2_Underwater();
    var UnderPressure: CrashBandicootNST_UnderPressure = new CrashBandicootNST_UnderPressure();
    
    var myNum :uint = 0;
    var audioTime :uint = 0;
    var music_object :Sound;
    var music_player :SoundChannel = new SoundChannel();
    var is_Playing :Boolean = false;
    
    var music_list :Array = new Array();
    music_list =    [   BeachBall, BrickDive, BloocheepOcean, CalmWaters, 
                        Level1, Level2, Level3, Underwater, UnderPressure 
                    ]; 
                 
    //# Changing the Background Music
    music_btn.addEventListener(MouseEvent.CLICK, on_pressMusic);
    
    function on_pressMusic (event:MouseEvent) : void  
    {
        if ( is_Playing == true ) //# if music is already playing 
        { music_player.stop(); }
        
        myNum = Math.floor( Math.random() * (music_list.length-1) );
        music_object = new ( music_list[ myNum ] ); 
        
        audioTime = 0; //# seconds to start from within track
        music_player = music_object.play( audioTime );
        is_Playing = true;
      
    }
    

    【讨论】:

    • 好的。这段代码怎么样: if(!sound){ sound = new Sound(); sound.load(new URLRequest('E:/Flash Animations/Folder 1/2-Depths of Aquatron/Bit-Bot Math Voyage/Music/Dire Dire Docks Remix - Calm Waters.mp3')); sound_channel = sound.play(0, int.MAX_VALUE); SoundMixer.soundTransform = new SoundTransform(0.4); }
    • 但是当我点击按钮时出现错误:“#1007: 在非构造函数上尝试实例化。”
    • 有两个代码。错误来自哪里,是 my 显示的代码还是 your 显示的代码?无论如何...构造函数使用new 所以用 new 关键字替换任何行...在我的代码中这意味着:尝试music_object = new... 变为music_object = music_list[ myNum ]; 如果仍然无法正常工作,然后使用构造函数但从 Array 加载您的音频,例如:music_object = new Sound(); music_object.load( music_list[0] );。一旦你可以让 music_list 数组中的第一首曲目正常工作,请将 0 替换为 myNum。
    • PS:如果错误出现在您的代码中:...我只能看到两个构造函数new Sound 和new SoundTransform,所以这两个构造函数之一会给您带来问题。而不是SoundMixer.soundTransform = ... 尝试改为sound_channel.soundTransform = new SoundTransform(0.4);。 PS:您不应该在每次单击按钮时创建new Sound。在任何函数之外,只需创建一次 Sound 变量,然后在函数内部,每当单击按钮时,您都会使用新的音频路径更新相同的 Sound。
    • 然后当我点击home键返回主页面时,音乐还在播放……
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多