(AS3) Changing quality with keyboard

8 posts

Flag Post
I wanted to make a code to change the swf quality when you pressed the letter Q. After seeing tutorials for keyboard events and changing quality I tried combining the two, but my code just doesn't want to work: --------------------------------------------------------------------------
package {

	import flash.display.*;
	import flash.events.*;
	import flash.ui.Keyboard;

	public class MainDocument extends MovieClip {

		public function MainDocument() {
			addEventListener(Event.ENTER_FRAME,EveryFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,MainKeyboard);
		}

		public function EveryFrame(e:Event) {
		}

		public function MainKeyboard(k:KeyboardEvent) {
			var MyKey = k.keyCode;//MyKey = the pressed key
			if (MyKey == 81) {//81 is the number for Q, I think
				if (stage.quality == StageQuality.HIGH) {
					stage.quality = StageQuality.LOW;
				}
				if (stage.quality == StageQuality.LOW) {
					stage.quality = StageQuality.MEDIUM;
				}
				if (stage.quality == StageQuality.MEDIUM) {
					stage.quality = StageQuality.HIGH;
				}
			}
		}

	}
}
-------------------------------------------------------------------------- Flash says there are no errors but it still doesn't work... any ideas?
 
Flag Post
package {

import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;

public class MainDocument extends MovieClip {

public function MainDocument() {
addEventListener(Event.ENTER_FRAME,EveryFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,MainKeyboard);
}
public function EveryFrame(e:Event) {
}
public function MainKeyboard(k:KeyboardEvent) {
var MyKey = k.keyCode;//MyKey = the pressed key
if (MyKey == 81) {//81 is the number for Q, I think
if (stage.quality == StageQuality.HIGH) {
stage.quality = StageQuality.LOW;
}
if (stage.quality == StageQuality.LOW) {
stage.quality = StageQuality.MEDIUM;
}
if (stage.quality == StageQuality.MEDIUM) {
stage.quality = StageQuality.HIGH;
}
}
}
}
}
 
Flag Post

there’s an edit button, and you need to read the stickies. Proper Forum Code Formatting Guide

 
Flag Post

For your issue, there are a few things I can point out:

1: You don’t need an Event.ENTER_FRAME event listener, as you aren’t even using it.
2: You seem to be trying to add an event listener (KeyboardEvent.KEY_DOWN) to the stage, at a point where the stage may not actually be accessible.
3: You are not type-casting your “MyKey” variable. It is good practice to do so (var MyKey:int = k.keyCode).
4: Although it is personal preference, naming all your functions and variables like “myKey” and “mainKeyboard” instead of “MyKey” and “MainKeyboard” usually looks cleaner and more readable (But is up to you).
5: Ensure you specify the type of object (or value) a function returns, try not to leave it blank. If your function does not return anything, use :void.

However your biggest problem is you have what I like to call a “if if if” problem. Lets run through your MainKeyboard function as if StageQuality is equal to StageQuality.HIGH (What it defaults to).

if(stage.quality==StageQuality.HIGH)

Well, this will be true, so we will now set stage.quality to LOW

if(stage.qualityStageQuality.LOW)
Well, this is going to be true now as well, so set the stage.quality to MEDIUM
if(stage.qualityStageQuality.MEDIUM)

Woah, this will be true too! So we now set the quality to HIGH.

But, we are now back to where we started! Oops. To solve this problem, we can just add some simple “else if” statements.
View the full fix (Including my above suggestions) below: *Note, I changed some things. Notably I dont use the StageQuality class, as it doesn’t seem to work for me.

public class Main extends MovieClip {
	
	public function Main() {
		if (stage) init();
		else addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
	}
	
	private function init(e:Event=null):void 
	{
		removeEventListener(Event.ADDED_TO_STAGE, init);
		stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown,false,0,true);
	}
	
	public function keyDown(k:KeyboardEvent):void {
		var myKey:int = k.keyCode;//MyKey = the pressed key
		if (myKey == Keyboard.Q) {//81 is the number for Q, I think
			if (stage.quality == "LOW") {
				stage.quality = "MEDIUM";
			} else if (stage.quality == "MEDIUM") {
				stage.quality = "HIGH"
			} else if (stage.quality == "HIGH") {
				stage.quality = "LOW";
			}
			trace("New quality: " + stage.quality);
		}
	}
}

If you have any questions, feel free to PM me or post here again.

 
Flag Post

I managed to get it work with the key space, but it still refuses to work with Q… I’ve tried using keyboard.Q and 81, maybe I have to import something… thanks for the help btw

 
Flag Post

add

trace(k.keyCode);

to your keyDown function. I don’t even look up keyCodes for anything anymore, I simply add that bit of code, run the game, and press the button I want to get the code for. Also certain keys won’t work if you are using Adobe Flash, unless you do the following.

1. test the movie

2. with the movie open, locate the top buttons.

3. click on the one that is called “Control”

4. then make sure that “Disable Keyboard Shortcuts” is checked.

5. you will need to check this every time you reopen adobe flash if you wish to access certain keys. I have no idea if “Q” is one of these.

Although if you compile your game using a different program, I can only speculate there is a similar situation happening and a similar toggle for shortcuts being disabled. (knowing how awesome flash develop is, it probably does it by default, or at least lets you perma-toggle it).

 
Flag Post

FD will accept keyboard events on your main display tree by default, at least in the external player. I always use that because I couldn’t get the tab-inside-FD version to give me trace output.

 
Flag Post

disabling the shortcuts solved the problem!!!! anyway, here’s the code I used, if it could be shorter tell me


package {

	import flash.display.*;
	import flash.events.*;
	import flash.ui.Keyboard;

	public class MainDocument extends MovieClip {

		public function MainDocument() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}

		public function init(e:Event) {
			stage.addEventListener(KeyboardEvent.KEY_UP, KeyboardUP);
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}

		public function KeyboardUP(e:KeyboardEvent) {
			if (e.keyCode == 81) {
				switch (stage.quality) {
					case "HIGH" :
						stage.quality = "LOW";
						break;
					case "LOW" :
						stage.quality = "MEDIUM";
						break;
					case "MEDIUM" :
						stage.quality = "HIGH";
						break;
				}
			}
		}

	}

}