Enum Types in As3

From what I can gather looking online and trying different things, there seems to be a couple of ways to solve the problem of using enum statements in AS3.

First, simply define a whole list of public static const ints at the top of an actionscript file, preferably the one using the enum constants most often. For example:

public class Thingy 

public static const ENUM_1:int = 1;
public static const ENUM_2:int = 2;
public static const ENUM_3:int = 4;

private var mType:int;

public function Thingy(type:int = 0) {

mType = type;

}

Then, when you make a new Thingy that needs to have reference an enumerated constant, you can just do:

var thingy:Thingy = new Thingy(Thingy.ENUM_1);

Alternatively, you can make an “empty” class that just holds constant ints and import it to use in your other classes. This might be better for long lists of constants, and especially if you plan to do bitfield comparisons.

public class Enums {

public static const ENUM_1:int = 1;
public static const ENUM_2:int = 2;
public static const ENUM_3:int = 4;
/* etc etc */
}

And then just use it where you need it, setting up your classes to receive int data as types or whatnot:

var thingy:Thingy = new Thingy(Enums.ENUM_1);
if (thingy.type == Enums.ENUM_1) {
trace("good to go");
}
Advertisements

search and return file format correct & incorrect

You can use this code with any other programming language. I will explain later how to use this code.


package
{
	public class FormatController
	{
		private var str    : String = "";
		private var strLen : int;
		public static const FORMAT : String = ".obj";

		public function FormatController()
		{
			/*	this function only returns true & false	*/
			trace(".obj format is correct ? -> " + controlSupportFormat("model.obj"));
		}

		private static function controlCorrectFormat(_fileFormatName:String):Boolean
		{
			if(_fileFormatName.length < 0 || _fileFormatName == "") return false;
			
			strLen = _fileFormatName.length;
			
			if(_fileFormatName.indexOf(FORMAT) > 0)
			{
				str = _fileFormatName.substring(int(_fileFormatName.indexOf(FORMAT)), strLen);
				
				return str == FORMAT.toString();
			}
			
			return false;
		}
	}
}
	

away3d simple movement

package
{
import away3d.containers.ObjectContainer3D;
import away3d.containers.ObjectContainer3D;
    import away3d.containers.View3D;
    import away3d.controllers.HoverController;
import away3d.core.base.Object3D;
import away3d.entities.Mesh;
    import away3d.lights.DirectionalLight;
    import away3d.materials.ColorMaterial;
    import away3d.materials.lightpickers.StaticLightPicker;
    import away3d.primitives.CubeGeometry;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.geom.Vector3D;
    import flash.ui.Keyboard;

    public class Sample1_LookAt extends Sprite
    {
        private var lookAtPosition:Vector3D = new Vector3D();
        private var view:View3D;
        private var cameraController:HoverController;

        private var cloneModel:ObjectContainer3D;

        private var light:DirectionalLight;
        private var lightPicker:StaticLightPicker;

        private var cubeMaterial:ColorMaterial;
        private var cubeMesh:Mesh;
        private var cubeGeo:CubeGeometry;
        private var cubeHolder:ObjectContainer3D;

        private var lastPanAngle:Number;
        private var lastTiltAngle:Number;
        private var lastMouseX:Number;
        private var lastMouseY:Number;
        private var isMove:Boolean = false;


        // movement controller
        private var isRightMove:Boolean;
        private var isLeftMove:Boolean;
        private var isDownMove:Boolean;
        private var isUpMove:Boolean;


        public function Sample1_LookAt()
        {
            view = new View3D();
            view.antiAlias =4;
            addChild(view);

            cubeHolder = new ObjectContainer3D();
            cubeHolder.name = "cube_holder";
            view.scene.addChild(cubeHolder);

            cameraController = new HoverController(view.camera, null);
            cameraController.panAngle = 180;

            initLight();
            prepareCube();
            addListeners();
        }

        private function initLight():void
        {
            light = new DirectionalLight();
            light = new DirectionalLight(-0.5, -1, -1);
            light.ambient = 0.4;
            lightPicker = new StaticLightPicker([light]);
            view.scene.addChild(light);
        }

        private function prepareCube():void
        {
            cubeMaterial = new ColorMaterial(0xFFCC00);
            cubeMaterial.lightPicker = lightPicker;

            cubeGeo = new CubeGeometry(150,150);
            cubeMesh = new Mesh(cubeGeo, cubeMaterial);
            cubeHolder.addChild(cubeMesh);

            // sonradan tekrarlaması için clone ile kopyasını alıyoruz.
            cloneModel = ObjectContainer3D(cubeHolder.clone());
            view.scene.addChild(cloneModel);
            cloneModel.x = 300;
        }

        private function addListeners():void
        {
            addEventListener(Event.ENTER_FRAME, onRender);
            stage.addEventListener(Event.RESIZE, onResize);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onStageUp);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            onResize();
        }

        private function onStageDown(event:MouseEvent):void
        {
            lastMouseX = stage.mouseX;
            lastMouseY = stage.mouseY;
            lastPanAngle = cameraController.panAngle;
            lastTiltAngle = cameraController.tiltAngle;
            isMove = true;
        }

        private function onStageUp(event:MouseEvent):void
        {
            isMove = false;
        }

        private function onKeyDown(event:KeyboardEvent):void
        {
            switch (event.keyCode) {
                case Keyboard.UP:
                case Keyboard.W:
                case Keyboard.Z: //fr
                    isUpMove = true;
                    break;
                case Keyboard.DOWN:
                case Keyboard.S:
                    isDownMove = true;
                    break;
                case Keyboard.LEFT:
                case Keyboard.A:
                case Keyboard.Q: //fr
                    isLeftMove = true;
                    break;
                case Keyboard.RIGHT:
                case Keyboard.D:
                    isRightMove = true;
                    break;
            }
        }

        private function onKeyUp(event:KeyboardEvent):void
        {
            switch (event.keyCode) {
                case Keyboard.UP:
                case Keyboard.W:
                case Keyboard.Z: //fr
                    isUpMove = false;
                    break;
                case Keyboard.DOWN:
                case Keyboard.S:
                    isDownMove = false;
                    break;
                case Keyboard.LEFT:
                case Keyboard.A:
                case Keyboard.Q: //fr
                    isLeftMove = false;
                    break;
                case Keyboard.RIGHT:
                case Keyboard.D:
                    isRightMove = false;
                    break;
            }
        }

        private function onResize(event:Event = null):void
        {
            view.width = stage.stageWidth;
            view.height = stage.stageHeight;

        }

        private function onRender(event:Event):void
        {
            if(isMove)
            {
                cameraController.panAngle   = 0.3 * (lastMouseX - stage.mouseX) + lastPanAngle;
                cameraController.tiltAngle  = 0.3 * (lastMouseY - stage.mouseY) + lastTiltAngle;
            }

            if(isUpMove)
                lookAtPosition.x -= 10;
            if(isDownMove)
                lookAtPosition.x += 10;
            if(isLeftMove)
                lookAtPosition.z -= 10;
            if(isRightMove)
                lookAtPosition.z += 10;

            cameraController.lookAtPosition = lookAtPosition;
            view.render();
        }
    }
}