FlashDevelop sound problems - actionscript-3

My goal is to create an Asteroids clone, I have the game set, as found on the chrism web tutorial. I have the game almost completely set, but, when I add a sound, The program complains with the following errors.
\src\Main.as(21): col: 3 Error: Access of undefined property soundClip.
\src\Main.as(20): col: 17 Error: Type was not found or was not a compile-time constant: Sound.
The code is shown here :
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();
}
}
}
I have tried researching the problem and no luck here, I have even checked some adding sound tutorials using the program.
I have even reduced the sound quality as found online
but no luck here and I have no idea what's going on. Please Help!!
Any Help would be extremely appreciated!
I used online audio converter to convert the audio.

To avoid your first error (Error: Type was not found or was not a compile-time constant: Sound.), you have to import the Sound class to your class :
// ...
import flash.media.Sound;
For the second error, you should know that you can not use your soundClip object outside a function, that part is just to declare (and initialize) your objects, so you can do, for example :
private function shotFunction(): void
{
soundClip.play();
}
Hope that can help.

Related

AS3 debugger stops responding while trying to load image into sprite using Loader

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.
The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

TypeError: Error #1009 with loading external SWF file

I was making 3D File Viewer in Flash Player with AS3.0
and i found AWD Viewer from Away3D Example File.
(http://awaytools.com/awaybuilder/tutorial-01/AwayBuilderTutorial01_SampleFiles.zip)
it works fine.
and i loaded it in my 'Main' swf file. but it's not work. it kept showing error to me.
error message is below
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AWDViewer/initEngine()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:74]
at AWDViewer/init()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:57]
at AWDViewer()[C:\Users\wintec\Desktop\Flash3DViewer\source\AWDViewer.as:49]
and that error line is just this
line 74 : stage.scaleMode = StageScaleMode.NO_SCALE;
line 57 : initEngine();
line 49 : init();
and I know that error message mean there's no properties that name.
I checked that, there's nothing wrong.
also, when I loading another swf file in my 'Main'swf, that's works. weird...
I don't understand why this error kept showing.
please help me.
below code is from Main.as
package
{
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
public class Flash3DViewer extends MovieClip
{
private var _request:URLRequest = new URLRequest("AWDViewer.swf");
private var _loader:Loader = new Loader()
public function Flash3DViewer()
{
init();
}
private function init():void
{
stop();
_loader.load(_request);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, initLoaded);
}
private function initLoaded():void
{
_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, initLoaded);
var extSwf = _loader.content as MovieClip;
swfContainer.addChild(extSwf);
trace("contents loaded");
}
}
}
I found the issue with your application based on the code you provided via DropBox. And just as I suspected, the stage property was being referenced before the object's addition to the stage was completed, which is why the null reference error was being generated.
The AWDViewer class was prematurely calling the stage property from one of the functions that is being called when the init function is called. I've updated the Flash3DViewer.as and AWDViewer.as files with the proper usage of the ADDED_TO_STAGE event so that this does not occur. I have also added comments to the code for you to follow along. Also, I had to modify the init function in the AWDViewer class to take a parameter of type Event to account for the fact that the function is now called when the ADDED_TO_STAGE event fires.
Flash3DViewer.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
public class Flash3DViewer extends MovieClip
{
private var loader:Loader;
public function Flash3DViewer():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
loadSWF("AWDViewer.swf");
}
private function loadSWF(url:String):void
{
var urlRequest:URLRequest = new URLRequest(url);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
loader.load(urlRequest);
addChild(loader);
}
private function onLoaded(e:Event):void
{
var target:AWDViewer = e.currentTarget.loader.content as AWDViewer;
trace(target);
//target.init(); // no longer need to call the init function manually as the AWDViewer calls it when it 'knows' it has been added to the stage. This line can be deleted.
addChild(target);
}
}
}
AWDViewer.as:
public function AWDViewer()
{
/* Used ADDED_TO_STAGE event to know when to trigger the init
function call to avoid the null reference errors when accessing
the 'stage' property */
addEventListener(Event.ADDED_TO_STAGE,init);
// init(); // no longer need to manually call the init function as the ADDED_TO_STAGE event listener will take care of this. This line can be deleted.
}
/**
* Global initialise function
*/
public function init(e:Event):void
{
initEngine();
initListeners();
AssetLibrary.enableParser(AWD2Parser);
//kickoff asset loading
var loader:Loader3D = new Loader3D();
loader.load(new URLRequest("assets/monkey.awd"));
_view.scene.addChild(loader);
}
While I did attempt to compile the code above, and the null reference errors ceased to generate with my corrected code, there were some compiler errors on my machine because of the different configurations of our computers. You'll just need to ensure that these compiler errors do not appear on your machine.
Warning: Library path "C:\Users\wintec\Desktop\3D_VR\source\libs" does not resolve to a valid directory.
Warning: Library path "$(FlexSDK)/frameworks/libs/flex.swc" does not resolve to a valid file.
If you have any other questions, just let me know.
Cheers.
That error means that you are trying to access something which has not been instantiated. You should put some breakpoints and run your app in debug mode when you are not sure what exactly is null.
It's very likely that stage is null in your case. Stage property of DisplayObject is set to the instance of app's stage when this display object is added to stage. However, all parents of this display object should be added to the stage too. Thus, make sure that the instance of Flash3DViewer has the stage before loading AWDViewer.

AS3 - How to get current scene name in class

I'm playing around with flash, and I've created multiple scenes for things like menu's, buttons, etc. When trying to add event handlers for buttons that are in one scene, but not others, the compiler complains saying that it can't reference to objects that don't exist.
I figured the solution to be simple... Get the scene name, match that against an if statement and load the event handlers through the if statements...
However, after digging around on the net for far too long, I just can't seem to find a way to do this properly. Does anyone know a way?
I've tried using the following :
var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;
Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".
Omit MovieClip and scenes as source for organising your project, and code on the timeline. Use Document class as entry point. This tutorial should help you to grasp main concept.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function setup():void {
const padding:int = 20;
//Initiate your UI components and place them in the display list
var menu:MyMenu = new MyMenu();
var buttons:MyFooterButtons = new MyFooterButtons();
var etc:AnotherComponent = new AnotherComponent();
addChild(menu);
addChild(buttons);
addChild(etc);
menu.x = menu.y = padding;
//Place them and initialise with concrete information
}
}
}
For example, MyMenu, MyFooterButtons, AnotherComponent could be MovieClip/Sprite in the library with export settings, where you did all your work with placement, styling, etc
I had a same problem. I wanted to check from an external Class the current scene name and depending on the name (name of the game level) to pass some values in some attributes… So what I did and it worked is that.
//main in 1st frame of the fla
stop();
var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();
//CheckSceneClass
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Scene;
public class CheckSceneClass extends flash.display.MovieClip {
public var myCurrentScene : Scene;
public function CheckSceneClass () {
}
public function checkScene() {
switch (myCurrentScene.name) {
case "Scene 1":
trace("It is the 1st Scene");
break;
default:
trace("Error with Scene Name");
break;
}
}
}
}

Flash TypeError: Error #1009: Cannot access a property or method of a null object reference. - when accessing TLFTextField from Document Class

I'm lost on this one. I receive a TypeError: Error #1009: Cannot access a property or method of a null object reference. output message the first time my Document Class tries to access a simple textfield on the stage (added from the IDE, not actionscript)
package {
import flash.display.*;
import fl.text.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip {
private var _netConnection:NetConnection;
private var _responder:Responder;
/* some other public + private vars */
public function Main() {
init();
}
public function init(e:*=null):void {
_netConnection = new NetConnection();
_responder = new Responder(uponResult);
txt.text = "init()";
}
/* more functions */
}
}
I tried adding txt.addEventListener(Event.ENTER_FRAME, init); incase the txt TLFTextField wasn't ... there... at the beginning, but it still outputs the error.
I feel like a bit of an idiot atm, what's the prognosis doc?
JB
TLFTextFields are weird creatures, I've had a lot of issues with them recently.
I'd try to use the Event.ADDED_TO_STAGE event because the TLFTextFields have to be on the stage when you try to access them:
public function Main() {
addEventListener(Event.ADDED_TO_STAGE, init);
};
public function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
txt.text = "init()";
};
It should work if your TLFTextField is on the first frame on the Main Timeline.
Let me know if this one does the magic,
Rob

as3 error 1063 with timer

getting error 1061: Call to a possibly undefined method stop through a reference with static type flash.events:TimerEvent.
on my as3 class. I'm just starting to learn as3 and cant figure out whats causing the error. code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class game extends MovieClip
{
//assign types to var names
//allows values and variables to be acessed in methods
public var as1:astroid;//astroids
public var ship1:ship;//ship
public var timer:Timer;
public function game()
{
//astroid
as1=new astroid();
addChild(as1);
//ship
ship1=new ship();
addChild(ship1);
//timer
timer=new Timer(25);//every n frames
timer.addEventListener( TimerEvent.TIMER, onTick );//attach function to timer
timer.start();//start timer
}
public function onTick( timer:TimerEvent ):void
{
//animate astroid
as1.moveDown();
//move ship
ship1.x = mouseX;
ship1.y = mouseY;
if(ship1.hitTestObject(as1))
{
timer.stop();//error on this line!
}
}
}
}
Rename timer to event in your event handler:
public function onTick( event:TimerEvent ):void
Also, in Flash CS5, go to File > Publish Setting > Flash, and turn on "Permit debugging". That should give you more useful error messages.