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
Related
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.
I found a quite strange problem while making two classes in AS3. Let's call them ParentClass and ChildClass. In order to make both of them you need a Sprite object, then the ParentClass makes it visible in the stage. ChildClass inherits the ParentClass, too.
ParentClass.as:
package myStudio.basic {
import flash.display.MovieClip;
import flash.display.Sprite;
public dynamic class ParentClass extends MovieClip {
public function ParentClass(mc:Sprite=null) {
addChild(mc);
}
}
}
ChildClass.as:
package myStudio.containers {
import myStudio.basic.ParentClass;
import flash.display.MovieClip;
import flash.display.Sprite;
public class ChildClass extends ParentClass {
public function ChildClass(mc:Sprite=null) {
addChild(mc);
}
}
}
Then, I write this code on Frame 1, Layer Actions of the FLA file:
var mc:MovieClip = new childMC;
var vig:ChildClass = new ChildClass(mc);
addChild(vig);
However, I got run-time error #2007:
TypeError: Error #2007: The value of the parameter child must not be null.
at flash.display::DisplayObjectContainer/addChild()
at myStudio.basic::ParentClass()
at myStudio.containers::ChildClass()
at myStudioComicAnimator_fla::MainTimeline/frame1()
I tried overriding the ChildClass constructor function, but it still doesn't work.
So here's my question: Is there another workaround to solve this problem?
The reason for that is that you are not calling super. You can check what's happening in the error stack (down to top):
you instantiate ChildClass, and you pass the previously created childMC to the constructor
ChildClass extends ParentClass, so when instantiated it always calls the constructor
the constructor of ParentClass tries to add something as a child
The problem is that you cannot add null as a child. But because the constructor is called internally, there is no param that is being passed to it. so mc variable is always null. But as we said - null cannot be added.
Use the super by yourself:
public function ChildClass(mc:Sprite=null) {
super(mc);
}
This way the ParentClass will get reference to the mc object and will be able to add it.
Another option is not to use addChild in the ParentClass, but only in ChildClass. Then it doesn't matter if you pass anything to super, or even if you are calling super at all.
Edit: I forgot to say that this is not a bug, but a standard behavior and works exactly like it should work. The reason for this is that each class can have a whole different override of the constructor. It can take more or less parameters, so the chain for calling parent's constructor is your job to handle.
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);
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.
What I would like to do is to call a method from a superclass in a subclass. Specifically I want to be able to add the subclass as a child of the superclass but without physically having to type addChild in the superclass (but I will have to type it in the subclass). For now I'm just trying to call a method in the superclass that draws some text from a subclass.
Here is the MAIN class (the superclass)
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class MAIN extends Sprite
{
public var SOMETEXT:TextField = new TextField();
public function MAIN()
{
new OBJECT_square().CREATE();
}
public function DRAWTEXT():void
{
SOMETEXT.text = "sometext";
addChild(SOMETEXT);
}
}
}
Here is the OBJECT_square class (the subclass)
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends MAIN
{
public function CREATE():void
{
MAIN.DRAWTEXT();
}
}
}
The code doesn't compile, I get "Call to a possibly undefined method DRAWTEXT through a reference with a static type class".
I realize there are otherways to display text on the screen. I just need to learn how to call superclass methods.
Just call it regularly. When you have a class extend a base class, the class inherits all the base classes methods.
package
{
import flash.display.*;
import flash.events.*;
public class OBJECT_square extends MAIN
{
public function CREATE():void
{
DRAWTEXT();
}
}
}
Edited
My bad about suggesting static, I clearly didn't pay much attention to your code before answering. DRAWTEXT doesn't show up anything because your object has not beed added to the stage. You have to call addChild() to see your diplsay object.
As a side note, don't take this bad, buy you have been asking a lot of questions that show that you lack a basic understanding of how Flash and Actionscript works (and also you seem to be a bit too insistent on having it work the way you want).
It's always ok to ask questions here (as long as you keep them on-topic, which seems to be the case with your questions), but I think you would be better off first learning the basics from some good books, tutorials, etc.
These two posts contain links to good resources, I think you should check them out.
Resources for Learning ActionScript 3.0 as a Professional Programmer
https://stackoverflow.com/questions/168586/where-to-learn-actionscript-3-0
Also, what's with all these users with the same name? Right now I can see 9 users with the name 1101 here: https://stackoverflow.com/users/, and I think all of them are yours. One is enough. Really. You don't need to create a new user everytime you want to ask a question. Also, try to follow up the questions you've asked to give some feedback to the people that bothered answering (this will be much easier if you have just one user). And when you want to comment on some response, leave a comment instead of adding an answer.