send keyCode without key event? - actionscript-3

I got this function (in the document class)
public function kyz(event:KeyboardEvent):void{
trace(event.keyCode);
switch (event.keyCode){
case 65:{
if (ppm.currentFrame<200 || ppm.currentFrame>300) {
ppm.gotoAndStop(301);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
pd.play();
}else {
pd.play();
ppm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 68:{
if (ssm.currentFrame<200 || ssm.currentFrame>300) {
ssm.gotoAndStop(301);
ppm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
sd.play();
}else {
sd.play();
ssm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 74:{
if (llm.currentFrame<200 || llm.currentFrame>300) {
llm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
ld.play();
}else {
ld.play();
llm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 76:{
if (mmm.currentFrame<200 || mmm.currentFrame>300) {
mmm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
md.play();
}else {
md.play();
mmm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
}
}
that receives key event's and do stuff. now I'm trying to pass they keyCode from another function (to avoid changing the whole code for adding click functionality) got any suggestions?

you could dispatch a KeyboardEvent
stage.dispatchEvent(
new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, myCharCode, myKeyCode));
just add the needed keyCode as a parameter

You cannot send any Keyboard or Mouse event without interactivity with user. You can handle this in another private function:
private function keyDownHandler(event : KeyboardEvent) : void {
this.handleEvent(event.keyCode);
}
private function handleEvent(keyCode : uint) : void {
//some actions
}
And when you need to make specific actions, you can just call handleEvent function without user side interactivity.

Related

How to change the behavior of the beforeAction?

There is a beforeAction() in Controller.php
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
}
return true;
}
return false;
}
It throws an exception but I want to change this in my own controller which extends controller.php. I try something like that
public function beforeAction($action) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
Yii::$app->session->setFlash('info', 'Error');
$this->goBack();
}
return parent::beforeAction($action);
}
But it still shows exception.
Not sure but it might work with just changing this row...
$this->goBack();
into...
return $this->goBack();
Another approach would be to catch the Exception from the parent instead. Later there might be other events triggered by beforeAction and if not calling the parent::beforeAction these may not be run as intended.

How to stop Touch or Click Event while Swipe Event is in action in as3

In my code i use both swipe gesture and click event at the same time. How to avoid click event or touch event while swipe gesture is in action?
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler);
wall.tile[0].addEventListener(MouseEvent.CLICK, showBook());
wall.tile[1].addEventListener(MouseEvent.CLICK, showBook());
public function fl_SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetY)
{
// swiped down
case 1:
{
if (swipe!=0){
Tweener.addTween(wall, {y: wall.y + 650, time:.5, transition:"easeOutSine" } );
swipe--;
}
// End your custom code
break;
}
// swiped up
case -1:
{
if (swipe<=(total/5)){
Tweener.addTween(wall, { y: wall.y - 650, time:.5, transition:"easeOutSine" } );
swipe++;
}
// End your custom code
break;
}
}
}
I usually have some boolean variable set to true when swipe is being activated, and onComplete function of tweener sets it back to false. And that boolean is one condition in my onClick functions (as false obviously):
public var swipeOn:Boolean;
public function fl_SwipeHandler(event:TransformGestureEvent):void{
switch(event.offsetY){
case 1:{
if (swipe!=0){
swipeOn = true;
Tweener.addTween(wall, {y: wall.y + 650, time:.5, transition:"easeOutSine", onComplete:doneSwipe } );
swipe--;
}
break;
}
case -1:{
if(swipe<=(total/5)){
swipeOn = true;
Tweener.addTween(wall, { y: wall.y - 650, time:.5, transition:"easeOutSine", onComplete:doneSwipe } );
swipe++;
}
break;
}
}
}
public function doneSwipe():void{
swipeOn = false;
}
public function showBook(Event:MouseEvent):void{
if(!swipeOn){
//....handle your clicks etc
}
}

How to change the mode dynamically in codemirror on encounter of a particular expression?

In the textarea whenever '<' is encountered the mode should be html and for '<#' or '<#' or '$', the mode should be ftl. In the code that I have written
function determineCodeMirrorType(cm) {
if (cm.getOption('mode') == 'text/ftl') {
checkAndSwitchToHTML(cm, cm.getValue());
} else if (cm.getOption('mode') == 'text/html') {
checkAndSwitchToFTL(cm, cm.getValue());
}
}
function checkAndSwitchToHTML(cm, val) {
if (/^\s*</.test(val)) {
cm.setOption("mode", "text/html");
}
}
function checkAndSwitchToFTL(cm, val) {
if (/[<#|<#|$]/.test(val)) {
cm.setOption("mode", "text/ftl");
}
}
function buildCMInstance(mode, value) {
var cm = CodeMirror.fromTextArea(document.getElementById("code"), {
mode:mode,
value:value,
lineNumbers:true,
onChange:function(cmInstance){
determineCodeMirrorType(cmInstance); //The call to this function is not made.
})
});
return cm;
}
$(document).ready(function(){
var cm = buildCMInstance("text/ftl")
});
I want to know is there any option that can be initiated which allows the code to change dynamically by making a call to the function "determineCodeMirrorType".
onChange does not exist in recent CodeMirror versions. You have to register the event handler by calling cm.on("change", function(cm, change) { .... }).

For each loop not looping through everything

I have a loop that runs in an onEnter function (bad i know). I only want it to run once but it doesnt loop through every object. I think it doesnt have time before the next frame runs. Any ideas on how to make it loop all the way?
private function onEnter(e: Event) {
if (deleteThis == true) {
if (timer == 0 || 1) {
if (checkExplosion == false) {
gotoAndStop(2)
if (Main.bloonList.length > 0) {
for each(Main.bloon in Main.bloonList) {
if (Main.areaOfCollision(Main.bloon, this, 0)) {
if (Main.bloon.currentFrame != 5) {
Main.money++;
Main.bloon.nextFrame();
Main.bloon.gotShot();
} else {
Main.money++;
Main.bloon.deleteBloon();
}
}
}
}
checkExplosion=true
}
}
}
}
Edit - deleteBloon();
public function deleteBloon()
{
this.parent.removeChild(this);
}
Firstly, this:
if (timer == 0 || 1)
Should be this:
if (timer == 0 || timer == 1)
Then you may need to change the code a little bit. Something like this could work. I do suspect your issue may actually lie in the deleteBloon function, though. Without seeing it I can't be sure, but there is nothing (technically) wrong with your for loop.
private function onEnter(e: Event) {
if (deleteThis == true) {
if (timer == 0 || timer == 1) {
if (checkExplosion == false) {
gotoAndStop(2);
if (Main.bloonList.length > 0) {
// This isn't optimised as I wasn't sure what Main.bloon could be
for each(var bloon:Main.bloon in Main.bloonList) {
if (Main.areaOfCollision(bloon, this, 0)) {
if (bloon.parent && bloon.currentFrame != 5) {
bloon.nextFrame();
bloon.gotShot();
} /*else {
// Make sure that deleteBloon is deleteing the correct array index. This is likely where the issue is.
bloon.deleteBloon();
}*/
Main.money++;
}
}
}
checkExplosion = true;
}
}
}
}
This would be in bloon:
import flash.utils.setTimeout;
function gotShot():void{
// second parameter is time in milliseconds before executing
setTimeout(deleteBloon, 250);
}

Check Mouse Status

I am trying to check when the right button of the mouse is up. I have tried to create an EventListener for RIGHT_MOUSE_UP, but it returns an error (1119: Access of possibly undefined property RIGHT_MOUSE_UP through a reference with static type Class.), while MOUSE_UP doesn't work. How I can do this?
EDIT:
public function mouseOverHandler( evt:MouseEvent )
{
if( evt.buttonDown == true )
{
this.addEventListener( MouseEvent.MOUSE_UP, onMouseRelease);
curState_ = 2;
if( animated_ == true )
{
// Stuff
}
else
{
// Stuff
}
}
else
{
// Stuff
}
dispatchEvent(new CustomButtonEvent(CustomButtonEvent.OVER));
}
check this tip http://www.leebrimelow.com/?p=3253
he said you need FP 11.2 beta for using 'MouseEvent.RIGHT_CLICK' event.