我的目标是创建一个小行星克隆,我有一个游戏集,如在chrism网络教程。我几乎完全设置了游戏,但是,当我添加声音时,程序会抱怨下面的错误。
\src\Main.as(21):col: 3错误:未定义属性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();
}
}
}我已经试着研究这个问题,但在这里没有运气,我甚至检查了一些添加声音教程使用的程序。
我甚至降低了在网上找到的声音质量。
但这里没有运气,我也不知道发生了什么。救命啊!!任何帮助都将不胜感激!我使用在线音频转换器来转换音频。
发布于 2015-12-08 03:17:12
为了避免第一个错误(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();
}希望能帮上忙。
https://stackoverflow.com/questions/34147001
复制相似问题