Controlling arithmetic operators

Im currently working on a game for flash with some very complex movement.
What I need to do is to change the direction the player moves when a certain key is pressed, making it the exact opposite of the direction the player would normally.

Example :
up arrow key moves the player forward, when the spacebar is hit the up arrow moves the player backwards.

So this is the code im using to move the player MC :
if (Key.isDown(Key.UP) ) {
if (angle == 360) {
this._y -=10;
}
}

so what needs to happen is that this._y -=10; needs to become this._y +=10;. So i thought ‘ill just use a variable!’, first problem was that the decision to change the operator comes from a different timeline, solved that with a _global variable, but when I entered :

this._y variable_name=10;

it didnt like it so I tried :

this._y [variable_name]=10;

that seemed ok, but when I tested the movie the player just wouldn’t move, I thought it may be to do with the fact that the variable_name was a string variable of either “-” or “+”

I would just re-write the code with the different operator, would seem the logical thing to do and then just change the conditions, but because there are so many movement statements that need to be controlled it would be to much code and a variable is the only logical way to control it.

anyone know what im doing wrong or if theres a specific way of representing operators?

(attached a demo of what im working on)

Why don’t you use a boolean variable, like this:


on (keyPress.SPACE) {
_root.SpaceDown = TRUE;
}

on (keyUp.SPACE) {
_root.SpaceDown = FALSE;
}

if (Key.isDown(Key.UP) ) {
if (_root.SpaceDown) {
if (angle == 360) {
this._y -=10;
} 
}else {
if (angle == 360) {
this._y +=10;
} 
}
}

I tried to peice some of it together from the code you had… and my exact AS coding is a little rusty… but that’s about the jist of it.

It would work fine but the thing is I have 36 if statements controlling the movement in comparision to the player angle, so your method would mean writing 36 else statements and leading to to much code.
(the only reason I have 36 if statements is because I couldn’t think of any mathematical calculation that would give me the movement for both X and Y in comparision to the angle of the player)
So I need to replace + or - with a variable which can then be reversed in everystatement in one go :-/

  • and - cannot be manipulated in that way. You will need to find some other solution, either through method handling (i.e. this.moveFoward(speed, isReversed)) or something like SeiferTim suggested… though my approach would be more like
speed = 2;
reverse = (Key.isDown(Key.SPACE)) ? -1 : 1;
myHorzMvmnt  = (Key.isDown(Key.RIGHT)-Key.isDown(Key.LEFT))*speed*reverse;