Why do extended classes have to re import in as3? - actionscript-3

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.

Related

Call custom method as Display Object AS3

I have a class which extends MovieClip. This class has an update() function which needs to be called every new frame with the deltaTime in the arguments. This works if the class has been declared but not if it has just been added to the display list.
Code in the main class:
package packageFoo{
import flash.display.MovieClip;
import packageFoo.customMovieclip;
public class Main extends MovieClip{
public function Main():void{
var testMc:customMovieClip = new customMovieClip();
addChild(testMc);
testMc.update(dt);
}
}
}
This outputs the correct values where as if I just added it without referencing it:
package packageFoo{
import flash.display.MovieClip;
import packageFoo.customMovieclip;
public class Main extends MovieClip{
public function Main():void{
addChild(new customMovieclip());
this.getChildAt(0).update(dt);
}
}
}
This makes the compile time error: 1061: Call to a possibly undefined method update through a reference with static type flash.display:DisplayObject.
I can't really reference the 'customMovieclip's because I am wanting multiple ones.
It looks like this.getChildAt(0) is not customMovieClip. This can arise if your Main has pre-places components at design time. To check, do trace(this.numChildren) as the first line of Main() constructor. And also, to address any subclass methods properly, you need to typecast your DisplayObject returned by getChildAt() to a proper type.
(this.getChildAt(0) as customMovieClip).update(dt);
Still, using a class-wide variable is better if you want to address that custom MC in more than one function of main class.
If you're trying to avoid a reference to the custom class in the document class, you can call it like this:
this.getChildAt(0)["update"](dt);

AS3 Unable to access public static constant

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!

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: Compiler bug with inner classes and interfaces?

For some irrelevant reasons I need a class:
that inherits (directly or not) from MovieClip.
that implements a particular interface (let's assume here that this interface is empty since it does not change anything to the issue).
and whose .as file declares internal classes.
The following code sums this up:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
class MyInnerClass { }
The problem with that code above is that it will not always compile. As soon as I use MyClass as Linkage for one of my library's item the compiler complains about MyClass not being a subclass of MovieClip. On the other hand, everything works great if I instantiate it manually and add it to the stage.
It looks like the interface and the inner class are somehow mutually exclusive in that very particular case. Indeed, if I remove the inner class I do not have that error anymore:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
Same thing when I remove the implemented interface but keep the inner class:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip { }
}
class MyInnerClass { }
Note that I've only tested this in Flash CS5.
I say it is a bug of compiler.
I have tested and found that private class must extend any class that is not Object. Instead of extending class it can also implement any interface.
This works same even if I put classes into deeper package.
I have tested this with Flash CS6.
If I'm reading you right, you want a public class to extend an internal class? - There is nothing that prevents you from doing this, so long as you declare your internal class as it's own packaged file.
According to the documentation:
[dynamic] [public | internal] [final] class className [ extends superClass ] [ implements interfaceName[, interfaceName... ] ] {
// class definition here
}
If it's the interface that is giving you grief, have you declared it in a separate file as well - that you would import? As eluded to in the comments, the namespace scoping is important so that the compiler understands what the escalating priority is.
Eg:
package my.example {
public interface EmptyInterface
{
}
}
So that:
package {
import flash.display.MovieClip;
import my.example.EmptyInterface;
public class MyClass extends MovieClip implements EmptyInterface { }
}
If this doesn't fix it I have another idea but try this first.
Click file
Click publish setting
Click on settings button
Uncheck Automatically declare stage instances
Click OK

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.