-
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
-
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.
-
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
-
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.
-
_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.
-
_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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|