away3d/away3d-core-fp11 — Issue #152: HoverController works but not always...
Author: DarceyLloyd
Created: 2011-10-14 15:32:59+00:00
State: open

I was cooking up a template class for Away3D4 and found that the HoverController wasn't updating, tried lots of stuff but then found if I simply added any movement to a primitive in the scene the HoverController would start to work? Without the movement of the primitive the hovercamera would move about 1 or 2 pixels and lock.

Here's the code (see lines 142 to 159 - uncomment 156 to get the HoverController to work)

[code]
package
{
import away3d.cameras.*;
import away3d.containers.*;
import away3d.controllers.HoverController;
import away3d.core.base.*;
import away3d.debug.*;
import away3d.entities.Mesh;
import away3d.events.*;
import away3d.library.AssetLibrary;
import away3d.library.assets.AssetType;
import away3d.lights.DirectionalLight;
import away3d.lights.PointLight;
import away3d.loaders.*;
import away3d.loaders.misc.AssetLoaderContext;
import away3d.loaders.parsers.*;
import away3d.materials.BitmapMaterial;
import away3d.materials.ColorMaterial;
import away3d.materials.methods.FilteredShadowMapMethod;
import away3d.primitives.Plane;
import away3d.primitives.WireframeSphere;

import flash.display.*;
import flash.events.*;
import flash.geom.Vector3D;
import flash.text.AntiAliasType;
import flash.utils.getTimer;

[SWF(backgroundColor="#000000", frameRate="60", quality="LOW")]

public class Away3D4_Template extends Sprite
{

//engine variables
private var scene:Scene3D;
private var camera:Camera3D;
private var view:View3D;
private var hoverController:HoverController;

//signature variables
private var Signature:Sprite;
private var SignatureBitmap:Bitmap;

//material objects
private var groundMaterial:BitmapMaterial;

//scene objects
private var light:DirectionalLight;

//navigation variables
private var move:Boolean = false;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var lastMouseX:Number;
private var lastMouseY:Number;

/**
* Constructor
*/
public function Away3D4_Template()
{
trace("Away3D4_Template()");

init();
}

/**
* Global initialise function
*/
private function init():void
{
trace("init()");
initEngine();
initObjects();
initListeners();
}

/**
* Initialise the engine
*/
private function initEngine():void
{
trace("initEngine()");

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

scene = new Scene3D();

//setup camera for optimal shadow rendering
camera = new Camera3D();
camera.lens.far = 10000;

view = new View3D();
view.scene = scene;
view.camera = camera;

//setup controller to be used on the camera
hoverController = new HoverController(camera, null, -5, -5, -1500);

//view.addSourceURL("srcview/index.html");
addChild(view);

addChild(new AwayStats(view));
}

/**
* Initialise the scene objects
*/
private var p:WireframeSphere;
private function initObjects():void
{
trace("initObjects()");
light = new DirectionalLight();
scene.addChild(light);

p = new WireframeSphere();
scene.addChild(p);

var t:Trident = new Trident();
t.scale(0.5);
scene.addChild(t);
}

/**
* Initialise the listeners
*/
private function initListeners():void
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(Event.RESIZE, onResize);
onResize();
}

/**
* Navigation and render loop
*/
private function onEnterFrame(event:Event):void
{

if (move) {
hoverController.panAngle = 0.3 * (stage.mouseX - lastMouseX) + lastPanAngle;
hoverController.tiltAngle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle;
}

// Bit of debug to see what's going on
var msg:String = "mx:" + stage.mouseX.toFixed(1) + " my:" + stage.mouseY.toFixed(1) + " lp:" + lastPanAngle.toFixed(1) + " lt:" + lastTiltAngle.toFixed(1);
msg += " pan:" + hoverController.panAngle.toFixed(1) + " tilt:" + hoverController.tiltAngle.toFixed(1) + " move:" + move;
trace(msg);

// Uncommend the line below and the hover camera will work?
//p.rotationX += 0.1;

view.render();
}

/**
* Mouse down listener for navigation
*/
private function onMouseDown(event:MouseEvent):void
{
lastPanAngle = hoverController.panAngle;
lastTiltAngle = hoverController.tiltAngle;
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
move = true;
stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
* Mouse up listener for navigation
*/
private function onMouseUp(event:MouseEvent):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
* Mouse stage leave listener for navigation
*/
private function onStageMouseLeave(event:Event):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
* stage listener for resize events
*/
private function onResize(event:Event = null):void
{
view.width = stage.stageWidth;
view.height = stage.stageHeight;

}
}
}
[/code]