The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumCrouching and not moving

10 4434
Midmenj
offline
Midmenj
216 posts
Nomad

So, I can crouch, but when I try to jump without moving it doesnt but I can crouch and stand still. But when I move and press crouch I still crouch. How do I make it so I don't crouch when I move. Also when I move and press jump it jumps which is fine. Here is the code, It is in AS2:

In walking:


onEnterFrame = function() {

if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && speed < maxSpeed) { speed += accel }
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) && speed > -maxSpeed) { speed -= accel }

if (speed > 0) { _xscale = 100 }
if (speed < 0) { _xscale = -100 }

if ( (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) ) || (Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT) ) ) {
if (Math.abs(speed) < accel ) {
speed = 0
} else {
speed -= speed/Math.abs(speed) * accel
}
}

_x += speed

if (!Key.isDown(Key.UP) ) { jumpIsDown = false }
if (Key.isDown(Key.UP) && !jumpIsDown) {
jumpIsDown = true
gravity = -jumpheight;
gotoAndStop("jump&quot
} else if (!_root.ground.hitTest(_x + speed, _y + gravity, true) ) {
gravity == 0
gotoAndStop("jump&quot
} else if (speed == 0) {
gotoAndStop("idle&quot
}
if (Key.isDown(Key.DOWN)) {
gravity = 0
gotoAndStop("crouch&quot
}else if (speed == 0) {
gotoAndStop("idle&quot
}


In Idle:

onEnterFrame = function() {

if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) ) { speed += accel; _xscale = 100 }
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) ) { speed -= accel; _xscale = -100 }

_x += speed

if (!Key.isDown(Key.UP) ) { jumpIsDown = false }
if (Key.isDown(Key.UP) && !jumpIsDown) {
jumpIsDown = true
gravity = -jumpheight;
gotoAndStop("jump&quot
} else if (!_root.ground.hitTest(_x + speed, _y + gravity, true) ) {
gravity = 0
gotoAndStop("jump&quot
} else if (speed != 0) {
gotoAndStop("walk&quot
}
if (Key.isDown(Key.DOWN)) {
gravity = 0
gotoAndStop("crouch&quot
}else if (speed == 0) {
gotoAndStop("idle&quot
}
}
stop();


In Jumping frame:

onEnterFrame = function() {
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && speed < maxSpeed) { speed += accel/2 }
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) && speed > -maxSpeed) { speed -= accel/2 }

_x += speed

if (_root.ground.hitTest(_x + speed, _y + gravity, true)) {
while (!_root.ground.hitTest(_x, _y, true)) {
_y++
}
gravity = 0
if (Math.abs(speed) < accel ) { speed = 0; gotoAndStop("idle&quot } else { gotoAndStop("walk&quot }
} else {
gravity ++;
_y += gravity;
if (_y > 1000) { gravity == 0; speed = 0; _y = 200; _x = 200 }
}
}


In crouching frame:

if (Key.isDown(Key.DOWN)) {
gravity = 0
gotoAndStop("crouch&quot
}else if (speed == 0) {
gotoAndStop("idle&quot
}


Also, I am doing the code in frames. So it's like the first frame layer is named walk or jump, depending on what it is, the second layer of frames is the code and the last is the animation that I want.

Thanks for the help. I gave you everything I have.
  • 10 Replies
plasmafish
offline
plasmafish
252 posts
Nomad

Try pulling the code out of the frames and putting it on the characters movie clip. Use onClipEvent(load) for initiating variables and onClipEvent(enterFrame) for everything else. So instead of gotoAndStop, use this.gotoAndStop.

Once you do that, you will have a good starting point for debugging your game.

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

Impressing code!

Midmenj
offline
Midmenj
216 posts
Nomad

Thanks, but is there anyway you can help me with the code i already have?

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

It seems to me It's most likely a variable thing. I'll look at it when I have more time :P

Midmenj
offline
Midmenj
216 posts
Nomad

ok thanks!

Smokeshow
offline
Smokeshow
69 posts
Peasant

o.O man how much you worked for this

Dannydaninja
offline
Dannydaninja
948 posts
Nomad

o.O man how much you worked for this



Haha, that's nothing compared to a professional like Jmtob2 or whatever he's called :P

Would have taken me about 2 hours.
Dannydaninja
offline
Dannydaninja
948 posts
Nomad

Wait, are there any compiler errors? If so, what are they? I think I might have found the problem...

Midmenj
offline
Midmenj
216 posts
Nomad

No, no errors. And this code sadly took me a few days to figure out. No thanks to that dam crouch code.

plasmafish
offline
plasmafish
252 posts
Nomad

Have you followed my initial consideration? Without doing that it won't be easy to debug because your code is all over the place.

Showing 1-10 of 10