Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsI'm working on a side scroller, and i'm using eased movement, so....
speed=0
maxspeed=14
if (speed<maxspeed){
speed++;
}
Now, speed is set to 0 whenever the arrow keys are either both pressed at the same time, or not pressed at all. The problem i'm having is, there's the instance where you can press left (or right) exactly after letting go of right (or left), wich bypasses both hitting the keys at the same time as well as not pressing either.
is there a way to set speed to 0 whenever you turn around like this?
if(holdingRight && !holdingLeft) {
if(speed < maxSpeed) {
speed++;
}
} else if(holdingLeft && !holdingRight) {
if(speed > -maxSpeed) {
speed--;
}
} else {
if(speed < 0) {
speed++;
} else if (speed > 0) {
speed--;
}
}
Guess that's the simpliest method I can think of quickly. Obviously just swap "holdingRight" and "holdingLeft" with whatever you're using to check what button is pressed and what is not.
At 2/23/11 08:36 AM, Shadowii2 wrote: if(holdingRight && !holdingLeft) {
if(speed < maxSpeed) {
speed++;
}
} else if(holdingLeft && !holdingRight) {
if(speed > -maxSpeed) {
speed--;
}
} else {
if(speed < 0) {
speed++;
} else if (speed > 0) {
speed--;
}
}
Hmm how do i use the variable when it goes into the negatives?
if (going right){
character._x+=speed;
}
if (going left){
character._x-=speed;
}
Thanks for the help though, the variable easing works at least =P
Player._x += speed;
As simple as that, granted that positive is right and negative is left. When the speed goes below zero, it tries to do "Player._x + (-speed)" equation, which is the same as "Player._x - speed". Likewise if speed is below zero, and it tries to do "Player._x - (-speed)", it is the same as "Player._x + speed".
At 2/23/11 10:50 AM, Shadowii2 wrote: Player._x += speed;
As simple as that, granted that positive is right and negative is left. When the speed goes below zero, it tries to do "Player._x + (-speed)" equation, which is the same as "Player._x - speed". Likewise if speed is below zero, and it tries to do "Player._x - (-speed)", it is the same as "Player._x + speed".
Thanks for explaining it, I got it working a while ago but couldn't understand why it did. =)
Here's what i ended up with. The character eases left and right without the annoying slippery effect
speed=0;
maxSpeed=14;
onEnterFrame = function() {
guy._x+=speed;
if(Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
if(speed < maxSpeed) {
if(speed<0){
speed=0;
}else{
speed+=2;
}
}
} else if(Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
if(speed > -maxSpeed) {
if(speed>0){
speed=0;
}else{
speed-=2;
}
}
} else {
speed=0;
}
}
Thanks again guys, now i just need to work the slide/stumble animation in there