PDA

View Full Version : Change to "A" or "S"



RyxiaN
January 21st, 2005, 06:45 PM
I want to change SPACE to A or S:

if (Key.isDown(Key.SPACE)){
i would like to make just like

if (Key.isDown(key_jump)){
or something... like a short cut
how?

petefs
January 21st, 2005, 06:56 PM
There's a list of ASCII code values in the flash docs somewhere, so look up the code for the key you want. Then do this:


var key_jump = 114; // replace with the proper code for what you want

then wherever you're checking it use what you have there:


if (Key.isDown(key_jump)) {


Just to clarify how Key.isDown() works:
Key.isDown() expects a number as argument. There are predifined constants in the Key class that reffer to the corresponding key code of commonly used keys, such as Key.SPACE, Key.ENTER, Key.LEFT etc... Any valid keycode is acceptable as an argument, however ^^

RyxiaN
January 21st, 2005, 07:00 PM
is A = 65?

petefs
January 21st, 2005, 07:05 PM
I don't have them memorized quite yet ^_^ Try it and see?

You can always do


var keyListener = new Object();
keyListener.onKeyUp = function () { trace(Key.getCode()); }
Key.addListener(keyListener);

and press A : )

senocular
January 21st, 2005, 08:15 PM
or more easily

Key.isDown(("A").charCodeAt(0))
;)

petefs
January 21st, 2005, 08:40 PM
ugh, that's why I like you senocular : ) I'm sure the character code lookup overhead is negligible ; )

I stay up at nights thinking of things like that, but lately I've been trying to actually get some sleep ; )

RyxiaN
January 22nd, 2005, 06:12 AM
Okay, I fixed it... but how do I make so he shots something

Ithaca
January 22nd, 2005, 05:33 PM
Ok, tell me if I did this wrong. I found out all the numbers for letters A, S, D, and W and then put them in for their respected positions in the UP, DOWN, LEFT, and Right positions in the code. But when I tried to move my character it didn't work. Heres my code. If this makes a difference I already have another character on the stage with the RIGHT etc. buttons for his movement.onClipEvent (load) {
vel_y = 0;
started = true;
jumping = false;
xspeed = 5;
this.stop();
_quality = "high";
}

//movement
onClipEvent (enterFrame) {
if (Key.isDown(Key.65)) {
this._x -= xspeed;
}
if (Key.isDown(Key.68)) {
this._x += xspeed;
}
if (started) {
if (move) {
if (Key.isDown(Key.83)) {
this.gotoAndStop("fire");
xspeed = 0;
}
if (Key.isDown(Key.65)) {
this.gotoAndStop("run");
this._xscale = -100;
}
if (Key.isDown(Key.68)) {
this.gotoAndStop("run");
this._xscale = 100;
}
}
}
}

//idle animations
onClipEvent (keyUp) {
if (move) {
if (Key.getCode() == Key.68) {
this.gotoAndStop("idle");
}
if (Key.getCode() == Key.65) {
this.gotoAndStop("idle");
}
if (Key.getCode() == Key.83) {
this.gotoAndStop("idle");
}
}
}

//jumping
onClipEvent (enterFrame) {
if (Key.isDown(Key.87) && !jumping) {
this.gotoAndStop("jump");
move = false;
vel_y = 17;
jumping = true;
}
if (jumping == true) {
vel_y -= 1;
if (vel_y<=-17) {
vel_y = -17;
}
this._y -= vel_y;
}
if (!_root.ground.hitTest(this._x, this._y+15, true) && !jumping) {
fall += 1;
move = false;
this.gotoAndStop("jump");
if (fall>17) {
fall = 17;
}
this._y += fall;
}
if (_root.ground.hitTest(this._x, this._y+15, true)) {
if (vel_y<=1) {
fall = 0;
vel_y = 0;
jumping = false;
this.jump.gotoAndStop(2);
move = true;
}
}
if (_root.goomba.hitTest(this.jump)) {
if (vel_y<=1) {
_root.goomba.gotoAndPlay("dead");
_root.goomba.xspeed = 0;
vel_y = 10;
_root.counter.play();
}
}
}

//questionable scrolling effect
onClipEvent (enterFrame) {
if (Key.isDown(Key.65)) {
scroll = 5;
xspeed = 0;
this._x -= 5;
_root._x += scroll;
}
if (Key.isDown(Key.68)) {
scroll = 5; //setting his scrolling speed//
xspeed = 0; //ceasing his movement//
this._x += 5;
_root._x -= scroll;
}
}

//level barriers
onClipEvent (enterFrame) {
//level barrier (left)
if (_root._x > 0 && _root._x <600) {
_root._x = 0;
}


if (this._x<20) {
this._x = 20;
}
// level barrier (top)
if (this._y<0) {
this._y = 0;
}
// level barrier (bottom);
if (this._y>500) {
this._y = 500;
}
// level barrier (right)
if (this._x>1244) {
this._x = 1244;
}
//grond text xMax
if (_root.ground.hitTest(getBounds(_root).xMax, _y, true)) {
_x -= xspeed;
}
//ground test xMin
if (_root.ground.hitTest(getBounds(_root).xMin, _y, true)) {
_x += xspeed;
}
}

And heres the fla. in MX 2004. If anyone wants the Mx version just tell me.
Thanks! :)

JoMan
January 22nd, 2005, 05:38 PM
Mama-mia :mario:! Dats-a-lotta code-a!

peace-a

Ithaca
January 22nd, 2005, 06:10 PM
Oh don't be intimidated by the length of the code.:yoshi: Its really not that much. If you can just tell me how I can set the letters A, S, D and W to move my character I can fix the rest.