View Full Version : moving charictor left or right when mouse is moved left or right
impsvi
April 28th, 2006, 09:42 AM
using the code written by nathan99 i have a charictor which can move around and jump using the arrow keys and also mouseclick to jump.
Im desingning for a pda and wanted to make it so when the mouse is moved left or right the charitor moves left or right and when the mouse has stoped moving left or right the charictor stops.
On a pda this would work well as the charitor could be completely controled using the pen/stylus thing instead of having to use annoying fidderly keys.
I need to try and adapt this code, so it works as: if(xmouse)? somehow instead of if(keydown
if (Key.isDown(Key.LEFT)) {
if (speed>-maxmove) {
speed--;
}
any suggestions would be of much niceness thanks
DangerousDan
April 28th, 2006, 12:37 PM
if(this._x > _root._xmouse){ /*if the movieclip's X is less then the Mouse's X, move the moveclip towards the mouse.*/
this._x -= speed;
}
else if(this._x < _root._xmouse){ /*if the movieclip's X is greater then the Mouse's X, move the moveclip towards the mouse.*/
this._x += speed;
}
Basically if the moveclip's x is bigger then the mouse's it will start moving towards the mouse, and vice versa if the movieclip's x is less.
You could put that code in a onClipEvent(enterFrame) in the movieclip and I think it will work, been a while since I've used the mouse.
impsvi
April 28th, 2006, 02:24 PM
thanks dangerouse your code made sense but it didnt work when i applied it to my charictor movieclip as im using slightly diffrent code to move my charictor, i think, so i tried to mix your code with the code that makes my charitor move only i got a whole bunch of errors which i failed to fix because im new at actionscript and its still a bit bafferling.
Here the code i treid to make:
not quite sure were im going wrong.
thanks again
onClipEvent(enterFrame){
if(this._x > _root._xmouse){ /*if the movieclip's X is less then the Mouse's X, move the moveclip towards the mouse.*/
if (speed>-maxmove) {
// if the speed is greater than neg. maxmove
speed--;
// speed goes lower
this.gotoAndStop(2, 3);
// goto and stop the run frame
this._xscale = -100;
}}
else if(this._x < _root._xmouse){ /*if the movieclip's X is greater then the Mouse's X, move the moveclip towards the mouse.*/
if (speed<maxmove) {
// if the speed is smaller than maxmove
speed++;
// speed goes up
}
this._xscale = 100;
// scale is set to 100
// this is what rotates ur char
this.gotoAndStop(2, 3);
}}
Joppe
April 28th, 2006, 02:36 PM
onClipEvent (load) {
speed = 0;
maxmove = 7;
}
onClipEvent (enterFrame) {
if (this._x<_xmouse) {
this._xscale = 100;
speed++;
this.gotoAndStop(2, 3);
} else {
if (this._x>_xmouse) {
this._xscale = -100;
speed--;
this.gotoAndStop(2, 3);
}
}
if (speed>maxmove) {
speed = maxmove;
} else {
if (speed<-maxmove) {
speed = -maxmove;
}
}
this._x += speed;
trace(speed);
}
what about something like that?
impsvi
April 28th, 2006, 02:57 PM
thanks
im using nathan99's platform tutorial code and added your (joppe) code to the top of the charictor movieclip's action but
the charitor kept moving left constantly and flipping from side to side when the mouse was on one side of the screen.
seemed close to working though, thanks for trying
Joppe
April 28th, 2006, 03:09 PM
onClipEvent (load) {
speed = 0;
maxmove = 7;
}
onClipEvent (enterFrame) {
if (_x<_root._xmouse) {
this._xscale = 100;
speed++;
this.gotoAndStop(2, 3);
} else {
if (_x>_root._xmouse) {
this._xscale = -100;
speed--;
this.gotoAndStop(2, 3);
}
}
if (speed>maxmove) {
speed = maxmove;
} else {
if (speed<-maxmove) {
speed = -maxmove;
}
}
this._x += speed;
trace(speed);
}
Sorry about that :P
InfestedDemon
April 28th, 2006, 04:03 PM
can we see your work as most of these people have helped you and they deserve it...and i wanna see what you needed it for :pleased:
impsvi
April 28th, 2006, 04:14 PM
yay it works, thanks jopp,
but the charictor doesnt move the same speed as when the keys are pressed (he moves much slower) also the walk cycle animation doesnt not start or stop.
Also because this is designed for pda i only really want this code to be active when the mouse is down.
thanks alot for your help
impsvi
April 28th, 2006, 04:20 PM
ok ill try and upload the swf in a zip file.....
its a game about a red squirrell who get attacked by grey squirels who knick his bag of nuts just before winter kicks in, he has to go around getting his nuts back.
In britain the red squrriels were almost completely wiped out after the introduction of the dominent grey squirrel, so this game features a red squirrel and the bad guys are the grey squirrels
Joppe
April 28th, 2006, 04:44 PM
well for the first thing the not goto the run frame i cant do anything about seeing i just pasted your gotoandstop code. secondly you just need to add an
ActionScript Code:
onClipEvent (load) {
speed = 0;
maxmove = 7;
}
onClipEvent (enterFrame) {
if(Key.isDown(Key.1)){
if (_x<_root._xmouse) {
this._xscale = 100;
speed++;
this.gotoAndStop(2, 3);
} else {
if (_x>_root._xmouse) {
this._xscale = -100;
speed--;
this.gotoAndStop(2, 3);
}
}
}
if (speed>maxmove) {
speed = maxmove;
} else {
if (speed<-maxmove) {
speed = -maxmove;
}
}
this._x += speed;
trace(speed);
}
code
And it moving slower, adjust the maxMove variable.
impsvi
April 28th, 2006, 06:49 PM
thanks jopp really appreciate the help although this code when placed on the actions of the charictor give some errors which i tried to fix but failed.
sorry to take up your time jopp, thanks again.
**Error** Scene=Scene 1, layer=action, frame=2:Line 6: ')' or ',' expected
if(Key.isDown(Key.1)){
**Error** Scene=Scene 1, layer=action, frame=2:Line 19: Statement must appear within on/onClipEvent handler
if (speed>maxmove) {
**Error** Scene=Scene 1, layer=action, frame=2:Line 26: Statement must appear within on/onClipEvent handler
this._x += speed;
**Error** Scene=Scene 1, layer=action, frame=2:Line 27: Statement must appear within on/onClipEvent handler
trace(speed);
**Error** Scene=Scene 1, layer=action, frame=2:Line 28: Unexpected '}' encountered
}
Joppe
April 28th, 2006, 06:53 PM
onClipEvent (load) {
speed = 0;
maxmove = 7;
}
onClipEvent (enterFrame) {
if(Key.isDown(1)){
if (_x<_root._xmouse) {
this._xscale = 100;
speed++;
this.gotoAndStop(2, 3);
} else {
if (_x>_root._xmouse) {
this._xscale = -100;
speed--;
this.gotoAndStop(2, 3);
}
}
}
if (speed>maxmove) {
speed = maxmove;
} else {
if (speed<-maxmove) {
speed = -maxmove;
}
}
this._x += speed;
trace(speed);
}
Once again sorry no Key. was supposed to be there
impsvi
April 28th, 2006, 08:17 PM
when i increase all the maxmove values the charictor still will only move slowly left or right when using mouse but normally when using keys.
speed *= .85;
// speed is multiplied by .85
// the lower the faster is slows
when i increase this value the charictors accleration increases alot but only when using keys still no increase in speed with mouse.
Not sure why, sorry for being so clueless.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.