Skip to main content
Jan Meisel
Participating Frequently
June 22, 2019
Answered

ActionScript function (move object with cursor)

  • June 22, 2019
  • 1 reply
  • 716 views

I have created a piece of code, based on code from this tutorial

Design simple interactive content |

which makes the object turn at 180 degree following the cusor movements.

I like to ask if anybody can tell me what code I should use, if I would like the object to follow the cursor around the stage ?

I have the following code in ActionScript 3,

stage.on('stagemousemove', function(e){

var radians = Math.atan2(e.localY - _this.test.y, e.localX - _this.test.x);

var degrees = radians * (180 / Math.PI);

_this.test.rotation = degrees - 180;

});

Also if anybody could tell me where to find ActionScript code in general and if possible examples of ActionScript code and functions.

Thanks

This topic has been closed for replies.
Correct answer kglad

that's not as3.  it looks like createjs and you're missing the definition for _this.

it's also not clear what you want.  if you want _this.test to 'tween' to the cursor use something like:

var _this=this;

var speed = .95; // 0 to 1

var fI;

stage.on('stagemousemove', function(e){

var radians = Math.atan2(e.localY - _this.test.y, e.localX - _this.test.x);

var degrees = radians * (180 / Math.PI);

_this.test.rotation = degrees - 180;

_this.test.x = speed*_this.test.x+(1-speed)*e.localX;

_this.test.y = speed*_this.test.y+(1-speed)*e.localY;

clearInterval(fI);

fI=setInterval(f.bind(e),50);

});

function f(){

_this.test.x = speed*_this.test.x+(1-speed)*this.localX;

_this.test.y = speed*_this.test.y+(1-speed)*this.localY;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 22, 2019

that's not as3.  it looks like createjs and you're missing the definition for _this.

it's also not clear what you want.  if you want _this.test to 'tween' to the cursor use something like:

var _this=this;

var speed = .95; // 0 to 1

var fI;

stage.on('stagemousemove', function(e){

var radians = Math.atan2(e.localY - _this.test.y, e.localX - _this.test.x);

var degrees = radians * (180 / Math.PI);

_this.test.rotation = degrees - 180;

_this.test.x = speed*_this.test.x+(1-speed)*e.localX;

_this.test.y = speed*_this.test.y+(1-speed)*e.localY;

clearInterval(fI);

fI=setInterval(f.bind(e),50);

});

function f(){

_this.test.x = speed*_this.test.x+(1-speed)*this.localX;

_this.test.y = speed*_this.test.y+(1-speed)*this.localY;

}

Jan Meisel
Participating Frequently
June 23, 2019

Thanks, and yes you are right, it is createjs :-)

Here is the complete code for the rotation of an object, posted in my my question, if anybody like to try it out.

stage.enableMouseOver(30);

var _this = this;

stage.on('stagemousemove', function(e){

var radians = Math.atan2(e.localY - _this.test.y, e.localX - _this.test.x);

var degrees = radians * (180 / Math.PI);

_this.test.rotation = degrees - 90;

});

kglad
Community Expert
Community Expert
June 23, 2019

you're welcome.