1120: Access of undefined property flash - actionscript-3

In my class, I tried using this line:
trace(flash.utils.getQualifiedClassName(getQualifiedClassName(TempButton)))
However, I get this error:
"1120: Access of undefined property flash."
Can someone help me solve this issue? The code is in an AS file under the class Main that extends as a MovieClip.

You probably have forgotten to import "getQualifiedClassName" class. This code should work:
// remember to import class(es)
import flash.utils.getQualifiedClassName;
public class ... {
// constructor
public function ... () {
// you don't have to use that "flash.utils." before you call the function,
// but you can do that too, if you want
trace(flash.utils.getQualifiedClassName(getQualifiedClassName(TempButton)));
}
}

Related

Call to possibly undefined method dispatchEvent through a reference with static type GetColor

I'm writing a program using Main.as, that needs to listen to a function (getColor) in another class file (GetColor.as). I have the following in GetColor.as:
public class GetColor
{
public function getColor(event:MouseEvent):void
{
//doing stuff here
this.dispatchEvent(new Event("changeColor") );
}
}
and then in Main.as I have:
var getPicColor:GetColor = new GetColor();
getPicColor.addEventListener("changeColor",changeColorNow);
function changeColorNow(e:Event):void
{
//do stuff here
}
However, I am getting an error:
1061: Call to a possibly undefined method dispatchEvent through a reference
with static type GetColor.
What does this mean? I have nothing declared as static. Am I supposed to create an instance of dispatchEvent(), as opposed to using "this"?
You cannot dispatch events with a class that (implicitly) extends Object -> that's why you are getting there error -> where is "dispatchEvent()" method coming from? Where is it inherited from? (answer: it is not!)
Your GetColor class (horrible name there! :) ) must either extend a display object - which in your case it not really the correct solution, extend EventDispatcher or implement IEventDispatcher.
Then you can use the method dispatchEvent.

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.

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

Actionscript 3 instance name in class is not working

I have set the instance name of MovieClip to char and when i try to compile this code i get 2 Errors:
package com.game
{
import flash.display.MovieClip;
import flash.events.*;
public class game extends MovieClip
{
var gravity = 0.8;
var velocity = 0;
char.addEventListener(Event.ENTER_FRAME,isHitted);
function isHitted(event:Event):void
{
if (char.hitTestObject(level1))
{
velocity++;
char.y -= gravity+velocity;
}
else
{
}
}
Errors:
.../game.as, Line 13 1120: Access of undefined property char.
../game.as, Line 13 1120: Access of undefined property isHitted.
First off, it sounds like this class definition is the definition for the instance you are referring to, if it is, you should use 'this' instead of 'char'
Also, you typically do not specify method calls like:
char.addEventListener(Event.ENTER_FRAME,isHitted);
outside of methods when declaring a class. Instead, that line of code should exist inside of a constructor or a method that is called during the instantiation of the MovieClip.
You need to make 'char' accessible to 'game' before being able to use it.
One way is to pass 'char' as parameter when you instantiate 'game'.
Two ways of doing this are described in the answer of #lee-burrows in Access caller object when using composition in AS3