Skip to main content
Participant
January 27, 2011
Answered

Mouse orientated movement in AS3?

  • January 27, 2011
  • 1 reply
  • 3142 views

I need help with something.

basically, i'm attempting to make a platform game, i know how to move left and right with the arrow keys, but that's quite boring.

i want to move my character based only on the x movement of my mouse. the more i move my mouse in a certain direction, the faster the character moves in that direction. i've been trying to modify the x-speed on my character based on a x value of the mouse, but nothing is working.

preferably, i want a code that keeps my cursor at the center of the screen, while still being able to detect x movements of the mouse.

i'd copy paste code from my program, but i'm fairly confident that doing so will serve no purpose. i need a new direction. i have no idea how to do this.

any help is greatly appreciated.

This topic has been closed for replies.
Correct answer kglad

thanks, the flash works now.

this is satisfactory, however, maybe i was unclear to as what i wanted. i wanted the x position of the mouse to change the acceleration of the object. a cursor all the way to the right would mean the object would travel to the right, and a cursor all the way to the left would make the object travel left. in other words, if i were to move the mouse to the right and leave it there, the object would continue moving right until i center the cursor, in which the object would then stop moving.

it's for a website and i want to make an interface where a banner of sorts would scroll pictures left/right depending on cursor location.

i really appreciate the help guys.


:

:

// speed should be greater than 0

var speed:Number=.01;

Mouse.hide();

var t:Timer=new Timer(100,0);

t.addEventListener(TimerEvent.TIMER,loopF);

t.start();

function loopF(e:TimerEvent):void{

yourchar.x+=(mouseX-stage.stageWidth/2)*speed;

}

1 reply

kglad
Community Expert
Community Expert
January 28, 2011

start a loop the repeatedly checks the mouse position.  and you can't control the position of the cursor but you can fake it by using a custom cursor and hiding the real one.

Opne_LaxzAuthor
Participant
January 28, 2011

in the flash, you can't see the mouse

the focus here isn't on the mouse, it's on the movement that the mouse is supposed to create. i don't know how to create it.

the more i move my mouse to the right, the faster the character moves to the right.

i want the code to do something like this:

mouse x position = 0, no movement

mouse x position = 5, slow movement right

mouse x position = 20, fast movement right

mouse x position = -5 slow movement left

etc.

kglad
Community Expert
Community Expert
January 28, 2011

:

// speed should be between 0 and 1.

var speed:Number=.9

Mouse.hide();

var t:Timer=new Timer(100,0);

t.addEventListener(TimerEvent.TIMER,loopF);

t.start();

function loopF(){

yourchar.x=(1-speed)*yourchar.x+speed*mouseX;

}