PDA

View Full Version : [AS1] Character control trouble


Baggers
04-23-2009, 09:08 PM
Hello all. I am having real trouble nailing the code for my charcter control script in Flash. I've searched everywhere but can't find any solutions. I hope you can help me.

I have a main sprite/Movie Clip (with movie clips inside) that I'm controlling with the arrow keys. Left triggers a walk backward animation, and Right is a forward animation. A taunt action is triggered with the S key also.

Everything works great but the problem I have is that the walk animation, once triggered, loops indefinitely. I want it to stop when I let go of the arrow keys and return to my normal stance animation. How do I go about doing this?

Here's my Code:

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this.gotoAndStop("walk_f");
this._x += 7;
}
if(Key.isDown(Key.LEFT)){
this.gotoAndStop("walk_b");
this._x -= 7;
}
if(Key.isDown(83)){
this.gotoAndStop(4);
}
}

Thanks.

PhatKitty
04-23-2009, 10:02 PM
To me AS1 is a dead language, I only ever see it on post 5 years old, but going by the fact your code is the same as the AS2 code then just put this at the end:

else {
this.gotoAndStop(standing still frame number/label)
}


And I also recommend putting all script on the main timeline, this will make future things like making killable enemy's a tonne easier:

charecter instance name.onEnterFrame = function() {
if(Key.isDown(Key.RIGHT)){
this.gotoAndStop("walk_f");
this._x += 7;
}
if(Key.isDown(Key.LEFT)){
this.gotoAndStop("walk_b");
this._x -= 7;
}
if(Key.isDown(83)){
this.gotoAndStop(4);
} else {
this.gotoAndStop(standing still frame number/label)
}
}

Baggers
04-23-2009, 10:32 PM
Thanks for the reply. Yeah, I don't know what the deal is with AS1. I certainly didn't type it (not that I remember), but it is AS2 I'm on about.

I added the end bit that you suggested and it nearly works. :) The only problem now is that the walk animations freeze on frame 1 when I press the arrow keys. But it does go back to the stance when nothing's pressed, so that's good.

Any ideas on why the animations now freeze?

Baggers
04-24-2009, 01:30 AM
Thanks for your help, but I have now solved the problem. My character now returns to normal animation when no keys are pressed, and my taunt and fireball animations play fully without holding down a key.

I also managed to stop the fireball/taunt animation cancelling if you press another key during playback.

Here's my code (I know you said put it in the timeline, and I will do later on:)):

onClipEvent (load) {
_root.taunt = false;
_root.hadoken = false;
speed = 7;
}

onClipEvent (enterFrame){
if (!Key.isDown(Key.LEFT) and !Key.isDown(Key.RIGHT) and _root.taunt == false and _root.hadoken == false ){
this.gotoAndStop("stance");
down = false;}

if (Key.isDown(83)and _root.hadoken == false) {
_root.taunt = true;
this.gotoAndStop("taunt");}

if (Key.isDown(65)and _root.taunt == false) {
_root.hadoken = true;
this.gotoAndStop("hadoken");}

if (Key.isDown(Key.RIGHT) and _root.taunt == false and _root.hadoken == false) {
this.gotoAndStop("walk_f");
this._x += speed;}

else if (Key.isDown(Key.LEFT) and _root.taunt == false and _root.hadoken == false) {
this.gotoAndStop("walk_b");
this._x -= speed;}
}

An explanation

onClipEvent (load) {
_root.taunt = false;
_root.hadoken = false;
speed = 7;
}
^This sets up my hadoken and taunt actions to play once (without holding key) and stop. Add _root.taunt = false to last frame of taunt anim as well. Same for hadoken anim. Also my movement speed variable.


onClipEvent (enterFrame){
if (!Key.isDown(Key.LEFT) and !Key.isDown(Key.RIGHT) and _root.taunt == false and _root.hadoken == false ){
this.gotoAndStop("stance");
down = false;}
^This tells my clip to play normal animation when no keys are pressed.


if (Key.isDown(83)and _root.hadoken == false) {
_root.taunt = true;
this.gotoAndStop("taunt");}

if (Key.isDown(65)and _root.taunt == false) {
_root.hadoken = true;
this.gotoAndStop("hadoken");}
^Fireball and taunt commands (you can add your own such as punch, kick, etc). 83 is the S Key and 65 is the A key.


if (Key.isDown(Key.RIGHT) and _root.taunt == false and _root.hadoken == false) {
this.gotoAndStop("walk_f");
this._x += speed;}

else if (Key.isDown(Key.LEFT) and _root.taunt == false and _root.hadoken == false) {
this.gotoAndStop("walk_b");
this._x -= speed;}
}
^This is my character movement script.

Thanks.