Access of Undefined property? Actionscript 3 - actionscript-3

I was programming something and when I thought everything was nice and good, Flash throws an error to me!?
At first I was dumbstruck. Then after checking my code, I couldn't see the culprit. So what I did was 'simple it down', and changed it to just a trace statement.
I was still however getting the error. I don't know what is wrong.
package {
import flash.display.MovieClip;
import src.data.DActors;
public class DocumentClass extends MovieClip {
public var dActors:DActors = new DActors;
public function DocumentClass() {
trace (dActors);
trace ("Main");
}
}
}
This is the DActors Class:
package src.data
{
public class DActors
{
public var me:int = 1;
public function DActors();
{
trace(me);
}
}
}
Some scope I'm not aware of or something?
Oh, and by the way, it throws that ''me' is not defined'!?
EDIT: Actually, I failed to realize the real problem, why the hell is my constructor not accepting variables!
package src.data
{
public class DActors
{
public var actors:Array = new Array();
public var dActor:DActor = new DActor();
public function DActors();
{
actors.push(dActor);
}
}
}
outputs:
1120: Access of undefined property actors.
1120: Access of undefined property dActor.
???? This worries me greatly. Either my eyes are fooling me or I'm missing something very basic.

public function DActors();
Constructor function will not end with ;(semicolon).

public var dActors:DActors = new DActors;
Should be:
public var dActors:DActors = new DActors();

Call the constructor properly
public var dActors:DActors = new DActors();

The semicolon after your DActors constructor breaks your code.
public function DActors();
if you change the DActors class to this it will work:
package src.data
{
public class DActors
{
public var me:int = 1;
public function DActors()
{
trace(me);
}
}
}

Related

AS3 - Error #1009: Cannot access a property or method of a null object reference

I have no idea what is causing the error. I'm sure its very obvious.
(See comments below).
package src.main{
import src.tilespack.Tile;
public class Main{
public var tile:Tile;
public function Main{
var t:Tile = Tile.tiles[0]; //Error here
trace(Tile); //This will also cause an error
}
Tile:
package src.tilespack{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
FloorTile:
package src.tilespack{
import src.gfx.Assets;
import src.tilespack.Tile;
public class FloorTile extends Tile{ //Error here
public function FloorTile(ID:int){
super(Assets.floorTileData, ID);
}
}
}
Error #1009: Cannot access a property or method of a null object
reference.
I've noticed few problems in your code:
1) The file Tile doesn't have a definition of class. Probably, this is just a typo, but anyway, this file should look something like:
package src.tilespack
{
public class Tile
{
public static var tiles:Array = [];
public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}
2) Your code has something like "recursive linkage" (sorry, I don't know the official term). Your class Tile has the static variable floorTile, and it tries to create an instance of the FloorTile class, which itself extends the class Tile.
So we have a situation, when the class Tile tries to use the class FloorTile (because static variables should be instantiated during the first class use), and the class FloorTile tries to use the class Tile. And it's a problem.
You may either remove static variable of the type FloorTile or change code of the class Tile to prevent usage of the class FloorTile before the class Tile is prepared to work. Here an example of the second approach:
package src.tilespack
{
import flash.display.BitmapData;
public class Tile
{
public static var tiles:Array = [];
private static var _floorTile:Tile;
public static function get floorTile():Tile
{
if (!Tile._floorTile)
{
Tile._floorTile = new FloorTile(0);
}
return Tile._floorTile;
}
public var ID:int;
public var bitmapData:BitmapData;
public function Tile(bitmapData:BitmapData, ID:int)
{
this.ID = ID;
tiles[ID] = this;
this.bitmapData = bitmapData;
}
}
}

Passing variables between classes in Actionscript 3

I'm not terribly good at this modern object coding lark and think I may have misunderstood how this is meant to to work:
This is test.as in my Flash Builder 4.7 project called test
package
{
import flash.display.Sprite;
public class test extends Sprite
{
public var myVar:int = 10;
public function test() {
var myTestsub:testsub = new testsub;
}
}
}
And this is my testsub.as file:
package
{
public class testsub
{
public function testsub()
{
trace(myVar);
}
}
}
Flash Builder tells me that in testsub.as "Possible access of undefined variable myVar" and will not compile.
From my limited, and probably incorrect, understanding, making myVar public in test.as should allow testsub.as to use it. Can anyone explain where I am going wrong? Thank you in advance.
you need to wirte your var like this:
public class the_var
{
public function the_var
{
public static var my_var:number = 10;
}
}
and call your var in other classes like this:
public class the_test
{
public function the_test
{
trace(the_var.my_var);
}
}
There are no connections between the classes. They need a link, similar to this solution:
public class test extends Sprite
{
public var myVar:int = 10;
public function test() {
var myTestsub:testsub = new testsub(this);
}
}
public class testsub
{
public function testsub(otherClass:test)
{
trace(otherClass.myVar);
}
}
In test when creating the testsub-class we pass a long a referende to the instance of test. That makes it possible to access public functions & variables on that instance from the other class. (Observer that you need to add test-class as an import inside testsub in order to compile code.

AS3 undefined function #1006

I have a parent class called 'main.as'. I am trying to get the child class to call main's function. They both reside in the same folder.
// main.as //
package {
public class main extends MovieClip {
public function main() {
var child:child_mc = new child_mc(this);
}
public function callFunction():void {
trace("Done it");
}
}
}
.
// child.as //
package {
import main;
public class child extends MovieClip {
private var main:MovieClip = new MovieClip();
public function child(main:MovieClip):void {
this.main = main;
main.callFunction();
}
}
}
This is the error I've been getting:
TypeError: Error #1006: callFunction is not a function.
so I tried doing a trace like this
trace(main.callFunction);
and it says undefined. Can someone tell me what I am missing. I get this feeling its a very basic thing that I have overlooked!
Your "child" package is defined as "main". I'm not even sure how it complied, let alone run to the point of showing the error message you got.
I believe the code below should do what you expected.
(I also took the liberty to rename the classes to use CamelCase (with initial caps) to adhere to best practices and to be easier to distinguish from variable names.)
Main.as
package {
public class Main extends MovieClip {
public function Main() {
var child:ChildMC = new ChildMC();
child.main = this;
}
public function callFunction():void {
trace("Done it");
}
}
}
EDIT: I just saw your comment that points out that child_mc is a MovieClip in the Library. I guess then that the child class is set as the Base Class of the child_mc?
If so, you cannot pass properties through the instantiator, you need to find another way to pass along the instance of the Main class to the Child class.
One way would be to add a setter, like the following:
Child.as (Base Class for ChildMC)
package {
public class Child extends MovieClip {
private var _main:Main;
public function Child() {
}
public function set main(main:Main):void {
this._main = main;
this._main.callFunction();
}
}
}

How do I tween a variable with TweenLite?

package
{
import com.greensock.TweenLite;
import flash.display.Sprite;
public class TweenTest extends Sprite
{
private var _test:Number = 10;
public function TweenTest()
{
TweenLite.to(this,1,{_test:200});
}
}
}
I get the error #1069: Property _test not found for TweenTest…
I also tried this example which does not work for me:
http://www.snorkl.tv/2010/09/how-to-tween-a-variable-with-flash-and-tweenlite/
TweenLite can only affect public properties of a class. Making _text public or creating an public getter should sort it out.
This is definitely possible by simply making your variable public.
You can also do something like:
var arr:Array = [0];
TweenLite.to(arr, 1, {endArray: [10], onUpdate: output});
function output():void
{
trace (arr[0]);
}

AS3 override public function both are called?

I am a little confused by this concept.
If I override a public function in a base class, I would have thought that this override function is called and the original is ignored? though this doesn't seem to be the case...
public class AbstractScreen extends Sprite
{
public function AbstractScreen()
{
}
public function updateLanguage():void
{
trace("WARNING: need to override public function updateLanguage()");
}
}
public class Start extends AbstractScreen
{
override public function updateLanguage():void
{
title.text = _model.language.start
title.setTextFormat(titleFormat);
}
}
public class ViewManager extends Sprite
{
private var _model:Model;
private var _screens:Array;
public function ViewManager(model:Model)
{
_model = model;
_model.addEventListener(AppEvent.INIT_VIEW, initViews);
_model.addEventListener(AppEvent.UPDATE_VIEW, updateView);
_model.addEventListener(AppEvent.CHANGED_LANGUAGE, changeLanguage);
}
private function initViews(e:AppEvent):void
{
trace("View Manager: initViews");
_screens = new Array(new Start(_model), new TakePhoto(_model));
dispatchEvent(new ViewEvent(ViewEvent.VIEW_READY));
}
private function changeLanguage(e:AppEvent):void
{
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
}
}
If my model dispatches a CHANGED_LANGUAGE event, the text in the views gets updated, But I also get a trace of "WARNING: need to override public function updateLanguage()" What could I be doing wrong?
You are right, this should not call the base class function. Are you sure there is no call to
super.initLanguage()
within your override?
Most IDE's add this call in the function body automatically, if they create an override for you.
EDIT
From your edit I see your are iterating over two objects of the types Start and TakePhoto. I assume TakePhoto is also derived from AbstractScreen and the trace may be comming from this one.
Also I suggest to use the abstract base class in your iteration.
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}