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.
Related
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.
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
}
I'm trying to use ExternalInterface as I normally would and access public functions of an .as file (connect.as) from main.swf. I can make the swf with zero errors but I'm not seeing logs and functions...
for example there is a function called create
public function create(webSocketId:int,url:String,protocols:Array,proxyHost:String=null,proxyPort:int= 0,headers:String=null):void {
I try to call it even from within connect.as
create(0,'ws://mysie.com:8004',undefined,undefined,undefined);
Ps: i have checked that the above code is normally what is passed in.
main.swf << I've given the document the class of connect.as
connect.as:
package net.gimite.websocket {
import flash.display.Sprite;
import flash.external.ExternalInterface;
import flash.system.Security;
import flash.utils.setTimeout;
import mx.utils.URLUtil;
ExternalInterface.call('consol.log',"flash hello");//<----not working
public class WebSocketMain extends Sprite implements IWebSocketLogger{
//there are lots of public functions I'm trying to access from main.swf in here
main.swf doesn't like line two in this attempt:
import connect;
var connect:Connect = new Connect();
connect.create(10,'ws://mysite.com:8004',undefined,undefined,undefined);
Scene 1, Layer 'Layer 1', Frame 1, Line 2 1026: Constructor functions must be instance methods.
Place ExternalInterface.call('consol.log',"flash hello"); inside constructor of class WebSocketMain or inside any other functions as Placing it outside the class doesn't make sense.
I've written a swc lib using flash pro cs6. Among others the swc contains "LPChat" class:
package {
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
import flash.net.URLRequestHeader;
import flash.utils.setInterval;
public class LPChat extends Sprite {
private var _sessionKey:String;
private var chatEvents:ChatEvents;
private var links:Object;
private var info:Object;
public function LPChat(chatObj:Object) {
.....
}
}
when included in a flash pro projects all works fine, but when included in a flex project I get the following error:
Error #1063: Argument count mismatch on LPChat(). Expected 0, got 1.
which is strange because the constructor does expect 1 and not 0 arguments. I can see the same behavior inside the flash builder IDEA:
any help would be appreciated
It seems that the default package caused the problem. when I moved it to com.lp all issues where solved.
I'm getting the "Base class is final" error on a project that uses the AIR for iOS player. Problem is I didn't set the base class to be final. Also this only happens when I use AIR as the player.
Main - document class
package {
import flash.display.*;
import parentfolder.*;
import parentfolder.childfolder.*;
public class Main extends MovieClip {
public function Main () {
addChild (new SplashScreen ());
}
}
}
Screen - inside parentfolder which is in the same folder as the document class
package parentfolder {
import flash.display.*;
public class Screen extends Sprite {
public function Screen () {
}
}
}
SplashScreen - inside parentfolder
package parentfolder.childfolder {
import flash.display.*;
import parentfolder.*;
public class SplashScreen extends Screen {
}
}
So...there's no "final" anywhere in the code and the problem is isolated in the AIR player.
There is a class in AIR called Screen, which is declared as final. You've used the .*notation on your imports, therefore flash.display.Screen is assumed as the base class for SplashScreen, instead of your custom parentfolder.Screen.
Since this class exists only in AIR, the problem does not occur in other players.
You should always use fully qualified imports (one for each class) to avoid naming conflicts like this.
Your issue is with Screen.
You are trying to extend from it in this line
public class SplashScreen extends Screen
Screen is declared as public final class Screen.
So you cannot inherit from a class that is final.
Quoting the Adobe Docs,
Specifies that a method cannot be overridden or that a class cannot be
extended. An attempt to override a method, or extend a class, marked
as final results in an error.