AS3 Unable to access public static constant - actionscript-3

I've got this class where I declare two public static constants:
package com.xxx.videoplayer_v2 {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
public class ControlBar extends MovieClip
{
public static const VOLUME_PRESSED:String = "volumePressed";
public static const PLAY_PRESSED:String = "playPressed";
...
The declaration looks good to me but when I call the constants from any other class in my project (below an example from the stage)
import com.xxx.videoplayer_v2.ControlBar;
trace(ControlBar.PLAY_PRESSED);
I get this error:
1119: Access of possibly undefined property PLAY_PRESSED through a
reference with static type Class.
Why does this happen? I've done this thousands of times with other classes, with the same syntax but I've never had this problem before.

I figured it out!
I have an instance of ControlBar on the stage wich in its properties I exported for ActionScript.
The problem was this: I filled in the Class textfield exactly the same name as the base class (ControlBar) and in the Base Class textfield I inserted "com.weborama.videoplayer_v2.ControlBar" which is right.
I fixed filling in the Class textfield "VPControlBar" instead of "ControlBar".
Now I know that I can't put the same name of the base class in there.
Thanks to everyone who tried to help me!

Related

Why do extended classes have to re import in as3?

Let's say I have a base class ParentClass that includes some import statements.
import flash.events.*;
If I then have a child ChildClass that extends my ParentClass and I want to do something with an event
public class ChildClass extends ParentClass{
public ChildClass():void{
addEventListener(Event.ADDED_TO_STAGE, childFunction);
}
}
it doesn't work unless I add the import statement in the child class:
import flash.events.*;
public class ChildClass extends ParentClass{
public ChildClass():void{
addEventListener(Event.ADDED_TO_STAGE, childFunction);
}
}
Why? If the child class is extending the functionality of the parent, why the need to re-import the classes that the parent required?
One reason that import statements exist is that actionscript 3 classes can have the same name as other classes, provided that they exist in different packages. Import statements tell the compiler which of these classes you are trying to use.
For example, if for whatever reason, you had previously made a class called Event in your this/is/an/example package, the compiler would need to see either an import flash.events.Event statement or an import this.is.an.example.Event statement to know which Event class you were using. The same rule applies to child classes.

how to access public functions within .as file

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.

AS3, Flash - Call a function in a class?

I am trying to call a function which is set inside a class..
How can I call that?
Here is my source, and I would like to call 'processLogin' from outside this class.
Link to source: http://pastebin.com/aFygyXKZ
You can create a new instance of your class main.
Try
var m:main = new main();
m.processLogin();
Also, AS3 best practices state that classes should begin with an Uppercase letter.
You also should extend Sprite instead of MovieClip for DisplayObject classes that do not need timeline functionality.
If you know you are only going to have one instance of the class main in your application, what you can do is:
Implement class main as a singleton class, in which case you
can access the processLogin method using
main.getInstance().processLogin or
Just add a public static
variable to your main class containing the instance of your main
class. In this instance your code would look something like:
package actions {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public static var instance:main;
public function main(){
instance = this;
}
//The rest of your main class code...
}
That way, you can access your processLogin function using main.instance.processLogin().
However, if your application is set to possibly have more than one instance of your main class, then the best approach would be to instantiate main and use that instance, as f-a suggested.

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.

AS3 Base class is final error when I didn't set it that way

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.