Be a Supporter!

as2 character movement problem

  • 410 Views
  • 4 Replies
New Topic Respond to this Topic
Bum-Secks
Bum-Secks
  • Member since: Oct. 12, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
as2 character movement problem 2011-02-23 08:05:44 Reply

I'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?

Shadowii2
Shadowii2
  • Member since: Sep. 30, 2006
  • Offline.
Forum Stats
Member
Level 29
Game Developer
Response to as2 character movement problem 2011-02-23 08:36:26 Reply

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.

Bum-Secks
Bum-Secks
  • Member since: Oct. 12, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to as2 character movement problem 2011-02-23 09:13:54 Reply

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

Shadowii2
Shadowii2
  • Member since: Sep. 30, 2006
  • Offline.
Forum Stats
Member
Level 29
Game Developer
Response to as2 character movement problem 2011-02-23 10:50:07 Reply

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".

Bum-Secks
Bum-Secks
  • Member since: Oct. 12, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to as2 character movement problem 2011-02-23 11:50:47 Reply

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