A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Character Following Mouse Help

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    7

    Lightbulb Character Following Mouse Help

    Hello
    For a current University Project I am trying to get a character to follow the users cursor and having them change direction depending on where the cursor is in relation to the character.

    Currently I have a scene made up of a isometric-like (think theme hospital) room where i would like my character (made up of 8 way sprites) to navigate around.

    I have managed to control my character via the arrow keys but I am lost in trying to find a solution to getting the character to follow the mouse cursor.
    And help would be greatly appreciated here.

    Thanks

  2. #2
    Senior Member
    Join Date
    May 2009
    Posts
    138
    There's three things you need to do to get mouse movement. First is listen for MouseEvent.MOUSE_MOVE on the stage and update a "destination" point with the event's stageX and stageY properties. Second is listen for Event.ENTER_FRAME and determine if the character is at the destination, if not move towards it. Third is selecting the direction on the sprite to display based on current position vs destination position.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    im lost. i have sample code for a character following mouse/cursor movement, however i need to implement the sprite changing properties into this instead of the character rotation properties it currently has, any examples???
    Thanks

  4. #4
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Instead of rotating, you need to display the appropriate sprite. You can figure out which one to display by comparing the character's position to the mouse's position. For example, if they have the same Y value and the mouse has a smaller X value, display the "moving left" sprite.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    _root.attachMovie("char","char",_root.getNextHighe stDepth());

    char.xSpeed = 0;
    char.ySpeed = 0;

    char.onEnterFrame = function(){

    if(Key.isDown(Key.UP)){
    trace("UP");
    this.ySpeed = -3;

    }
    else if(Key.isDown(Key.DOWN)){
    trace("DOWN");
    this.ySpeed = 3;

    }

    else {
    this.ySpeed = 0;
    }

    if(Key.isDown(Key.LEFT)){
    trace("LEFT");
    this.xSpeed = -3;

    }

    else if(Key.isDown(Key.RIGHT)){
    trace("RIGHT");
    this.xSpeed = 3;

    }

    else {
    this.xSpeed = 0;
    }


    if(this.xSpeed < 0) {
    char.gotoAndStop("right");
    }
    else if(this.xSpeed > 0){
    char.gotoAndStop("left");
    }
    else if(this.ySpeed > 0) {
    char.gotoAndStop("down");
    }
    else if(this.ySpeed < 0) {
    char.gotoAndStop("up")
    }



    this._x += this.xSpeed;
    this._y += this.ySpeed;
    }



    this is the kind of code im using to animate my character at the moment, how would i incorparate the mouse move function into this?
    this is really stressing me out

    thanks.

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    7
    _root.attachMovie("char","char",_root.getNextHighe stDepth());

    char.xSpeed = 0;
    char.ySpeed = 0;

    char.onEnterFrame = function(){

    if(Key.isDown(Key.UP)){
    trace("UP");
    this.ySpeed = -3;

    }
    else if(Key.isDown(Key.DOWN)){
    trace("DOWN");
    this.ySpeed = 3;

    }

    else {
    this.ySpeed = 0;
    }

    if(Key.isDown(Key.LEFT)){
    trace("LEFT");
    this.xSpeed = -3;

    }

    else if(Key.isDown(Key.RIGHT)){
    trace("RIGHT");
    this.xSpeed = 3;

    }

    else {
    this.xSpeed = 0;
    }


    if(this.xSpeed < 0) {
    char.gotoAndStop("right");
    }
    else if(this.xSpeed > 0){
    char.gotoAndStop("left");
    }
    else if(this.ySpeed > 0) {
    char.gotoAndStop("down");
    }
    else if(this.ySpeed < 0) {
    char.gotoAndStop("up")
    }



    this._x += this.xSpeed;
    this._y += this.ySpeed;
    }



    this is the kind of code im using to animate my character at the moment, how would i incorparate the mouse move function into this?
    this is really stressing me out

    thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center