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).
Related
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);
}
I have a dropdown menu that lets you select an item to be placed on the stage. The item is drag and droppable so I use event.currentTarget.startDrag(); to start the drag. Ok, everything works fine so far.
However, I also need to be able to rotate the item while it is being dragged (using the spacebar).
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.SPACE){
rotate++;
if (rotate == 5){
rotate = 1;
}
WHATGOESHERE?.gotoAndStop(rotate);
}
If I hardcode in an instance name of an object everything works fine - so the rotate function is working properly. The problem is, how can I reference event.currentTarget from the startDrag function while inside of the keyDown event?
My first thought was to set event.currentTarget to a variable and then calling the variable from within the keyDown event. However, targetHold = event.currentTarget; does not seem to record the instance name of the object being clicked...
public var targetHold:Object = new Object;
function ClickToDrag(event:MouseEvent):void {
event.currentTarget.startDrag();
targetHold = event.currentTarget;
trace ("targetHold " + targetHold);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.SPACE){
rotate++;
if (rotate == 5){
rotate = 1;
}
targetHold.gotoAndStop(rotate); //does not work
}
}
function ReleaseToDrop(event:MouseEvent):void {
event.currentTarget.stopDrag();
}
As you click the object, it should have focus. If you register the listener for the KeyboardEvent on the object and not on the stage, it will be .currentTarget.
Here's an example of what I have in mind. Right after starting the drag, add the listener to the same object instead of the stage.
event.currentTarget.startDrag();
event.currentTarget.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
The proper way of doing this would be to define all the functionality in a class. Within a self contained class, you would not need any .currentTarget.
Here is how I would do this: (well, actually I'd follow #null's advice and encapsulate it in a sub class that all your dragable objects would extend, but that is a little broad so this will do)
public var targetHold:MovieClip; //don't make a new object, just create the empty var
public function YourConstructor(){
//your other constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); //don't add the listener in the click function
}
private function clickToDrag(event:MouseEvent):void {
if(targetHold) ReleaseToDrop(null); //safeguard in case flash lost the focus during the mouse up
targetHold = event.currentTarget as MovieClip; //assign the current target. Might as well cast it as MovieClip and get code completion benefits
targetHold.startDrag();
trace ("targetHold " + targetHold);
}
private function myKeyDown(e:KeyboardEvent):void{
//check if target hold exists
if (targetHold != null && e.keyCode == Keyboard.SPACE){
rotate++;
if (rotate == 5){
rotate = 1;
}
targetHold.gotoAndStop(rotate);
}
}
private function ReleaseToDrop(event:MouseEvent):void {
if(targetHold) targetHold.stopDrag();
targetHold = null;
}
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();
}
I have a function in my "enterFrameHandler* function and it is structured like this:
private function addBoss():void{
if(kills >= 3){
trace("boss time");
}
}
Since "addBoss" is a part of my ENTER_FRAME event listener function, "boss time" is traced infinitely as soon as kills reaches 3. I would like this function to stop as soon as it activates 1 time.
Is there another event listener I could use that will check for the function, but then stop as soon as it goes off?
Any suggestions would be appreciated. Thanks!
private function addBoss():void{
if(kills >= 3){
trace("boss time");
removeBossEnterFrame();
}
}
private function removeBossEnterFrame():void{
removeEventListener(Event.ENTER_FRAME, addBoss);
}
I'd prob remove the Enterframe event listner thats calling the addBoss function once you trace out "boss time".
This should do it.
private var isBossTime:Boolean = false;
private function addBoss():void
{
if(kills >= 3 && isBossTime == false)
{
trace("boss time");
isBossTime = true;
}
}
textChanged and valueCommit both event listener are attached with a spark textarea as follows:
addEventListener("textChanged",
function(event:Event):void {
colorize();
},false,0,true);
addEventListener("valueCommit",
function(event:Event):void {
colorize();
},false,0,true);
if I type any thing in textarea, then this colorize() function is called twice. How can I stop this one that both event should not be triggered together. Pls help
If you want to listen for typing, why do you have two listeners? If you really need two listeners, you need to queue colorize with a setTimeout instead of calling it directly:
import flash.utils.setTimeout;
private var colorizeQueued:Boolean = false;
private function queueColorize():void
{
if (colorizeQueued)
return;
colorizeQueued = true;
setTimeout(function():void
{
// Process for real and note update
colorize();
colorizeQueued = false;
}, 100);
}
addEventListener("textChanged",
function(event:Event):void {
queueColorize();
},false,0,true);
addEventListener("valueCommit",
function(event:Event):void {
queueColorize();
},false,0,true);