【问题标题】:FlashDevelop sound problemsFlashDevelop 声音问题
【发布时间】:2015-12-08 02:34:01
【问题描述】:

我的目标是创建一个 Asteroids 克隆,我有游戏集,如 chrism 网络教程中所示。我几乎完成了游戏设置,但是,当我添加声音时,程序抱怨以下错误。

\src\Main.as(21): col: 3 Error: Access of undefined property soundClip.

\src\Main.as(20): col: 17 错误:找不到类型或不是编译时常量:声音。

代码如下:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author Chris Moeller
     * http://www.chrismweb.com
     * Tutorial: Creating an Asteroids Game: Part 4
     */

    public class Main extends Sprite 
    {
        [Embed(source = "snd/9mmshot.mp3")]

        private const embeddedSound:Class;
        var soundClip:Sound = new embeddedSound(); // Bullet
        soundClip.play(); // Play the sound
        private var game:Game;
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            //create the game object passing in the swf width and height
            game = new Game(stage.stageWidth, stage.stageHeight);

            //add the game bitmap to the screen/ Main.as Sprite to make it visible
            addChild(game.bitmap);

            //Create the main game loop
            addEventListener(Event.ENTER_FRAME, Run);


            //add keylisteners
            stage.addEventListener(KeyboardEvent.KEY_DOWN, game.KeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, game.KeyUp);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, game.MouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, game.MouseUp);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, game.MoveMouse);
        }

        private function Run(e:Event):void
        {
           game.Update();
           game.Render();            
        }   
    }   
}

我已经尝试研究这个问题,但没有运气,我什至检查了一些使用该程序添加声音的教程。

我什至降低了网上找到的音质

但这里没有运气,我不知道发生了什么。请帮忙!! 任何帮助将不胜感激! 我使用在线音频转换器来转换音频。

【问题讨论】:

    标签: actionscript-3 flash flashdevelop


    【解决方案1】:

    为避免您的第一个错误 (Error: Type was not found or was not a compile-time constant: Sound.),您必须将 Sound 类导入您的类:

    // ...
    
    import flash.media.Sound;
    

    对于第二个错误,你应该知道你不能在函数之外使用你的soundClip对象,那部分只是为了声明(和初始化)你的对象,所以你可以这样做,例如:

    private function shotFunction(): void 
    {
        soundClip.play();
    }
    

    希望能有所帮助。

    【讨论】:

    • 第一个错误已修复,但是,现在程序开始抱怨警告说,\src\Main.as(21): col: 7 Warning: var 'soundClip' will be scoped to默认命名空间:Main:内部。它在这个包之外是不可见的。我现在有以下代码 public class Main extends Sprite { [Embed(source = "snd/9mmshot.mp3")] private const embeddedSound:Class; var soundClip:Sound = new EmbeddedSound(); // 子弹私有函数 shotFunction(): void { soundClip.play();研究没有帮助,但错误消失了。请帮我!!我几乎解决了这个问题!
    • @JohnDoeLuis 你可以做到public : public var soundClip:Sound = new embeddedSound(); ...
    • 程序现在抱怨这个 \src\Main.as(21): col: 19 Error: Attribute is invalid.,我几乎没有错误了。请帮忙!!完整代码在这里(".public class Main extends Sprite { [Embed(source = "snd/9mmshot.mp3")] private const embeddedSound:Class; public : public var soundClip:Sound = new embeddedSound(); // PlaySound private var game:Game; public function Main():void") 。我的最终目标是为游戏添加声音。
    • @JohnDoeLuis 应该是public var soundClip:Sound = new embeddedSound();,冒号(:)是给你解释怎么设置的public
    • 很遗憾,我遇到了同样的错误。我不知道我做错了什么,我复制了这个值,但仍然得到同样的错误。这是我用来添加声音的教程。 as3tutorialblog.blogspot.com/2009/05/… 。请帮帮我!!
    猜你喜欢
    • 2016-03-26
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    相关资源
    最近更新 更多