Action Script. How to disable keyboard? - actionscript-3

how to disable keyboard keys in Action Script?
I'm creating Flash "memory" game, Idea to discover 2 equal cards. When 2nd card is discovered it is shown for 750 milliseconds, in that time player can't do any actions.
When I use this mouseChildren = false; player can't click with mouse for this time, but he can use keyboard arrows/enter/space/tab buttons... I need to disable It for this time.
Here is part of my code:
{
trace("Wrong");
_message = "Wrong";
message_txt.text = _message;
_secondCard = event.currentTarget;
var timer:Timer = new Timer(750, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
timer.start();
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);//added here
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);//added here
mouseChildren = false;
}
}
function blindKeyboard(e:KeyboardEvent):void{ //added here function
e.preventDefault();
e.stopPropagation();
}
protected function flipBack(event:TimerEvent):void
{
_firstCard.gotoAndPlay("flipBack");
_secondCard.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
_secondCard.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = _secondCard = undefined;
mouseChildren = true;
}

You could just have functions for adding/removing listeners :
function addListeners():void
{
// loop through and add the listeners for the cards
// add keyboard listeners
}
function removeListeners():void
{
// loop through and remove listeners from the cards
// remove keyboard listeners
}
Before you set the timer, you remove your listeners :
removeListeners();
Then in your flipback timer handler you just call the addListeners :
addListeners();

Try
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}

Related

change keyboard event to mouse event as3

I am a new new person who learn action script 3.
i have problem when i convert keyboard event to mouse event when i moving a walking character.
when use the keyboard event i have no problem. this is my code
import flash.ui.Keyboard;
var speed:Number=2;
stage.addEventListener(KeyboardEvent.KEY_DOWN, stikman);
function stikman(e:KeyboardEvent)
{
if (e.keyCode==Keyboard.LEFT)
{
stik.x-=speed;
stik.scaleX=-1;
stik.stik2.play();
}
else if (e.keyCode==Keyboard.RIGHT)
{
stik.x+=speed;
stik.scaleX=1;
stik.stik2.play();
}
}
and then i try to change keyboard event to mouse event when moving character with button it should press click, click and click. i want to hold the click when moving the character and when mouse up the character stop. but i still don't know how. this my code when i try to change to mouse event
var speed:Number=2;
mundur.addEventListener(MouseEvent.MOUSE_DOWN, stikman);
function stikman(e:MouseEvent)
{
stik.x-=speed;
stik.scaleX=-1;
stik.stik2.play();
}
maju.addEventListener(MouseEvent.CLICK, stikman2);
function stikman2(e:MouseEvent)
{
stik.x+=speed;
stik.scaleX=1;
stik.stik2.play();
}
Because keyboard produces KeyboardEvent.KEY_DOWN event repeatedly as long as key is pressed, while MouseEvent.CLICK as well as MouseEvent.MOUSE_DOWN are dispatched only once per user action.
With mouse you need to change the logic.
// Subscribe both buttons.
ButtonRight.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
ButtonLeft.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
var currentSpeed:Number = 0;
var isPlaying:Boolean = false;
function onButton(e:MouseEvent):void
{
// Set up the directions and start the animation.
switch (e.currentTarget)
{
case ButtonLeft:
currentSpeed = -speed;
stik.stik2.play();
stik.scaleX = -1;
break;
case ButtonRight:
currentSpeed = speed;
stik.stik2.play();
stik.scaleX = 1;
break;
}
isPlaying = true;
// Call repeatedly to move character.
addEventListener(Event.ENTER_FRAME, onFrame);
// Hook the MOUSE_UP even even if it is outside the button or even stage.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onFrame(e:Even):void
{
// Move character by the designated offset each frame.
stik.x += currentSpeed;
if (!isPlaying)
{
// Stop at last frame.
// if (stik.stik2.currentFrame == stik.stik2.totalFrames)
// Stop at frame 1.
if (stik.stik2.currentFrame == 1)
{
// Stop the animation.
stik.stik2.stop();
// Stop moving.
removeEventListener(Event.ENTER_FRAME, onFrame);
}
}
}
function onUp(e:MouseEvent):void
{
// Indicate to stop when the animation ends.
isPlaying = false;
// Unhook the MOUSE_UP event.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}

MOUSE_MOVE Event not working with Timer

I have a Timer, and when the time expires it goes to another scene. When you move the mouse, Timer gets reset. However, when time expires (and it goes to the other scene) and I go back to the first scene (via clicking the on the stage of the other scene), my timer mouse event no longer works. The time does not get reset, and instead time expires and goes to the other scene again. I put a trace in my MOUSE_MOVE event method so I see what is going on in there, and the time is just not being reset. Can anyone help?
Here is my code:
//Define, Set and Start Timer
var myTimer:Timer;
myTimer = new Timer(10000, 1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
myTimer.start();
//Mouse Move Events, Reset Timer when you move the mouse
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
stage.addEventListener(TouchEvent.TOUCH_BEGIN, mouseMoved);
//Timer Complete Method
function onTimerComplete(event : TimerEvent) : void
{
gotoAndStop(1, "Screensaver");
}
//Time Mouse Move Method, Reset Timer when you move the mouse
function mouseMoved(event:MouseEvent):void
{
myTimer.stop();
myTimer.reset();
myTimer.start();
}
And here is the click event on my screensaver scene:
stage.addEventListener(MouseEvent.CLICK, stopScreensaver);
function stopScreensaver(e:MouseEvent):void
{
screensaver1.visible = false;
screensaver2.visible = false;
screensaver3.visible = false;
screensaver4.visible = false;
screensaver5.visible = false;
screensaver6.visible = false;
screensaver7.visible = false;
screensaver8.visible = false;
touchStart.visible = false;
timer.stop();
timer.reset();
stage.removeEventListener(MouseEvent.CLICK, stopScreensaver);
timer.removeEventListener(TimerEvent.TIMER,timerListener);
stage.removeChild(whiteBackground);
gotoAndStop(1, "Home");
}

How to stop multiple Movieclips at a time by play/pause button in AS3

I have 4 Movieclips in timeline.these movieclips have inner animation. they are classically tween in timeline. I have to stop and play these four movieclips at a time by play/pause button. Noted that, I have used the following code for that purposes.
It can stop only one MovieClip which is in the upper layer, and the rest 3 Movieclips can't be stopped by play/ pause button. (I have tried all movie clips are in same instance name and also tried with different instance).
Here is my code:
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
first_sl.play();
this.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
first_sl.stop();
this.stop();
}
You can try like when you are stopping main timeline with stage.stop(); also use mc.stop(); ect. and when u play them use stage.play(); also mc.play();
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
mc.play();
mc2.play();// other 2 mc's too
stage.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
mc.stop();
mc2.stop(); // other mc's
stage.stop();
}
And if you are using last position for timeline also use it for mc's so it will be sync..
hope this on help tho :)
To stop the main timeline you can use stop() or MovieClip(root).stop() and to play it again, you can use play() or MovieClip(root).play() :
var playing: Boolean = false;
btn_play.addEventListener(MouseEvent.CLICK, onPlayClick);
btn_pause.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void
{
if (!playing) {
play(); // you can also use : MovieClip(root).play();
mc1.play();
mc2.play();
mc3.play();
mc4.play();
playing = true;
}
}
function onPauseClick(event: MouseEvent):void
{
if (playing) {
stop(); // you can also use : MovieClip(root).stop();
mc1.stop();
mc2.stop();
mc3.stop();
mc4.stop();
playing = false;
}
}
Hope that can help.

AS3 Works but I get a ReferenceError: Error #1069 Property startDrag not found

I am trying to make a simple project when you click a button a draggable MovieClip is added to the stag and when you click it releases the MovieClip to the X/Y where you clicked, you can then pickup the MovieClip and drag it into a bin (MovieClip) where it destroys itself. The code is working great I can make multiple Movieclips with the button and they are all destroyed when I drag them in the bin however I don't like having "Error Codes".
import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
function makeRubbish (event:MouseEvent):void {
addChild(rubbish);
rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}
function stopDragging (event:MouseEvent):void {
rubbish.stopDrag()
event.target.addEventListener(MouseEvent.CLICK, startDragging);
rubbish.buttonMode = true;
if (event.target.hitTestObject(bin))
{
trace("hit");
event.target.name = "rubbish";
removeChild(getChildByName("rubbish"));
}
}
function startDragging (event:MouseEvent):void {
event.target.startDrag();
this.addEventListener(MouseEvent.CLICK, stopDragging);
}
Some Pointers:
The target property of an Event is not always what it seems. It actually refers to the current phase in the event bubbling process. Try using the currentTarget property.
I would also recommend tying the stopDragging method to the stage, as sometimes your mouse won't be over the drag as you're clicking.
I would use the MOUSE_UP event as opposed to a CLICK for standard dragging behaviour.
When dragging, keep a global reference to the drag in order to call the stopDrag method on the correct object.
Try This:
import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
var dragging:my_mc;
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
function makeRubbish (event:MouseEvent):void {
addChild(rubbish);
rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
rubbish.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}
function stopDragging (event:MouseEvent):void {
this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
if(dragging !== null){
dragging.stopDrag();
if (event.currentTarget.hitTestObject(bin)){
removeChild(dragging);
}
dragging = null;
}
}
function startDragging (event:MouseEvent):void {
dragging = event.currentTarget as my_mc;
dragging.startDrag();
this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}

Disabling repeating keyboard down event in as3

now I'm trying to make the keyboard events to stop repeating.
My idea was to have a true and false condition for when the key is pressed so that it wont repeat if the key is down already.
//Mouse Event Over
keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function(){gotoAndStop(2)});
//Variable
var Qkey:uint = 81;
//Key Down Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
var soundplayed = false;
function keydown(event:KeyboardEvent){
if (event.keyCode==Qkey) {
this.soundplayed=true;
}
}
if (this.soundplayed==false){
gotoAndPlay(3);
}
//Key Up Event
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
function keyup(event:KeyboardEvent){
this.soundplayed=false;
gotoAndStop(1);
}
doing this makes the key loop over and over without a keyboard event
I think i need to add a "&& keyDown..." to "if (this.soundplayed==true)" but i dont know how to do it without getting errors
here is the keyboard player i'm trying to fix http://soulseekrecords.org/psysci/animation/piano.html
Just another (perhaps a bit more general) way to write what Kishi already suggested:
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
var downKeys:Dictionary = new Dictionary();
function keyDown(e:KeyboardEvent):void {
if(!downKeys[e.keyCode]) {
downKeys[e.keyCode] = true;
processKeyDown(e);
}
}
function keyUp(e:KeyboardEvent):void {
delete downKeys[e.keyCode];
}
function processKeyDown(e:KeyboardEvent):void {
trace(e.keyCode);
}
The processKeyDown function will be called as if keydown repeating was disabled. If you need to do something when the key is up, put that code in the keyUp function, or maybe call a processKeyUp function defined like processKeyDown.
I'm not sure what you are doing on these frames..
Is that the complete code?
Anyway, you should try something like this:
// Mouse Events
this.keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function():void{ gotoAndStop(2) });
// Variables
var Qkey:uint = 81;
var soundplayed = false;
// Keyboard events
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
this.stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
// Event listeners
function keydown(event:KeyboardEvent){
if (event.keyCode == Qkey && !this.soundplayed) {
this.soundplayed = true;
this.gotoAndPlay(3);
}
}
function keyup(event:KeyboardEvent){
this.soundplayed = false;
this.gotoAndStop(1);
}
Notice that the keydown event listener will now execute once -- I mean.. at least the if branch -- as the soundplayed variable is used as a locking mechanism.
It'll only execute again after keyup's been executed (this.soundplayed = false).