can you make Keyboard events differ on different pages? - actionscript-3

Hey is there a way to have certain keys do one thing on one page and then different on another, as i use
But then on next frame i cant use space bar again to do another task?
Any help would be appreciated
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
gotoAndPlay("welcome");
}
}

just remove the listener before you leave the frame.
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
gotoAndPlay("welcome");
}
}
in the frame "welcome" add the same code again and just change the gotoAndPlay() part.
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
gotoAndPlay("a different frame");
}
}

Related

Keyboard Event copping another event flash as3

Hellow again people.
continuing on making the score board, I have not come across a problem with my Keyboard Event.
The reset and start keys works fine but the stop key doesn't stop it rather resets it. I think it might have something to do with me putting the stop command in the reset code as well but I'm not sure how to go around it.
they all work perfect as (btn) but that's no good for me as the monitor will be far away. take a look?
stage.addEventListener( KeyboardEvent.KEY_DOWN, KeysDown);
function KeysDown(event:KeyboardEvent)
//reset the timer with (space)
{
if(event.keyCode == Keyboard.SPACE)
timer.stop();
timeRemaining = 300;
showTime.text = formatTimeRemaining();
// start the timer
if(event.keyCode == Keyboard.ENTER)
timer.start();
// stop the timer
if(event.keyCode == Keyboard.S)
timer.stop();
}
When writing if statements, the curly braces { } must surround the body of the statement (if the body is 1 line then the braces can be omitted).
Always use the braces and you won't run into this problem again.
stage.addEventListener( KeyboardEvent.KEY_DOWN, KeysDown);
function KeysDown(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.SPACE)
{
timer.stop();
timeRemaining = 300;
showTime.text = formatTimeRemaining();
}
// start the timer
else if(event.keyCode == Keyboard.ENTER)
{
timer.start();
}
// stop the timer
else if(event.keyCode == Keyboard.S)
{
timer.stop();
}
}

Flash GotoFrame on Keypress

I am trying to figure out some code where when I press one of the 4 arrow keys, flash goes to a certain frame on the timeline. And when none of these keys are being pressed, it will return to its original start point on the timeline. I have seen some examples on this site, but I am struggling with it, especially returning to the original point on the timeline when there are no keypresses.
So for example :
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
gotoAndStop(40);
}
if (event.keyCode == Keyboard.RIGHT)
{
gotoAndStop(30);
}
if (event.keyCode == Keyboard.UP)
{
gotoAndStop (10);
}
if (event.keyCode == Keyboard.DOWN)
{
gotoAndStop (20);
}
}
stop();
I can get it to go to various points on the timeline, but I do not know how to make it return to its original point on the time line (keyframe 1) when none of those keyboard keys are being pressed.
What I am trying to do is to show on screen when a button is being pressed, and when its no longer being pressed to disapear
If anyone can help, I would be grateful. Thanks
Detect KEY_DOWN and KEY_UP events, if you want to return to the first frame when you release the keys, this could be a possible solution:
var frames:Object = {
38: 10, // Up Key
40: 20, // Down Key
37: 40, // Left Key
39: 30 // Right Key
};
stage.addEventListener(KeyboardEvent.KEY_DOWN, showMessage);
stage.addEventListener(KeyboardEvent.KEY_UP, hideMessage);
function showMessage(event:KeyboardEvent):void{
if(frames.hasOwnProperty(event.keyCode.toString())) gotoAndStop( frames[event.keyCode] );
}
function hideMessage(evt:KeyboardEvent):void{
gotoAndStop( 1 );
}

moving a movie clip by pressing a button AS3

Does anyone know how to move a movie clip by clicking a button on the stage. I can get it to move in increments, but I want it to move constantly. Currently I have this:
down.addEventListener(MouseEvent.MOUSE_DOWN, arrowDown);
function arrowDown(event:MouseEvent):void
{
bottomArrow.y += 1;
}
First, you should listen for KeyboardEvents instead of MouseEvent. Then I think you should listen for those events being dispatched by the stage.
Here is an example using the Event.ENTER_FRAME event. If you'd like to control better the speed of you sprite moves you might want to user a timer instead.
This example works when the down arrow is pressed but you can change Keyboard.DOWN with any key you want.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onEnterFrame(event:Event):void
{
bottomArrow.y += 1;
}

trigger sound effects when mc hits a mc! FLASH AS3

i have two mcs, i want this mc to hit another mc which then plays a loud bang. How would i do this? I'll be using a keyboard event to go with it as well.
Thanks
Create a new Flash ActionScript 3 document. be save.
Put your sound like names boom.mp3 in same directory.
Draw a square shape on the stage and convert it into a Movie Clip symbol.
Put a total of 2 instances of this Movie Clip symbol on the stage.
Bring up the Actions panel and add the following code.
Code below, simply drag the mouse to mc1 and mc2 collision when sound is played. is a basic collision with sound playback.
import flash.media.Sound;
import flash.events.MouseEvent;
import flash.net.URLRequest;
mc1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
var sound:Sound=new Sound();
sound.load(new URLRequest("boom.mp3"));
function drag(e:MouseEvent):void
{
mc1.startDrag();
}
function drop(e:MouseEvent):void
{
mc1.stopDrag();
if (mc1.hitTestObject(mc2))
{
trace("Collision detected!");
sound.play(0);
}
else
{
trace("No collision.");
}
}
If you want using a KeyboardEvent try this:
function playIsCollision(char:DisplayObject, target:DisplayObject):void
{
if(char.hitTestObject(target))
{
sound.play(0);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
trace("right move");
mc1.x += 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.LEFT)
{
trace("left move");
mc1.x -= 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.UP)
{
trace("up move");
mc1.y -= 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.DOWN)
{
trace("down move");
mc1.y += 1;
playIsCollision(mc1,mc2);
}
}
here is my code: Collision

Detect first key-down event only in AS3

In the KeyboardEvent object I get on key-down events, is there a way to know if the event is the first key-down, or a repeat when the key is held down?
I want to toggle a setting when a key is pressed; I could listen to key-up but I'd prefer the action is taken as soon as the key is pushed... as long as I can stop it toggling back and forth as the key-down repeat events are generated.
Tested and works
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
var keys:Object = {};
function onDown(e:KeyboardEvent):void {
if( ! Boolean(e.keyCode in keys)) {
trace("First Time");
keys[e.keyCode] = true;
}
}
function onUp(e:KeyboardEvent):void {
delete keys[e.keyCode];
}
The idea of storing key codes in an object and the use of delete on key up originally came from Senoculars library. See http://www.senocular.com/flash/actionscript/
The easiest thing is to remove or disable the KEY_DOWN listener with a flag inside the KEY_DOWN listener, and then add or enable it in the KEY_UP.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}
private function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}