Movement using keyboard keys tute

Hi,

First, this site and the tutorials are great, thanks.

I am building a site for school in which i am using keyboard keys to control a character on screen. The character is only viewed from the side, and only moves left and right. In the tutorial, the bug changes direction using _root.bug._rotation = ###; Because it is a side view, the rotation does not suit my needs. (I need the character to flip horizontally when changing directions, not rotate, so it stays upright…)
Anyone able to suggest anything that might help me out? I am a bit of a flash noob, so forgive me if this is an ignorant question.
Thanks!

Make the bug 2 keyframes.

Keyframe 1 having him facing one direction, and keyframe 2 having him face another. Then when the arrow button is pressed, add this code…

_root.bug.gotoAndStop(2);

and for the other way…

_root.bug.gotoAndStop(1);

Hi, thanks for your reply lostinbeta.

I made the changes, but now when i test the movie, the character flips back and forth; the movieclip keeps looping. I tried adding a stop, but then the character still will only face one way.
How can i fix it? Thanks a lot.

Put a stop on both frames.

That way it will stop on frame one, which will be the default original position.

And you will of course need a stop action on frame 2 as well.

So just put a stop(); action on both frames and it should work.

Hi
i tried adding the other stop, but now it is stuck facing one direction only. I just want to make sure I have done things correctly, so here is what i’ve done:
On the main timeline, i have 1 keyframe, with the button in it. the action for the button is:

on (keyPress “<Left>”) {
currentX = this._x;
this._x = currentX - 4;
_root.bug.gotoAndStop(1);

}
on (keyPress “<Right>”) {
currentX = this._x;
this._x = currentX + 4;
_root.bug.gotoAndStop(2);

}
Inside the button is the movie clip of the character, it has 2 keyframes, one for each direction, and each frame has a stop action on it.
What am i doing wrong? Thanks a lot for helping me, i really appreciate it.

o ya and the <Left> and <Right> are in there too…I forgot to put them in in my previous post.

oic…thats left and right in brackets…

Assuming you are putting this code on a Movie Clip symbol and that it starts off racing the right (facing right in frame 1 and left in frame 2)… this should work (untested)…

on (keyPress "<Right>") {
	this.onEnterFrame = function() {
		this._x += 5;
		_root.bug.gotoAndStop(1);
		if (this._x>=100) {
			this._x = 100;
		}
		delete this.onEnterFrame;
	};
}
on (keyPress "<Left>") {
	this.onEnterFrame = function() {
		this._x -= 5;
		_root.bug.gotoAndStop(2);
		if (this._x<=10) {
			this._x = 10;
		}
		delete this.onEnterFrame;
	};
}

The if statements are in there to constrain it to an area. In this case it can’t go past 100px on the right or 10px on the left. It will just stop instead.

[edit] About time I got that thing to show right [/edit]

It still is stuck facing one direction only. Sorry to keep buggin…

You have a stop action on both frame 1 and frame 2 of your bug movie clip?

Frame 1 has a stop() action and the bug facing your right.

Frame 2 has a stop() action and the bug facing your left.

I just tested my code and it worked fine for me.

Actually, I can even edit that code just a tad and replace _root.bug with “this” Since you will put this code right on the bug movie clip…

on (keyPress "<Right>") {
    this.onEnterFrame = function() {
        this._x += 5;
        this.gotoAndStop(1);
        if (this._x>=100) {
            this._x = 100;
        }
        delete this.onEnterFrame;
    };
}
on (keyPress "<Left>") {
    this.onEnterFrame = function() {
        this._x -= 5;
        this.gotoAndStop(2);
        if (this._x<=10) {
            this._x = 10;
        }
        delete this.onEnterFrame;
    };
}

I posted a file for you to look at.

It works great for me and I used the code I gave you.

Ok, ya i got it to work! Thanks so much for your help, much appreciated.
I liked your site, great photoshop work.
:slight_smile:

I am glad you got it to work :slight_smile:

Thanks for the compliments on my site and my Photoshop work =)

Ok, I have another somewhat related question, so I thought I’d just add to this post.

Is it possible to use onKeyPress to make button objects work?

For instance, with the littleman .fla, if the character was moved over another button symbol, pressing the space bar on it would play another Scene. (an action placed on the button…)

I’ve been reading a lot, but I haven’t been able to figure out how this could be done. Any Ideas?

Thanks a lot

No clue, but I have seen it done at www.tweened.com.

My guess is something like…


if (this._x >= 50 && this._x<=100){
  //keypresscode;
}

This code was pulled out of the air. Totally untested, but it should work like… if theobject is between 50 and 100, and you press this key, it should do this…

Not possitive… in theory I suppose it should work :-\

Ok, that makes sense, thanks!

I guess making the onkeypress interact with buttons in order to change scene is not really necessary. I tried adding a gotoAndPlay (“Scene”, 1) after the ‘if’ condition, but that didn’t seem to work for me. (surprise, surprise, hehe.) Anyways, thanks for the quick reply. I’m going to plug away at it a bit more, maybe I’ll find the answer trial and error stylez. If you have any suggestions, that would be sweet.

O ya that tweened.com site was cool. This project is trying to emulate www.kerb.co.uk
(…in a simplified “drawn super crappy, doesn’t even compare at all” kinda way…)

thanks lots :slight_smile:

WOW, THAT SITE IS AWESOME!!!

The cat totally rules.

BTW: Never get down on yourself :slight_smile: Your skills are always more than what you think.

I don’t have any suggestions right now. There was a guy named flex that used to post in this forum, but he dropped off the face of the earth :-\ He was excellent at game coding and things like this.

You can see his stuff at http://flex.funkyolive.com