Actionscript 3 detect successive key press - actionscript-3

I have 2 movie clips. One has a character walking and the other one he's running. How do I make it so that if I press the left arrow key, it will play the walking clip. If I press the left arrow in quick succession, it will switch to the running clip. I am using Actionscript 3.

This requires a bit of codework as Flash AS3 does not have native support for double clicks. Here is a simple example of how you can achieve this effect with SetTimeout
var keyCodeToListenFor = '13'; // can be any keycode;
var keySinglePress:Boolean = false;
var keyDoublePress:Boolean = false;
var timeToWaitForDoublePress:Number = 400;
var waitingForDoublePress:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
function handleKeyDown(e:Event) {
if (e.keyCode == keyCodeToListenFor) {
keySinglePress = true;
if (waitingForDoublePress) {
keyDoublePress = true;
}
waitingForDoublePress = true;
setTimeout(function() {
waitingForDoublePress = false;
keyDoublePress = false;
}, timeToWaitForDoublePress);
}
if (keyDoublePress) {
trace('You logged a double press!')
} else if (keySinglePress) {
trace('You logged a single press!');
}
}

Related

How can I make a symbol visible only if other two symbols are visible in ActionScript 3.0?

I want to make a symbol visible (symbol3) only if symbol1 and symbol2 are visible in ActionScript 3.0 (Adobe Animate CC), but I can't make it, because I am really new in programming... Can anyone help me? I have this code:
symbol1.visible = false;
symbol2.visible = false;
symbol3.visible = false;
button1.addEventListener(MouseEvent.CLICK, fl_ClickToHide_1);
function fl_ClickToHide_1(event:MouseEvent):void
{
symbol1.visible = true;
}
button2.addEventListener(MouseEvent.CLICK, fl_ClickToHide_2);
function fl_ClickToHide_2(event:MouseEvent):void
{
symbol2.visible = true;
}
symbol3.visible = symbol2.visible && symbol1.visible
Thanks a lot,
Tom
The reason why it doesn't work for you is because symbol3.visible = symbol2.visible && symbol1.visible; is being executed right after all is hidden and buttons listeners were added. After you click buttons only code in fl_ClickToHide_1 or fl_ClickToHide_2 is being executed and in there you just change visibility of one symbol. If you want to execute checking after click you can do something like that:
symbol1.visible = false;
symbol2.visible = false;
symbol3.visible = false;
button1.addEventListener(MouseEvent.CLICK, fl_ClickToHide_1);
function fl_ClickToHide_1(event:MouseEvent):void
{
symbol1.visible = true;
do_magic();
}
button2.addEventListener(MouseEvent.CLICK, fl_ClickToHide_2);
function fl_ClickToHide_2(event:MouseEvent):void
{
symbol2.visible = true;
do_magic();
}
function do_magic():void
{
symbol3.visible = symbol2.visible && symbol1.visible;
}

Using a variable to allow only one video to play at a time

I recently created a project that has four buttons. Each button adds an FLV player to the stage and plays a video. During testing, I noticed that clicking another button before the first function is finished loading the movie, TWO movies will play.
Is there a more efficient way of handling this? I feel like there is a lot of redundant code here, is there a better way to do the following:
Button One Pressed
Load Video One
Ignore all other event listeners until the movie ends, or the user dismisses the video (Clicks to dismiss)
I initially though it would be a good idea to create a two functions, one that removes all event listeners, and another that adds all the event listeners. When a button is pressed, before it loads the FLVPlayback, remove all event listeners. When the movie is COMPLETE, add all the listeners back. That seems taxing, and very inefficient.
I wrote the following code as an example of what I'm trying to do. (This outputs the same string "One Can play" every time though).
var canLaunchOne:Boolean;
var canLaunchTwo:Boolean;
var canLaunchThree:Boolean;
var canLaunchFour:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
trace(canLaunchTwo);
function playMovieOne(event:Event):void {
canLaunchOne = true;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = false;
trace(canLaunchOne);
playTheRightVideo();
}
function playMovieTwo(event:Event):void {
canLaunchOne = false;
canLaunchTwo = true;
canLaunchThree = false;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieThree(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = true;
canLaunchFour = false;
playTheRightVideo();
}
function playMovieFour(event:Event):void {
canLaunchOne = false;
canLaunchTwo = false;
canLaunchThree = false;
canLaunchFour = true;
playTheRightVideo();
}
function playTheRightVideo():void {
if(canLaunchOne = true){
trace("One Can Play"); // do all that video stuff for video one
} else if(canLaunchTwo = true){
trace("Two Can Play"); // do all that video stuff for video one
} else if(canLaunchThree = true){
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("Four Can Play");
}
}
I tried this code, and I got it "working", but every function will always set canLaunchMovie to true...
import flash.events.Event;
var canLaunchMovie:Boolean;
buttonMCOne.addEventListener(MouseEvent.MOUSE_DOWN, playMovieOne);
buttonMCTwo.addEventListener(MouseEvent.MOUSE_DOWN, playMovieTwo);
buttonMCThree.addEventListener(MouseEvent.MOUSE_DOWN, playMovieThree);
buttonMCFour.addEventListener(MouseEvent.MOUSE_DOWN, playMovieFour);
function playMovieOne(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("One Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie one) Dave");
}
}
function playMovieTwo(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Two Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie two) Dave");
}
}
function playMovieThree(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Three Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie three) Dave");
}
}
function playMovieFour(e:Event):void {
if(canLaunchMovie = true){
this.canLaunchMovie = false;
trace("Four Can Play"); // do all that video stuff for video one
} else {
trace("I can't do that(play movie four) Dave");
}
}
You don't need to have more than one flvplayback.
You only need to change the source of your flvplayback.

Actionscript 3: How to detect when a button isn't pressed in time?

Okay, this is another question for the game I am making. I am putting together a second level, where you need to press a long series of keys at the right time. Pressing them too early or too late results in failure. What I need to know is how to detect when a key ISN'T pressed, when it should have been. This is what I have so far:
var count3:Number = 23;
var myTimer3:Timer = new Timer(1000, count3);
var timeLeft3:Number = count3;
var buttonPressed:Boolean = false;
var btnCounter:Number = 2;
var btnTimer:Timer = new Timer(1000, btnCounter);
myTimer3.addEventListener(TimerEvent.TIMER, countdown3);
myTimer3.start();
btnTimer.addEventListener(TimerEvent.TIMER, btnCountdown);
btn1.addEventListener(MouseEvent.CLICK, buttonPress);
btn1.visible = false;
btn2.visible = false;
btn3.visible = false;
btn4.visible = false;
btn5.visible = false;
btn6.visible = false;
btn7.visible = false;
btn8.visible = false;
btn9.visible = false;
function countdown3(event:TimerEvent): void {
if (((count3)-myTimer3.currentCount)==20) {
btn1.visible = true;
btnTimer.start();
} else if (((count3)-myTimer3.currentCount)==19) {
btn1.visible = true;
} else {
btn1.visible = false;
}
}
function btnCountdown(event:TimerEvent):void {
if (((btnCounter)-btnTimer.currentCount)==0) {
if (buttonPressed = true) {
btnTimer.stop();
} else {
gotoAndStop(2);
}
}
}
function buttonPress (event:MouseEvent): void {
buttonPressed = true;
}
For some reason, it won't do anything when btnCounter hits 0. If someone could help me sort this out, that would be awesome. Thanks.
N.B. This is a personal project, I am just learning actionscript
Start a timer for the time duration when the button is supposed to be pressed (for example, if the button must be pressed within 2 seconds, set the timer's interval to 2 seconds).
Then create a global Boolean variable (or class member variable, however your scene is set up) and set it to false initially. When the button is pressed, set this variable to true and disable the timer.
If the timer fires and this variable is false, it means that it hasn't been disabled by the button so the user didn't click the button within 2 seconds.
Don't use terribly short times - timers are not very accurate and processor speed may have an effect on their duration. (Human reaction times aside.)

swap depthes when movie clips loaded from library

I have two movie clips in the library with linkage.
on the stage I have two buttons - each load a movie clip to a specific mc target on the stage.
I also have a third button that removes the mc target, to clear the stage.
I want to know how can I change the code in AS3 so the loaded movie clips will not show at the same time, but swap each other, like I used to use depth in AS2.
This is the code:
var myIgool = new igool ();
var myRibooa = new ribooa ();
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
mc_all.addChild (myIgool);
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
mc_all.addChild (myRibooa);
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
removeChild(mc_all);
;
}
I would recommend something like this:
var myIgool = new igool ();
var myRibooa = new ribooa ();
mc_all.addChild(myIgool);
mc_all.addChild(myRibooa);
myIgool.visible = false;
myRibooa.visible = false;
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
myIgool.visible = true;
myRibooa.visible = false;
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = true;
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = false;
}
But if you really want to swap, you could also do the following, however I recommend setting the visible flag as it's more efficient rather than covering something up that wouldn't need to draw.
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
if (myIgool.parent != mc_all)
{
mc_all.addChild(myIgool);
}
else
{
mc_all.setChildIndex(myIgool, mc_all.numChildren - 1);
}
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
if (myRibooa.parent != mc_all)
{
mc_all.addChild(myRibooa);
}
else
{
mc_all.setChildIndex(myRibooa, mc_all.numChildren - 1);
}
}

ActionScript 3 Mouse Move event not dispatching

Script problem is that every movieclip dispatch down and up mouse event but mouse move event is not dispatching by some movieclips, which is an unexpected behaviour while I have traced the down event and it trace successfully on every object
also recommend your feedback on my code, thanks.
private function loadPurchasedClip(){
var decorationItem:String;
var lastItemIndex:uint = this.getChildIndex(tree1);
var item:Sprite;
for(var a in purchasedItems){
for(var b in purchasedItems[a]){
if(purchasedItems[a][b].item=='shed'){
item = new shed();
} else {
var ClassDefinition:Class = loadedDecorationItem.purchaseItem(purchasedItems[a][b].item) as Class;
item = new ClassDefinition();
}
item.x = purchasedItems[a][b].posX;
item.y = purchasedItems[a][b].posY;
item.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent){
Mouse.cursor = "hand";
e.target.startDrag(false);
dusbin.visible = true;
item.addEventListener(MouseEvent.MOUSE_MOVE,trashMe);
});
item.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent){
Mouse.cursor = "auto";
e.target.stopDrag();
externalPhpCall(e);
dusbin.visible = false;
if(trashClip){
removeChild(trashClip);
trashClip = null;
}
});
item.mouseChildren = false;
// if item is fence or flowers then move them behind the tree
if(
String(purchasedItems[a][b].item).indexOf('fence')!=-1
||
String(purchasedItems[a][b].item).indexOf('flower')!=-1
){
addChildAt(item,lastItemIndex);
lastItemIndex++;
} else {
addChildAt(item,this.numChildren-2);
}
purchasedNameAr[getChildIndex(item)] = purchasedItems[a][b].item;
}
}
Can't be sure, but I think it's probably that you're expecting a clip to continue to dispatch MouseEvent.MOUSE_MOVE events even once the mouse has left the clip - this won't happen, it's only whilst the local mouse pointer co-ordinates (ie yourClip.mouseX/mouseY) intersect the graphics of the clip itself that it will fire - even when dragging a clip, it can't be guaranteed that it will dispatch a MOVE event.
Let's suppose your clips are all on the root, which means you have access to 'stage' - you could do this:
replace:
item.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
with:
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
...but you should remember to remove that event when necessary (use stage again, in case mouse is not released over the clip):
stage.addEventListener(MouseEvent.MOUSE_UP,endMove);
//Don't use anon function as won't have stage reference:
function endMove(e:MouseEvent):void {
//The rest of your code, then:
stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
}
private function loadPurchasedClip(){
var decorationItem:String;
var lastItemIndex:uint = this.getChildIndex(tree1);
var item:Sprite;
var Move:Boolean
for(var a in purchasedItems){
for(var b in purchasedItems[a]){
if(purchasedItems[a][b].item=='shed'){
item = new shed();
} else {
var ClassDefinition:Class = loadedDecorationItem.purchaseItem(purchasedItems[a][b].item) as Class;
item = new ClassDefinition();
}
item.x = purchasedItems[a][b].posX;
item.y = purchasedItems[a][b].posY;
item.addEventListener(e:Event.ENTER_FRAME, onEnterFrame);
item.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent){
Mouse.cursor = "hand";
e.target.startDrag(false);
Move = true
dusbin.visible = true;
});
item.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent){
Mouse.cursor = "auto";
e.target.stopDrag();
externalPhpCall(e);
dusbin.visible = false;
if(trashClip){
removeChild(trashClip);
trashClip = null;
}
});
item.mouseChildren = false;
// if item is fence or flowers then move them behind the tree
if(
String(purchasedItems[a][b].item).indexOf('fence')!=-1
||
String(purchasedItems[a][b].item).indexOf('flower')!=-1
){
addChildAt(item,lastItemIndex);
lastItemIndex++;
} else {
addChildAt(item,this.numChildren-2);
}
purchasedNameAr[getChildIndex(item)] = purchasedItems[a][b].item;
}
function onEnterFrame(e:Event):void{
if(Move){
// what ever here
{
}