Skip navigation
Orithedor
Currently Being Moderated

3D rotation script stuck at 0,0

Oct 12, 2013 6:15 PM

Tags: #flash #script #action_script_3 #animation #cs6 #actionscript_3.0 #actionscript_3 #cc #3d-rotation

Hello,

 

The context of this project was to create a square that followed the movement of the mouse on a 3D scale, it's hard to explain but I will post the script so feel free to test and experiment. If you have played it, it is the same style that the Crysis 3 menu uses. Unfortunately whenever I run it the centre offset (or whatever it is called) is stuck to the top left hand corner of the canvas.

 

I'm sorry if this is blatantly obvious but I'm not experienced enough to see the problem.

 

Below is the script, the objects name is "pnt":

 

import flash.events.Event;

 

 

addEventListener (Event.ENTER_FRAME,ongo);

 

 

var mpx:Number=0;

var mpy:Number=0;

 

 

function ongo(e:Event):void

{

          pnt.rotationX = -(pnt.rotationX-mouseY)/10;

          pnt.rotationY = (pnt.rotationY-mouseX)/10;

}

 

 

// Below block ensure symbol rotates at any canvas size

var pp:PerspectiveProjection = this.transform.perspectiveProjection;

 

 

onResize(null);

stage.align = StageAlign.TOP_LEFT;

stage.scaleMode = StageScaleMode.NO_SCALE;

stage.addEventListener(Event.RESIZE, onResize);

function onResize(e:Event):void

{

          pnt.x = (stage.stageWidth) / 2;

          pnt.y = (stage.stageHeight) / 2;

          pp.projectionCenter = new Point(pnt.x, pnt.y);

}

// End of block

 

Thank you for reading!

 

(just realised that the two variables [mpx and mpy] serve no purpose, I should admit I have been using these codes from video tutorials so an explaination in less jargon would be beneficial!)

 

Message was edited by: Orithedor

 
Replies
  • Currently Being Moderated
    Oct 14, 2013 3:15 PM   in reply to Orithedor

    I'm not really sure whatever that Crysis menu does but if you're just trying to get the object to "look" toward the mouse cursor, slightly modding your code (paste into a new AS3 document), here's a full code example (I'm creating "pnt" in code as well, comment it out to use your object):

     

    import flash.events.Event;

    import flash.display.Sprite;

     

    stage.align = StageAlign.TOP_LEFT;

    stage.scaleMode = StageScaleMode.NO_SCALE;

    stage.addEventListener(Event.RESIZE, onResize);

    // only listen to the mouse moving on the stage

    stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);

     

    // save centered projection

    var pp:PerspectiveProjection = this.transform.perspectiveProjection;

     

    // generate pnt center registration

    var pnt:Sprite = new Sprite();

    pnt.graphics.lineStyle(5,0x0,1,true);

    pnt.graphics.beginFill(0xFF0000,1);

    pnt.graphics.drawRect(-100,-50,200,100);

    pnt.graphics.endFill();

    addChild(pnt);

     

    // center pnt

    onResize(null);

     

    function onMove(e:Event):void

    {

              pnt.rotationY = ((stage.stageWidth / 2) - mouseX)/5;

              pnt.rotationX = (mouseY - (stage.stageHeight / 2))/5;

    }

     

    function onResize(e:Event):void

    {

              pnt.x = (stage.stageWidth) / 2;

              pnt.y = (stage.stageHeight) / 2;

              pp.projectionCenter = new Point(pnt.x, pnt.y);

    }

     

    Maybe that can get you started.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 17, 2013 9:47 AM   in reply to Orithedor

    If you're all set please mark a correct answer so we can filter out unanswered questions. You're welcome and good luck!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points