简体   繁体   中英

as3 keyboard game control diagonal and multiple keypresses

I've followed on a tutorial for a vertical 2D shooter and am expanding the game with what i've learned.

Keyboard control was ready supplied, and lacks some explanation so i am having a few issues with it:

SOLVED (see second answer ) 1-control of my ship goes with the ARROW-Keys and SPACE was used to fire bullets(limited to X/fps)

Controlling the ship up/down/left/right and diagonal works fine EXCEPT when simultaniously the SPACE is being pressed(or held), when SPACE is pressed the ship can ONLY move up/down/left/right and diagonal-up/right

This issue is worked around by changing SPACE for eg SHIFT or CONTROL which allow diagonal movements in all directions when the fire-key is pressed

I would really like to know why this is happening and if this can be solved using SPACE for regular fire

SOLVED see edit at end of this post 2-I have added an upgrade ingame which adds missile to the ship, and since i would have a different fire rate as the regular bullets i would like to use a different key for shooting the missiles.

Adding the missiles with a different key worked (as in the example codes below) except i cannot fire the Bullets(currently SHIFT) and the Missiles(currently CONTROL) simultaniously

Is there any way i could keep different keys pressed to fire both?

3-Can i change the constant names of the key into keycode in the example code?

Sorry for the bad english

My key class AS3:

import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;

public class Key {

    private static var initialized:Boolean = false;
    private static var keysDown:Object = new Object();  // stores key codes of all keys pressed

    public static function initialize(stage:Stage) {
        if (!initialized) {
            // assign listeners for key presses and deactivation of the player
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
            stage.addEventListener(Event.DEACTIVATE, clearKeys);

            // mark initialization as true so redundant
            // calls do not reassign the event handlers
            initialized = true;
        }
    }


    public static function isDown(keyCode:uint):Boolean 
    {
        return Boolean(keyCode in keysDown);
    }


    private static function keyPressed(event:KeyboardEvent):void {
        keysDown[event.keyCode] = true;
    }


    private static function keyReleased(event:KeyboardEvent):void {
        if (event.keyCode in keysDown) {
            delete keysDown[event.keyCode];
        }
    }


    private static function clearKeys(event:Event):void {
        // clear all keys in keysDown since the player cannot detect keys being pressed or released when not focused
        keysDown = new Object();
    }
}

}

The portion of code in my ship class that handles movement and shooting:

    function move(e:Event) {


        if (Key.isDown(Keyboard.RIGHT)) {
            this.x = this.x + velocity;
        }
        if (Key.isDown(Keyboard.LEFT)) {
            this.x = this.x - velocity;
        }
        if (Key.isDown(Keyboard.UP)) {
            this.y = this.y - velocity;
        }
        if (Key.isDown(Keyboard.DOWN)) {
            this.y = this.y + velocity;
        }
        if (Key.isDown(Keyboard.CONTROL)&& shootLimiter > 50 ) {
            {
                            shootLimiter = 0;
            var b = new Missile();
            stage.addChild(b);
            b.x = this.x;
            b.y = this.y -28;
            }

        }
        if (Key.isDown(Keyboard.SHIFT)&& shootLimiter > 20 ) {
            shootLimiter = 0;               
            var a = new Bullet();
            stage.addChild(a);
            a.x = this.x;
            a.y = this.y -28;
        }

    }

EDIT: Sjeesh i feel stupid now, after days of tinkering and posting my question here, i found the solution to the biggest problem (No.2) myself:

It seems i overlooked 1 line of code in the ENTERFRAME event that adds to the shootlimiter variable +1

Trying this shootlimiter variable at different values in the MOVE event for the Bullet and Missile caused only 1 of them to work a the time

I basically copied the whole logic of the shootlimiter variable and added it as missilelimiter which now is used for the missile

Simultainiously pressing CONTROL and SHIFT it now is possible to shoot both, but i've also changed that for both Bullets and Missiles to shoot together on pressing SHIFT, but at different rates

The issue about SPACE and limited diagonal movement when its pressed is of minor concern, also for using keycodes instead of the constant names i have found some good info, but i will leave the two questions open, although the answers don't seem so important anymore

You should rewrite your key events processing code. When you get key event you want then just set some flag (variable). For example isUp = true. After you will get all key events for frame you can process all of these flags in single place. Ie player pressed Up and Left you set isUp=true and isLeft=true. And further on enter frame you can move your ship up and left (diagonal movement).

Although it could be a little bit tidier, as @shubniggurath suggested, I don't see anything in your code that immediately jumps out at me as the culprit for this behavior.

What brand/make/model of keyboard are you using? This could simply be keyboard ghosting, in which case, there really is nothing you can do programmatically to fix it.

http://www.microsoft.com/appliedsciences/antighostingexplained.mspx

https://gaming.stackexchange.com/questions/6669/how-do-i-remove-the-limit-on-pc-keyboard-button-presses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM