3

AS3 - Class root access variable

 2 years ago
source link: https://www.codesd.com/item/as3-class-root-access-variable.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AS3 - Class root access variable

advertisements

How to access a variable in the root timeline from within a class? There is a variable called myblocks in the root timeline I need to read the value from.

This is the related class part:

package myclasses
{

public final class Bcoder extends EventDispatcher
    {

private function getBlocks():void
        {

            for (var i:int = _getNumBlocks; i--; ){
            // how to get the myblocks value from here?

            }}

This is from the root timeline: (ActionScript stands in a keyframe)

    import myclasses.Bcoder;
var myblocks:Number=20


This is complete nonsense and really bad practice. You should avoid this manner of coding!!!

This is really not OOP and make me think about bad AS1 /2 and 3 combined!!!

However this is possible if you have no class defined in Document properties as main Class.

ex : in a foler "com", the class ObjectOnStage.as :

package com {
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.events.Event;
    public class ObjectOnStage extends Sprite{

    public function ObjectOnStage() {
                this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,false,0,false);
                this.addEventListener(Event.ACTIVATED,onActivate,false,0,false);
    }
    public function onAddedToStage(e:Event):void{
                    // will output null for both
            trace("\n       added " + e.target + "\n");
            trace(e.target.parent["nonSense"]);
            trace(e.target.parent["nonsense"]);
        }
    public function onActivate(e:Event):void{
                    // will output the values.
            trace("\n       added " + e.target + "\n");
            trace(e.target.parent["nonSense"]);
            trace(e.target.parent["nonsense"]);
        }
    }

}

On frame 1 of the Timeline :

import com.ObjectOnStage;
var nonSense:int = 1;
var nonsense:String = "This is a nonsense";
var oos:ObjectOnStage = new ObjectOnStage();
this.addChild(oos);

You'd better change the whole script!

Adobe should remove the possibility to write script on the Timeline since the export settings are set to AS3 and the strict mode should be always set to strict mode ON. Also private constructors will be welcome in order to permit an usage of

MyClass.getInstance();

This will pemit something like:

package com {
public class MyMainObject {
    private static var instanceOfMainObject;
    private function MyMainObject(args:Vector.<String>){
                    // or MyMainObject(...args)
        trace("new Instance of MyMainObject created with " + args.toString());
    }
    public static function main(args:Vector.<String>):void{
        instanceOfMainObject = MyMainObject.getInstance(args);
        trace("arguments.length = " + args.length);
        for(var i:int = 0 ; i < args.length ; i++){
            trace( i + " = " + args[i]);
        }
    }
    public static function getInstance(args:Vector.<String>):MyMainObject{
        var instance:MyMainObject = new MyMainObject(args);
        return instance;
        }
    }
}

Now, this code throws an Error:

1153: A constructor can only be declared public.

Perhaps this will be the case in AS4 ???

If I understand it trough your comment you must pass the DisplayObjectContainer where your variables are declared to the class as argument.

Example : in MyClass.as

    package com {
    import flash.display.DisplayObjectContainer;
    import flash.events.EventDispatcher;
    public class MyClass extends EventDispatcher{
        public function MyClass(doc:DisplayObjectContainer) {
            trace(doc["nonSense"]);
            trace(doc["nonsense"]);
            // but this is again not OOP even if you use the "class" KEYWORD.
        }
    }
}

on the timeline : var nonSense:int = 1; var nonsense:String = "This is a nonsense"; var mclss:MyClass = new MyClass(this);

Concerning EventDispatcher you can also read my answer about EventDispatcher here


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK