Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MouseEvent3D not working! #708

Open
hewiefreeman opened this issue Sep 21, 2014 · 0 comments
Open

MouseEvent3D not working! #708

hewiefreeman opened this issue Sep 21, 2014 · 0 comments

Comments

@hewiefreeman
Copy link

This is my first time posting on the away 3D forums, so hello community! Name's Nick ^^
I've ran into a problem with MouseEvent3D when assigned to a Mesh made from an asset loader. I'm working on this project in Flash Pro CS6, so I made a movieclip on the stage with a class that loads up all necessary models throughout the game into Mesh and TextureMaterials in the Main.as. It also has it's own layer because I'll be loading models on 2 separate stage frames (the game and map editor frames - then there's a few more frames for menus and such). Here's the asset loader functions inside the assetsLoader movieclip's class:

//MODEL EMBEDS

[Embed(source="models/WaterHex.3ds", mimeType="application/octet-stream")]
public var WaterHexM:Class;
[Embed(source="models/GrassHex.3ds", mimeType="application/octet-stream")]
public var GrassHexM:Class;
[Embed(source="models/RockHex.3ds", mimeType="application/octet-stream")]
public var RockHexM:Class;
[Embed(source="models/MtnHex.3ds", mimeType="application/octet-stream")]
public var MtnHexM:Class;
[Embed(source="models/Tree1.3ds", mimeType="application/octet-stream")]
public var Tree1M:Class;
[Embed(source="images/water_normals.jpg")]
public var WaterNormals:Class;
[Embed(source="images/GrBlade.png")]
public var GrassBlade:Class;

//FUNCTIONS

public function loadMapEditorAssets(){
    //PARSERS FOR LOADER3D
    Parsers.enableAllBundled();
    assetLoaderContext = new AssetLoaderContext();

    _loader = new Loader3D();
    loadingTilesOn = "water";
    _loader.addEventListener(AssetEvent.ASSET_COMPLETE, onLoadAssetME);
    setupMandG();
}

//LOAD MAP EDITOR MODELS

private function setupMandG(){

    if(loadingTilesOn == "water"){
        if(MovieClip(root).waterGeom == null){
            _loader.loadData(new WaterHexM(), assetLoaderContext);
        }else{
            MovieClip(root).waterMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).waterMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            loadingTilesOn = "grass";
            setupMandG();
            return;
        }
    }else if(loadingTilesOn == "grass"){
        if(MovieClip(root).grassGeom == null){
            _loader.loadData(new GrassHexM(), assetLoaderContext);
        }else{
            MovieClip(root).grassMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).grassMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            loadingTilesOn = "mountain";
            setupMandG();
            return;
        }
    }else if(loadingTilesOn == "mountain"){
        if(MovieClip(root).mtnGeom == null){
            _loader.loadData(new MtnHexM(), assetLoaderContext);
        }else{
            MovieClip(root).mtnMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).mtnMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            loadingTilesOn = "rock";
            setupMandG();
            return;
        }
    }else if(loadingTilesOn == "rock"){
        if(MovieClip(root).rockGeom == null){
            _loader.loadData(new RockHexM(), assetLoaderContext);
        }else{
            MovieClip(root).rockMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).rockMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            loadingTilesOn = "tree1";
            setupMandG();
            return;
        }
    }else if(loadingTilesOn == "tree1"){
        if(MovieClip(root).tree1Geom == null){
            _loader.loadData(new Tree1M(), assetLoaderContext);
        }else{
            MovieClip(root).tree1Mat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).tree1Mat.lightPicker = MovieClip(root).mapEditor._lightPicker;

            //BACKDROP MAP
            MovieClip(root).backDropPlaneMat = MovieClip(root).tree1Mat;
            MovieClip(root).backDropPlaneMat.alpha = 0;

            //DO AFTER MODEL INIT
            if(MovieClip(root).mapEditor.doAfterInitM == "newMap"){
                MovieClip(root).mapEditor.buildMap();
            }

            //DISPOSE
            _loader.removeEventListener(AssetEvent.ASSET_COMPLETE, onLoadAssetME);
            loadedMesh = null;
        }
    }
}

private function onLoadAssetME(evt:AssetEvent){
    loadedMesh = evt.asset as Mesh;
    if (evt.asset.assetType == AssetType.MESH) {
        if(loadingTilesOn == "water"){
            MovieClip(root).waterGeom = loadedMesh.geometry;

            loadingTilesOn = "grass";
            setupMandG();
        }else if(loadingTilesOn == "grass"){
            MovieClip(root).grassGeom = loadedMesh.geometry;

            loadingTilesOn = "mountain";
            setupMandG();
        }else if(loadingTilesOn == "mountain"){
            MovieClip(root).mtnGeom = loadedMesh.geometry;

            loadingTilesOn = "rock";
            setupMandG();
        }else if(loadingTilesOn == "rock"){
            MovieClip(root).rockGeom = loadedMesh.geometry;

            loadingTilesOn = "tree1";
            setupMandG();
        }else if(loadingTilesOn == "tree1"){
            MovieClip(root).tree1Geom = loadedMesh.geometry;

            //DO AFTER MODEL INIT
            if(MovieClip(root).mapEditor.doAfterInitM == "newMap"){
                MovieClip(root).mapEditor.buildMap();
            }

            //DISPOSE
            _loader.removeEventListener(AssetEvent.ASSET_COMPLETE, onLoadAssetME);
            loadedMesh = null;
        }
    }else if (evt.asset.assetType == AssetType.MATERIAL) {
        if(loadingTilesOn == "water"){
            MovieClip(root).waterMat = evt.asset as TextureMaterial;
            MovieClip(root).waterMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).waterMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            MovieClip(root).waterMat.specular = 0;
            MovieClip(root).waterMat.ambientColor = 0x303040;
            MovieClip(root).waterMat.ambient = 1;
            MovieClip(root).waterMat.bothSides = true;

        }else if(loadingTilesOn == "grass"){
            MovieClip(root).grassMat = evt.asset as TextureMaterial;
            MovieClip(root).grassMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).grassMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            MovieClip(root).grassMat.specular = 0;
            MovieClip(root).grassMat.ambientColor = 0x303040;
            MovieClip(root).grassMat.ambient = 1;

        }else if(loadingTilesOn == "mountain"){
            MovieClip(root).mtnMat = evt.asset as TextureMaterial;
            MovieClip(root).mtnMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).mtnMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            MovieClip(root).mtnMat.specular = 0;
            MovieClip(root).mtnMat.ambientColor = 0x303040;
            MovieClip(root).mtnMat.ambient = 1;

        }else if(loadingTilesOn == "rock"){
            MovieClip(root).rockMat = evt.asset as TextureMaterial;
            MovieClip(root).rockMat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).rockMat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            MovieClip(root).rockMat.gloss = 30;
            MovieClip(root).rockMat.specular = 0.4;
            MovieClip(root).rockMat.ambientColor = 0x303040;
            MovieClip(root).rockMat.ambient = 1;

        }else if(loadingTilesOn == "tree1"){
            MovieClip(root).tree1Mat = evt.asset as TextureMaterial;
            MovieClip(root).tree1Mat.shadowMethod = new DitheredShadowMapMethod(MovieClip(root).mapEditor._light);
            MovieClip(root).tree1Mat.lightPicker = MovieClip(root).mapEditor._lightPicker;
            MovieClip(root).tree1Mat.specular = 0;
            MovieClip(root).tree1Mat.ambientColor = 0x303040;
            MovieClip(root).tree1Mat.ambient = 1;

            //BACKDROP MAP
            MovieClip(root).backDropPlaneMat = MovieClip(root).tree1Mat;
            MovieClip(root).backDropPlaneMat.alpha = 0;
        }
    }
}

So essentially I check to see if I've loaded the geometries into the variables in the Main.as, and if I haven't I load them (and the TextureMaterials) up with an assetLoader. Then I make a View3D inside another movieclip on the stage named "mapEditor". I set up the lights and cameraController to give a birdseye view of the made tiles. I use a for loop to make the map and give each map tile a MouseEvent3D. I also made an invisible plane under the map that calls the same MouseEvent3D (but nothing happens at all):


//START A NEW MAP
public function startNew(mapSize:String, mapName:String){
    //clearMap(); WILL DISPOSE OF THE CURRENT VIEW WHEN MAKING A NEW MAP AGAIN

    this.x = 0;
    this.y = 0;

    theName = mapName;
    theSize = mapSize;

    if(mapSize == "small"){
        dimWidth = 12;
        dimHeight = 10;
    }else if(mapSize == "medium"){
        dimWidth = 14;
        dimHeight = 12;
    }else if(mapSize == "large"){
        dimWidth = 16;
        dimHeight = 14;
    }

    doAfterInitM = "newMap";
    initEditor();
}

public function initEditor(){

    MovieClip(root).mouseArea1.mouseEnabled = false;///////////////////
    MovieClip(root).mouseArea2.mouseEnabled = false;//FOR A CUSTOM GESTURE SYSTEM

    _view = new View3D();
    this.addChild(_view);

    //SIGHT DISTANCE
    _view.camera.lens.far = 2100;
    _view.camera.y = 100;
    _view.mousePicker = PickingType.RAYCAST_FIRST_ENCOUNTERED;

    //CAMERA CONTROLLER
    _cameraController = new HoverController(_view.camera, null, 0, 89);

    _cameraController.minTiltAngle = 50;///////////55 or 50 DEFAULT
    _cameraController.maxTiltAngle = 89;
    _cameraController.distance = _cameraController.tiltAngle*(_cameraController.tiltAngle*0.06);

    //SUN LIGHT
    _light = new DirectionalLight(-1, -1, -1);
    _light.x = 666;
    _light.y = 1000;
    _light.z = 666;
    _light.rotationY = 90;
    _light.castsShadows = true;
    _light.ambient = 1;
    _light.shadowMapper.depthMapSize = 512;
    _lightPicker = new StaticLightPicker([_light]);
    _view.scene.addChild(_light);

    //SETUP TILE GEOM AND MATS
    MovieClip(root).assetsLoader.loadMapEditorAssets(); //THIS CALLS THE CODE IN THE ASSETS LOADER WHICH THEN CALLS buildMap() AFTER IT LOADS THE NECESSARY MODELS IF ANY

    //AWAY3D PERFORMANCE STATS
    addChild(new AwayStats(_view));
}

//THEN BUILD THE MAP
public function buildMap(){
    theRowOn = 1;
    colOn = 1;
    tileDir = "down";

    for(i = 0; i < dimWidth*dimHeight; i++){

        tempMapMesh = new Mesh(MovieClip(root).grassGeom, MovieClip(root).grassMat); //RIGHT NOW THEY'RE ALL THE SAME BECAUSE IT'S A "NEW" MAP

        tempMapMesh.scale(100);
        tempMapMesh.rotationY = 30;
        tempMapMesh.x = (-1)*(colOn*right);
        tempMapMesh.y = -120;
        tempMapMesh.z = (theRowOn*downDown)+this[tileDir];
        tempMapMesh.name = colOn+"_"+theRowOn;
        _view.scene.addChild(tempMapMesh);

        tempMapMesh.mouseEnabled = true;
        tempMapMesh.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
        tempMapMesh.addEventListener(MouseEvent3D.MOUSE_DOWN, clickedTile);

        AssetLibrary.addAsset(tempMapMesh);

        checkForColor(colOn, theRowOn);

        if(tileDir == "down"){
            tileDir = "up";
        }else{
            tileDir = "down";
        }

        if(colOn == dimWidth){
            colOn = 0;
            theRowOn++;
            tileDir = "down";
        }
        colOn++;
    }

    tempMapMesh = null;

    //ADD A PLANE BEHIND THE MAP SO YOU CAN CLICK IN BETWEEN & OUT OT THE MAP
    backDropPlane = new Mesh(new PlaneGeometry((dimWidth*4)*right, (dimHeight*4)*downDown), MovieClip(root).backDropPlaneMat);
    backDropPlane.y = -50;
    backDropPlane.mouseEnabled = true;
    backDropPlane.addEventListener(MouseEvent3D.MOUSE_DOWN, clickedTile);
    _view.scene.addChild(backDropPlane);

    //add listeners
    this.addEventListener(Event.ENTER_FRAME, manageView);
    trace("WHAT THE FAQ.");
}

The MoseEvent3D clickedTile doesn't get triggered at all! The map is there visible on the screen and I know the backdrop's in the right place because I've tested it without a TextureMaterial (and we all know what that does). NOTE: this was working before when I would load up the Meshes and TextureMaterials into the mapEditor movieclip, but the problem with that is that I'd have to embed the models every time the mapEditor (or the game's) movieclip is loaded onto the stage again which I discovered would build up a bunch of system trash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant