Call custom method as Display Object AS3 - actionscript-3

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);

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.

Error when overriding constructor of extended class

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.

Select a extern class to start with

my problem is pretty hard to describe and to google for <_< so ill give it a try here.
ihave my main.as, char.as, enemy.as, classes
my main was the stage of course.... it worked pretty well, but now that my main is extern too, it wont be called anymore...
it calls: char, enemy(which is a sub class of char) and then the empty stage but it never calls my main.as which should be called first...
also it never calls any constructors...how can i tell flash to start with my main.as?
i hope you got it, the code is probably to much to post here :P
If get you right.. you have to put super() calls to extended classes.
So for example in enemy class constructor:
public class enemy extends char {
function enemy() {
super(); // calls "char" class constructor
}
}
and if you have params in your constructors:
public class enemy extends char {
function enemy(param1: String, param2:String) {
super(param1, param2); // calls "char" class constructor
}
}
Overriding methods:
override public function doSome():void {
super.doSome(); // call parent class
}
and to make flash start with your main class - select your FLA file stage in flash (professional) and from the right side panel (properties) set Class to your main class (for example: com.myApp.Main).

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.