How AS3 class files.as work together with FLA file? - actionscript-3

I have two questions: Can i write app using just *.as files, and then compile them somehow in SWF? (i am making myself a webpage now)
Secondly, please look at the code, my problem is the same - I can't render text field onto stage, to be visible.
Flash say's 'undefined method addChild.' and 'Access of undefined property tekstuKaste through a reference with static type Class.'
This is a constructor type class inic which kinda serves for initialization, cos all i do is, I make an instance OF THIS class in FLA file by ActionScript, and expect, all application to work.
package {
import pacinas.visuals.*;
import pacinas.visuals.AE_kanva;
public class inic {
public function inic() {
trace("===========");
trace("inicializēt un izsaukt visu no Kanvas klases");
trace("===========");
trace(" ");
var kanvas:AE_kanva = new AE_kanva();
trace(" ");
kanvas.varis();
trace(" ");
trace("===========");
trace("inicializēt un izsaukt visu no Lauki klases");
trace("===========");
trace(" ");
var laukiTxt:BE_tekstaLaukiPrimitive = new BE_tekstaLaukiPrimitive();
trace("");
laukiTxt.simpleText();
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
}
}
}
There is another EXTERNAL CLASS By whom i hoped to place a rectangles - that does not work too. Example:
package pacinas.visuals
{
import flash.display.Sprite;
public class AE_kanva extends Sprite
{
public function AE_kanva()
{
var kvad:Shape = new Shape();
kvad.graphics.beginFill(0xFF0000);
kvad.graphics.drawRect(0, 0, 100,100);
kvad.graphics.endFill();
addChild(kvad);
trace("konstruktors - zīmē kanvu");
}
public function varis()
{
trace("te glabaas variaabljus");
var ff:int = 4;
var dd:int = 8;
}
}
}
And here is class i hoped will make text box for me (to fill it with XML later)
package pacinas.visuals
{
import flash.text.*;
import flash.display.Sprite;
public class BE_tekstaLaukiPrimitive extends Sprite
{
public var tekstuKaste:TextField = new TextField();
private var kontinents:String = new String ("XML SATURU CMON! a123");
public function BE_tekstaLaukiPrimitive():void
{
trace("teksta rāmis = konstruktora klase");
addChild(tekstuKaste); <--CAN'T GET THIS TO WORK!!!
tekstuKaste.text = kontinents;
}
public function simpleText()
{
trace("nekonstruktora f-cija no Teksta lauki");
}
}}
p.s. I do not use document Class. Ok I will if it's needed. But how?

Can I write app using just *.as files, and then compile them somehow into a SWF?
Yes - using the Flex SDK you can write pure ActionScript and compile it down into a working SWF. FlashDevelop is a good IDE that takes advantage of this.
You will however need to understand how the document class works.
Flash says undefined method addChild. and Access of undefined property tekstuKaste through a reference with static type Class.
In your code, this line is causing your issue:
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
The first error undefined method addChild is because your class inic does not extend a class that implements the method addChild(). DisplayObjectContainer defines this method, so you'll want to extend that as a minimum, like this:
public class inic extends DisplayObjectContainer
The second error is because you're attempting to access a property of the class BE_tekstaLaukiPrimitive as if it were static. I suspect what you actually wanted to do was this:
addChild(laukiTxt); // laukiTxt is the instance you created.

Related

actionscript 3 - error #1009 issues calling function from another class

Im having trouble calling functions from other classes. I want to call a function in one class which updates a score display in another class. The error code for this is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code.functions::EnemyYellow()[code\functions\EnemyYellow.as:18]
at code::Main()[\code\Main.as:27]
Would appreciate if someone could help me out, I set up 2 basic files with the code which is causing an issue. Its normally not set up like this, I just made this for testing and so I can clearly explain the problem.
Main file:
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.*;
public class Main extends MovieClip {
public var _enemy:EnemyYellow;
public var playerHP:Number;
public function Main() {
playerHP = 10;
_playerHPdisplay.text = playerHP.toString();
trace(playerHP)
_enemy = new EnemyYellow;
}
public function lowerHP ():void
{
playerHP = 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.lowerHP();
trace ("done")
}
}
}
I also tried adding _main = new Main; in the second file but the game just loads with a blackscreen and an error about invalid data.
First of all, you surely need to "instantiate" the Main class, which means basically to create it.
public var _main:Main;
This line just declares that there will be a variable of type Main. But for now, the value of _main is null. So you are right, that you need to call:
_main = new Main();
After you've done this, the first error will disappear. But then you have things in that MovieClip that are still vague. Like _playerHPdisplay. Where's that from? Is it an instance from stage or what? You just created a brand new object, and it does not have any reference to other objects, TextFields or whatever.
So this basically answers your current question and problem, but for sure you will have more :)

Object On Stage Cannot Be Accessed From External Class

I have an external ActionScript class, which is called Menu.as, trying to access objects on the stage using the following code:
MovieClip(parent).fullmenu_mc.x = 80;
Although the program compiles, I get Error #1009: Cannot access a property or method of a null object reference
I don't see how this is possible since the object is already on the stage and just needs to be accessed. Am I somehow accessing the object incorrectly?
I also tried the following code inside Menu.as:
import EngineClass;
var engine : EngineClass = new EngineClass();
engine.fullmenu_mc.x = 80;
which gives the same runtime error. Any thoughts are welcome!
Short Version:
Basically you first need to setup a variable that acts a reference name to your on-stage movieclip. You can instead make it a public static variable if you want other Class files imported to also control this variable (and therefore also the movieclip) from their own Class code.
So define a public static var menuMC : MovieClip = new MovieClip(); then when Stage + stage items is available (since Flash processes code first then makes a Stage + items after), we update definition to know the instance name of on-stage object it represents by saying: menuMC = DisplayObjectContainer(getChildByName("fullmenu_mc")) as MovieClip;
Now you can update via any imported external class using a line like: Main.menuMC.x = 200; (where Main is the name of Class that holds the definition of what menuMC means).
Long Version:
Assuming you have some setup like this.
fullmenu_mc = instance name of Object on stage
Main.as = your FLA's document Class file attached to hold the main running code
External.as = some other external Class file that will also control the
on-stage MC
Note: If your code is on the timeline instead of a (Main.as) document class let me know to update (if possible), but the above Classes setup would be easier...
In Main.as.. (define variable and also run a function that is inside external Class)
package
{
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import External;
public class Main extends MovieClip
{
public var ExtClass : External = new External; //init the external class
public static var menuMC : MovieClip = new MovieClip(); //reference to on-stage MC
public function Main ()
{
if (stage != null) { Init(); } //do Init function if SWF loaded ok...
else { addEventListener (Event.ADDED_TO_STAGE, Init); } //or do the function when ready..
}
//If stage is available then we can now do this
public function Init (event:Event = null) :void
{
//update menuMC to know it means some item "X" on stage. "X" is instance name
menuMC = DisplayObjectContainer(getChildByName("fullmenu_mc")) as MovieClip;
trace("menuMC is type : " + menuMC); //if ok then traces [object MovieClip]
menuMC.x = 10; //just for testing
ExtClass.accessTest(); //run some function in External class
}
}
}
Now in External.as we can control the fullmenu_mc (now known as menuMC) like so..
public function accessTest () :void
{
//access stage item using Static Variable from Main Class
Main.menuMC.x += 500;
trace("Full_menu... new x-pos is : " + Main.menuMC.x);
}

AS3 Accessing a variable from an external include ActionScript File .AS not Class

i am loading in a external AS file directly into the main timeline IDE using the "include" method with some variables and calling another Class within the AS file for example;
include "vars.as"
contents of "vars.as":
var test:* = "test?!";
var foo:FOO = new FOO();
addChild(foo);
contents of "FOO.as":
package {
import flash.display.*;
public class FOO extends MovieClip {
public function FOO() {
trace("test= "+test);
}
}
}
error;
1120: Access of undefined property test.
how can i access the "test" variable inside "vars.as" from the "foo" class, is that possible?
Included actionscript files, during compilation, are treated as though they were right in the body of the actionscript you included them in. You can just access them normally as though you defined them in the same place.
trace(test); //test
ok so i found a solution, hope this helps someone else out there.
this is how you should call it from the main timeline;
var foo:FOO = new FOO(this, stage);
addChild(foo);
and here is the updated FOO class:
package {
import flash.display.*;
public class FOO extends MovieClip {
var _stage:Stage;
var _root:*;
public function FOO(root:*, stage:Stage){
this._stage = stage;
this._root = root;
trace(this._root.test);
}
}
}

GetDefinition in as3 using sprite class

i have a problem. I have been able to run my game successfully using as3isolib library (IsoSprites), but when i search on the internet, the IsoSprites don't have the hitTestObject or hitTextPoint features. So, i changed the IsoSprites to the Sprite, but when i already changed the IsoSprites to the Sprite, i am getting this error:
1119: Access of possibly undefined property sprites through a
reference with static type flash.display:Sprite.
and it is pointed in:
Constant.dudeEfis.sprites = [efisFrontClass];
dudeEfis is Sprite
i am aware that the Sprite don't have the feature of:
dudeEfis.sprites
but the problem is, if i change the "dudeEfis" to the IsoSprite, i am not be able to get the hitTestObject or hitTestPoint. And if i change to the Sprite, i am getting that error. How do i solve it? The code that i am giving to you is for movement faces direction (when the player is facing south, so do the character)
Here is the code:
public static var loaderEfis:Loader;
public static var dudeEfis:Sprite; //public static var dudeEfis:IsoSprite;
public static var _numXEfis:Number = 0;
public static var _numYEfis:Number = 0;
Constant.loaderEfis = new Loader();
Constant.loaderEfis.load(new URLRequest("efis.swf"));
var efisFrontClass:Class = Constant.loaderEfis.contentLoaderInfo.applicationDomain.getDefinition("EfisFront") as Class;
if (Constant._numXEfis == 0 && Constant._numYEfis == 0)
{
Constant._numXEfis = Constant.dudeEfis.x;
Constant._numYEfis = Constant.dudeEfis.y;
Constant.dudeEfis.sprites = [efisFrontClass];
}
Any help will be appreciated! Thank you in advance!
Sounds like you already know what the problem is. One way you can solve this is to create your own class that extends the IsoSprite class then add the hitTestObject and hitTestPoint methods yourself, they're pretty easy to implement.
package {
import flash.display.DisplayObject;
import flash.geom.Point;
public class MyOwnSprite extends IsoSprite {
public function hitTestObject (object:DisplayObject):Boolean {
//some logic here
}
public function hitTestPoint (point:Point):Boolean {
//some logic here
}
}
}
This way you get to keep all the functionality of IsoSprite while having those two methods that you need.

AS3 - passing the main timeline/stage into an external class

Suppose I have the following class
package {
import flash.display.Stage;
public class CustomObject {
private var stage:Stage;
public function CustomObject(stageRef:Stage) {
// stage access through
// constructor argument
stage = stageRef;
}
}
}
Which is not a document class. I wish to pass the stage of the main timeline into the class, say on frame 1
stop();
var c:CustomObject = new CustomObject(this.stage);
Is this the right way of passing the stage of the main timeline into another class?
That will work perfectly well - but if your custom class is extending a display object of any kind (Sprite, MovieClip, etc) it will have it's own stage property which is populated automatically if your object is in the display tree. I believe this would also mean your private variable would result in a compiler error.