actionscript 2 to actionscript 3 my code -


can me transform code as2 as3?

for simple circle, want when go mouse cursor right , circle rotate (don't need move mouse cursor circle still rotating)

i know _root._xmouse goes t mousex , this._rotation goes this.displayobject.rotation

onclipevent(enterframe) {     this.xmouse = math.min(908, math.max(0, _root._xmouse));     if (_root._xmouse > 0)      {         var offset = stage.width / 2 - this.xmouse;         this._rotation = this._rotation + offset / 2000;     } else {         this._rotation = this._rotation - 0.02;     }     this._rotation = this._rotation % 180; } 

as3 version:

stage.addeventlistener( event.enter_frame, mouseover );  function mouseover( e: event ) : void  {     rota.mousex == math.min(908, math.max(0, stage.mousex));     if (stage.mousex > 0)      {         var offset = stage.stage.width / 2 - rota.mousex;         rota.rotation = rota.rotation + offset / 2000;     }else{         rota.rotation = rota.rotation - 0.02;     }     rota.rotation = rota.rotation % 180; } 

this should work :

var offset : int = 0; //declare variable (integer)  //stage.addeventlistener(mouseevent.mouse_move, mousemoving ); rota.addeventlistener(mouseevent.mouse_move, mousemoving );  function mousemoving ( evt : event ) : void {     rota.x = stage.mousex; //math.min(908, math.max(0, stage.mousex));      if (stage.mousex > 0)      {         offset = stage.stage.width / 2 - rota.mousex;         rota.rotation = rota.rotation + offset / 2000;     }else{         rota.rotation = rota.rotation - 0.02;     }     rota.rotation = rota.rotation % 180; } 

notes / tips :

  • declare variables outside of functions possible.

  • the evt in ( evt : event ) target reference whatever has .addeventlistener(mouseevent.mouse_move) attached it. if want move more 1 thing, give them same addevent same rota.addevent... can see function moves rota @ present, changing code using evt.rotation , evt.mousex etc... evt makes universal listening mousemoving function.


edit (based on comments) :

the variable speed sets speed of rotation. rotation set direction either -= or +=.

stage.addeventlistener(mouseevent.mouse_move, mousemoving ); //stage : direction rota.addeventlistener(event.enter_frame, mouserotating); //object : spin/rotate  var prevx:int = 0; var currx:int = 0; var directx: int = 0; //will update 1=left or 2=right  var speed : int = 7; //speed of rotation  function mousemoving ( evt : event ) : void {     prevx = currx; currx = stage.mousex;       if (prevx > currx) { directx = 1; }  //moving = left     else if (prevx < currx) { directx = 2; } //moving = right     //else { directx = 0;} //moving = none  }  function mouserotating ( evt : event ) : void {     evt.target.x = stage.mousex; //follow mouse      if ( directx == 1 ) { evt.currenttarget.rotation -= speed; }     if ( directx == 2 ) { evt.currenttarget.rotation += speed; }  } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -