Reputation: 33
For flash actionscript 3 running in cs6 I am having an issue in which a function will only run after a mouse click event, I don't know why this is I've tried everything I can come up with, and there doesn't appear to be any similar issues online.
This is the basic template for the program I'm creating this is in frame one. It's designed so that when you click on one of the possible buttons it will check if that was the correct button or not and take you to the next frame
stop();
var buttons:Array=new Array(oport); //list of buttons
var correct:String="oport"; //button name
var gotoFrame:int=2;
var win:String=null;
const NEXT_FRAME:int=0;
var removeOtherListeners:Function=function():void{};
addListeners();
function addListeners():void {
for (var i:int=0; i<buttons.length; i++) {
buttons[i].addEventListener(MouseEvent.CLICK, checkButton);
}
}
function removeListeners():void {
for (var i:int=0; i<buttons.length; i++) {
buttons[i].removeEventListener(MouseEvent.CLICK, checkButton);
}
}
//checks button onclick and applies the functions above
function checkButton(e:MouseEvent):void {
removeListeners();
removeOtherListeners();
removeOtherListeners=function():void{};
Mouse.show();
if (e.currentTarget.name==win) {
gotoAndStop("win");
} else if (e.currentTarget.name==correct) {
if (gotoFrame<=0) {
nextFrame();
} else {
gotoAndStop(gotoFrame);
}
} else {
gotoAndStop("lose");
}
}
This is on the second frame this code is designed for cs4 but it should still work. This is the frame presenting the issue, when I enter this from from frame one, I must click on the screen before I can start typing. If I type before clicking on the screen nothing will happen.
var lDown:Boolean = false;
var sDown:Boolean = false;
var dDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);
function onKeyBoardDown(e:KeyboardEvent):void
{
if (e.keyCode == KeyCodes.L)
{
lDown = true;
}
if (lDown == true)
{
if (e.keyCode == KeyCodes.S)
{
sDown = true;
}
}
if (sDown == true)
{
if (e.keyCode == KeyCodes.D)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyBoardDown);
gotoAndStop(5);
}
}
}
This is the class referred to in KeyCodes
package {
public class KeyCodes {
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
public static const D:uint = 68;
public static const E:uint = 69;
public static const F:uint = 70;
public static const G:uint = 71;
public static const H:uint = 72;
public static const I:uint = 73;
public static const J:uint = 74;
public static const K:uint = 75;
public static const L:uint = 76;
public static const M:uint = 77;
public static const N:uint = 78;
public static const O:uint = 79;
public static const P:uint = 80;
public static const Q:uint = 81;
public static const R:uint = 82;
public static const S:uint = 83;
public static const T:uint = 84;
public static const U:uint = 85;
public static const V:uint = 86;
public static const W:uint = 87;
public static const X:uint = 88;
public static const Y:uint = 89;
public static const Z:uint = 90;
public function KeyCodes() {
// constructor code
}
}
}
Thank you for any answers or advice.
Upvotes: 1
Views: 797
Reputation: 54
Most likely, it is a focus issue caused by the browser. As a security precaution the browser won't give Flash focus without a click. The solution is to use JavaScript to give the SWF focus on page load.
See Adobe's solution > http://helpx.adobe.com/flash/kb/give-keyboard-focus-embedded-movie.html
And I recommend that you try putting all of your code into a Document Class. This will eliminate any bugs that could occur from separating code into frame scripts. As well, making your code re-usable.
Upvotes: 1