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 ForumCrouch program

11 4627
Midmenj
offline
Midmenj
216 posts
Nomad

Hey, I was wondering if anyone knew how to fix this code I was working on. It's in Action Script 2.0 and is in the time line.

Here is the code:

if (!Key.isDown(Key.DOWN) ) { IsCrouch = false }
if (Key.isDown(Key.DOWN))
IsCrouch = true
gotoAndStop("crouch&quot
}else if (speed == 0) {
gotoAndStop("idle&quot
}


What it's suppose to do is when you press the down arrow it goes to the crouching animation called "crouch" and when you let go, it goes back to idle.
Unfortunately it doesn't work. Please help if you can.

Thanks.
  • 11 Replies
plasmafish
offline
plasmafish
252 posts
Nomad

I would need to see more code that's interacting with the character control to figure this one out. Everything you have there looks fine, unless it's a syntax or naming error.

Darkroot
offline
Darkroot
2,763 posts
Peasant

Yeah, I agree with plasma I don't see anything wrong, the problem is probably located somewhere else or you made a mistake somewhere. Or more likely don't fully understand the code so entire sections are probably wrong, because this is a relatively simple thing to do.

Midmenj
offline
Midmenj
216 posts
Nomad

it says syntax error.

driejen
offline
driejen
486 posts
Nomad

You need to put in the missing open brace at line 2;

if (!Key.isDown(Key.DOWN) ) { IsCrouch = false }
if (Key.isDown(Key.DOWN)){
IsCrouch = true
gotoAndStop("crouch&quot
}else if (speed == 0) {
gotoAndStop("idle&quot
}

Midmenj
offline
Midmenj
216 posts
Nomad

it didn't work for me....

driejen
offline
driejen
486 posts
Nomad

It works for me... Have you made sure to put this inside an onEnterFrame function? or a function called by an onEnterFrame function? If you don't then this bit of code will only be called on once at the beggining or even never at all. Also I'm not sure why you have quotation marks on "crouch" and "idle", if you have made variables called 'crouch' and 'idle' that hold integers of to hold frame numbers, then you don't need the quotation marks to refer to those variables.

Midmenj
offline
Midmenj
216 posts
Nomad

yes they are inside EnterFrame functions.

I have this now:

onEnterFrame = function() {
if (Key.isDown(Key.DOWN))
gravity = -jumpheight;
gotoAndStop("crouch&quot
gravity == 0
else if (!Key.isDown(Key.DOWN))
gotoAndStop("idle&quot
}


and it says "else encountered with no if" Not sure what that means
plasmafish
offline
plasmafish
252 posts
Nomad

Try taking the second "if" away.

plasmafish
offline
plasmafish
252 posts
Nomad

Actually, what I mean is this:

onEnterFrame = function() {
if (Key.isDown(Key.DOWN)){
gravity = -jumpheight;
gotoAndStop("crouch"
gravity == 0;
}
if (!Key.isDown(Key.DOWN)){
gotoAndStop("idle"
}
}
Midmenj
offline
Midmenj
216 posts
Nomad

@plasmafish it works but it doesn't have gravity even though in the code it says it should. I am making a platformer if that helps.

driejen
offline
driejen
486 posts
Nomad

onEnterFrame = function() {
if (Key.isDown(Key.DOWN))
gravity = -jumpheight;
gotoAndStop("crouch&quot
gravity == 0
else if (!Key.isDown(Key.DOWN))
gotoAndStop("idle&quot
}

Are you having problems with copy pasting? Because you always seem to miss out some vital parts. If you dont use an open brace after after an if statement it will only run the next line as part of the if statement and you wont be able to accompany it with an else. And else needs to follow a closing brace.
What you might be trying to do is this;
onEnterFrame = function() {
if (Key.isDown(Key.DOWN)){
gravity = -jumpheight;
gotoAndStop("crouch&quot
gravity = 0
}else{
gotoAndStop("idle&quot
}
}

You put two equals sign for gravity = 0, and that checks if gravity is equal to 0, it doesnt make gravity equal to 0.
This piece of code makes no sense however, even though there should be no syntax errors here at all. You are setting gravity to equal -jumpheight and then to 0 shortly after, and then you don't have anyway to revert gravity to a normal value.
Showing 1-11 of 11