"Could not find symbol" - actionscript-3

I'm trying to embed an swf file in this way:
[Embed(source="../assets/assets.swf", symbol="main")]
public var MC:Class;
However, it keeps saying that error, "Could not find symbol main in...".
I've checked that the linkage of the symbol is indeed main.
What else should I check?.
Thanks.
(the file is found, if I remove the embed I can add the entire swf to the stage without problems)

I see nothing wrong with your syntax. The only place that you could be making a mistake is the name of the symbol. Use the ApplicationDomain.getDefinition() and ApplicationDomain.getQualifiedDefinitionNames() APIs to verify that the symbol name is correct.
The other problem might be that the file that contains the symbol is in a different location from the one being embedded. It sounds dumb, but I've done worse things when working on a tight deadline.

Example1:
if structure folder is
src(folder)
--core
-- Assets.as
assets(folder)
-fly01.png
The error because the directory path not extracly ( asset folder outside core folder)
Asset.as you write error:
package core
{
import starling.textures.Texture;
public class Assets
{
[Embed(source="assets/sky.png")]
private static var sky:Class;
public static var skyTexture:Texture;
}
}
Asset.as you write:error
package core
{
import starling.textures.Texture;
public class Assets
{
[Embed(source="../assets/sky.png")]
private static var sky:Class;
public static var skyTexture:Texture;
}
}
Asset.as you write:true
package core
{
import starling.textures.Texture;
public class Assets
{
[Embed(source="../../assets/sky.png")]
private static var sky:Class;
public static var skyTexture:Texture;
}
}

Related

actionscript 3 - Error #2136

So im trying to understand how I can call a function from one class from another class. Im getting a few errors and am wondering if someone can explain what im doing wrong here.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
import code.functions.*;
public class Main extends MovieClip {
public var _playerHP:Number;
public var _enemyYellow:EnemyYellow;
public function Main() {
_enemyYellow = new EnemyYellow;
_playerHP = 10;
_playerHPdisplay.text = _playerHP.toString();
trace("loaded")
}
public function lowerHP ():void
{
_playerHP -= 1;
_playerHPdisplay.text = _playerHP.toString();
trace(_playerHP)
}
}
}
Second File:
package code.functions {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class EnemyYellow extends MovieClip {
public var _main:Main;
public function EnemyYellow() {
_main = new Main;
_main.lowerHP();
trace ("test")
}
}
}
It will then load with a blackscreen and the following error:
Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code.functions::EnemyYellow()[test\code\functions\EnemyYellow.as:15]
at code::Main()[test\code\Main.as:16]
Error opening URL 'file:///test/Main.swf'
However, If I remove _enemyYellow = new EnemyYellow; from the Main file it loads but the second file is not loaded.
If I remove _main = new Main; from the Second file, the game again loads but it does not call the lower HP function, and I get the following error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[test\code\functions\EnemyYellow.as:16]
at code::Main()[test\code\Main.as:16]
If anyone could help me it would be appreciated. Im just trying to get my head around how to call a function from another file..
_playerHPdisplay.text is also a text box on the stage when the game loads.
If you do not assign a value to _main, it is null. That's why you receive the #1009 if you do not assign new Main() to it.
However, you do not want to create a new Main object either.
The main class represents the application and generally speaking you do no explicitly instantiate it in your project.
To make your code work, you have to pass a reference of Main to the enemy class.
A better approach to this is to let the enemy class dispatch events, so that the Main class can be notified "some damage was dealt". This however will not work from within the constructor of enemy.
Think about whether your package names make sense. Pretty much all packages contain code, which makes "code" a not very informative name. The package "functions" contains the class EnemyYellow, which doesn't seem to be a good fit.

5001 and 5008 Errors While Importing a Class

I've a class file named PlayerClass.as which is in the same directory with my .fla file.
PlayerClass.as starts with:package PlayerClass {
.fla file starts with: import PlayerClass;
My function is: PlayerClass.SimplePlayer(Sound1);
But I'm getting errors 5001 and 5008. How can I fix these errors?
ActionScript Error #5001: The name of package does not reflect the location of this file
ActionScript Error #5008: Means you're trying to use a class but the class is in a subdirectory that should be reflected in the package name. An example:-
c:\PackageTest\com\ayumilove\Game.as
package com.ayumilove
{
import flash.display.MovieClip;
public class Game extends MovieClip
{
public function Game()
{
trace("Game Created");
}
}
}
//An example to instantiate the class
import com.ayumilove.Game;
var game:Game = new Game();
Hope this helps. Just check your directories and make sure they are all spelled correctly.
Exactly what Rachel said.
In your case, the PlayerClass should have a empty package
package {
//... your class definition here
}

Target main .as file in ActionScript 3

I am trying to target a variable in the main .as file (The one that acts as the stage) from another .as file.
public var stageRef:MovieClip = root as MovieClip;
or
MovieClip(root)variable = 10;
don't seem to want to work for me. Neither of them produce any compile errors but when I try to use them they give me a 1009 error, cannot access a property or a null object reference. Any ideas of how i would go about doing this? Thanks in advance.
Im your Main.as class make the variable public. Here's an example:
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
public function Main()
{
}
}
}
DEFAULT_VALUE is optional. VARIABLE_TYPE is recommended, if not specified the type will be Object by default.
There are many ways to pass a variable to another class. If the class is created inside the Main class, just pass the variable to that class like this:
var myOtherClass:OtherClass = new OtherClass(YOUR_VAR_HERE);
or
var myOtherClass:OtherClass = new OtherClass();
myOtherClass.varReference = YOUR_VAR_HERE;
In first case make sure the constructor is expecting a variable. In the second, make sure the OtherClass has a public variable varReference that you can access and modify.
Another way loved by newbie programmers are static (singleton) variables: in the Main class specify your variable as such:
public static var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
Then you can access YOUR_VAR_HERE simply by referring to the class Main. Like this:
trace(Main.YOUR_VAR_HERE);
NOTE: it's considered to use all uppercase letters for constants, not variables, in this case I used all caps for readability.

AS3 Error #1065

I must be missing something simple here, but in my main app, I import my Pages class, which in turn imports and dynamically instantiates one of two page types. Unfortunatley it only results in the error: ReferenceError: Error #1065: Variable PageA is not defined. (when I call Pages.load("A");)
Pages
package pages
{
import pages.PageA;
import pages.PageB;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
public class Pages
{
public static function load(pageType:String):void
{
var pageClass:Class = getDefinitionByName("pages.Page"+pageType) as Class;
}
}
}
PageA
package pages
{
import flash.display.Sprite;
public class PageA extends Sprite
{
public function PageA()
{
trace("PageA init");
}
}
}
PageB
package pages
{
import flash.display.Sprite;
public class PageB extends Sprite
{
public function PageB()
{
trace("PageB init");
}
}
}
Exactly, the compiler plainly didn't include those classes in the compiled SWF. I've hit this wall somewhere before, when I've tried instantiating via generated string (in my case 'Gem'+an integer), and received about the same error. I went around it by creating a dummy constant, enumerating all the classes I plan to use, this made the compiler aware of this. So, make the following:
private static const PAGES:Array=[PageA, PageB];
And compile. Should do. Also, you don't need to import parts of "pages" package, they are already visible in your project, since your "Pages" class belongs to the same package.

Using internal in package gives error

I'm trying to place a class into a package where another public class is placed. The documentation says that only one external visible declaration can be put in a package.
So i declare the second class internal. But then it gives the following error:
5006: An ActionScript file can not have more than one externally visible definition: character.AnimatedCharacterClass, character.CharacterPositions
The code I use is:
internal class CharacterPositions
{
public static const BEGIN_WALK:String = 'begin_walk';
public static const END_WALK:String = 'end_walk';
public static const STAND:String = 'stand';
}
Does anyone have a clue what is happening here?
I found that i have to put the second class outside the package. It still confuses me though.