Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to rotate and move with keyboard?

Status
Not open for further replies.

iostream71

Programmer
Mar 13, 2002
229
0
0
US
i have an object that, when pressing the left or right keys, rotates and if up or down is pressed, moves forward/back in the current direction pointing

 
you have or you need to know how to ? the question isnt clear.
 
oops...yeah, i have a clip with an object in it that i need to do that with :)
 
for a movie clip where (number) is the key code reprsenting the arrow

onClipEvent (enterframe) {
if (Key.isDown(number)) {
//whatever you want here
}
}

for an object use addlistner in the same way
 
i know how to do the key press detection stuff. it's the math i don't know how to set. i found some code that i'm using, but there's inertia added that i don't want(want to stop on a dime, so to speak)


this is in the clip i'm moving:

onClipEvent (enterFrame) {
_root.speed = speed;
_root.mph = Number(speed)*2;
_root.mph -= _root.mph%1;
if (_root.mph<1) {
_root.mph = 1;
}
if (Key.isDown(Key.UP)) {
speed += 2;
}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (Math.abs(speed)>20) {
speed *= .7;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 14;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 14;
}
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.boundary.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
onClipEvent (enterFrame) {
if (this._x> 600) {
this._x = -50;
}
}
onClipEvent (enterFrame) {
if (this._y> 450) {
this._y = -50;
}
}
onClipEvent (enterFrame) {
if (this._x <-50) {
this._x = 600;
}
}
onClipEvent (enterFrame) {
if (this._y <-50) {
this._y = 450;
}
}

 
all i think you need is

onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
this.rotation += 2;//or whatever
}
if (Key.isDown(Key.DOWN)) {
//
}
if (Key.isDown(Key.LEFT)) {
//
}
if (Key.isDown(Key.RIGHT)) {
//whatever
}
}

i think id replace key.down and other similar with numbers though.

 
ok, but how do i get rid of the inertia?? this is code for a car, and i'm moving a person o_O
 
just add your own code to the if statements

i stripped out the inertia above
 
there's a problem with that approach though. the person doesn't face the direction he's moving if i do that way
 
im unsure what the problem is...but if its a figure facing facing one way or another then put in a boolean value and depending on key press have him facing whatever way you want.
 
You have to maintain this part of the code, as it translates an 'x' and &quot;y&quot; positional value (forward movement) based on the rotation value you have accumulated.

x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.boundary.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;

It's really the core of positional translations based on direction. P.S. Bill Watson, i think I can use this for my issue also.

The 'x' & 'y' become temporary holders during the calculation. Review these two lines and make sure you understand what is accomplished. In your case speed is a constant (either 1 or 0), i assume your example person does not have a variable speed? Let your key depresses change speed from 0 to 1, and then the person starts to move in the current direction pointed to by 'rotation'

 
i figured out what i needed to do:

onClipEvent (enterFrame) {

if (Key.isDown(Key.LEFT)) {
this._rotation -= 2;
}
if (Key.isDown(Key.DOWN)) {
x = Math.sin(_rotation*(Math.PI/180))*2;
y = Math.cos(_rotation*(Math.PI/180))*2;
this._x += x;
this._y += y;
}
if (Key.isDown(Key.UP)) {
x = Math.sin(_rotation*(Math.PI/-180))*5;
y = Math.cos(_rotation*(Math.PI/-180))*5;
this._x -= x;
this._y -= y;
}
if (Key.isDown(Key.RIGHT)) {
this._rotation += 2;
}
}
 
perhaps you are right

i was just answering the question as to how to use the keyboard arrows to control an object on stage without thinking about what that movement was.

much depends on the complexity of the object as to whether you need that math at all. a simple stick figure does not.
 
Hello,

Hey bill I have a question for you. I just downloaded this FLA file.It's a message board. I put it on the web and I can't get it to start a new topic. Try downloading it: See if you can get it to work.
This would be a BIG help.

Thank you so much,

Billy

P.S. Julian is my first name. that's why I put it on the board.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top