AS3 Keyboard Events doesn't work - actionscript-3

///toggle
var tamEkranMi:Boolean = false;
toggle.buttonMode = true;
toggle.addEventListener(MouseEvent.CLICK, tamEkran);
function tamEkran(e:MouseEvent)
{
if(tamEkranMi == false)
{
tamEkranMi = true;
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
toggle.gotoAndStop(2);
}
else
{
tamEkranMi = false;
toggle.gotoAndStop(1);
stage.displayState = StageDisplayState.NORMAL;
}
}
/*top of this works fine
below this just doesn't work when i try on a website
*/
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
function reportKeyDown(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.ESCAPE:
tamEkranMi = false;
toggle.gotoAndStop(1);
} /*
if(e.keyCode == Keyboard.ESCAPE)
{
tamEkranMi = false;
stage.displayState = StageDisplayState.NORMAL;
toggle.gotoAndStop(1);
}*/
}
Here is my code block. When i use this in my computer it just works fine but when i use a website to try these codes just not working. The toggle button works but when i use ESC key on my keyboard that code block simply doesn't work.

I think that your problem is just because you are using the Escape key which is reserved to exit the fullscreen mode of the flash player standalone version or in the web browser when the fullscreen mode is active, otherwise, you can catch it without any problem.
For the FULL_SCREEN_INTERACTIVE mode, don't forget to enable it in your html code.
Hope that can help.

Related

AS3 Flash Animation not fully working in captivate 5.5

Hi I have done a few Flash Animations previously in AS3 all of which import and run fine in Captivate 5.5. However, One of them, a simple drag an drop game won't work. It imports and is visible in captivate everything works with one (irritating) problem. That is, the objects will not drop onto the appropriate drop zones. The animation works fine as an SWF in my browser but just will not function when put into captivate
any ideas? The outline of the code is below. I am tearing my hair out, any advice would be greatly appreciated.
code:
right_mc.visible=false;
wrong_mc.visible=false;
var orig1X:Number=item1_mc.x;
var orig1Y:Number=item1_mc.y;
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item1_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);
}
function item1Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone1_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone1_mc.x;
item.y=dropZone1_mc.y;
} else {
item.x=orig1X;
item.y=orig1Y;
}
};
done_btn.addEventListener(MouseEvent.CLICK,checkAnswers);
function checkAnswers(event:MouseEvent):void {
if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
dropZone16_mc.hitTestPoint(item16_mc.x,item16_mc.y)) {
wrong_mc.visible = false;
right_mc.visible = true;
} else {
wrong_mc.visible = true;
right_mc.visible = false;
}
}
reset_btn.addEventListener(MouseEvent.CLICK,reset);
function reset(event:MouseEvent):void {
item1_mc.x=orig1X;
item1_mc.y=orig1Y;
right_mc.visible=false;
wrong_mc.visible=false;
}
Because hitTestPoint only works with global coordinates.
When you open the SWF in the browser, local and global coordinates are the same, that's why it works. But when you load it inside Captivate, they differ.
Try this:
import flash.geom.Point;
// ...
var localPoint:Point = new Point(item.x, item.y);
var globalPoint:Point = item.parent.localToGlobal(localPoint);
if (dropZone1_mc.hitTestPoint(globalPoint.x, globalPoint.y)) {
item.x = dropZone1_mc.x;
item.y = dropZone1_mc.y;
}
// ...

Does fullscreen event trigger resize in as3?

I'm trying to set up a fullscreen toggle and implement elements of fluidity however am uncertain if I have to include a dispatch event in the toggle for screen resize or will this be something that will be detected by the previous eventListener when the fullscreen toggle is activated?
Furthermore having looked online for several days on the subject, I'm still uncertain where it's optimal to place the resize control of my stage elements.
public function Main():void {
addEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.RESIZE, resizeListener);
stage.addEventListener(FullScreenEvent.FULL_SCREEN_INTERACTIVE_ACCEPTED, fullScreenRedraw);
}
private function resizeListener (e:Event):void {
// - Do I put my resize control options here to cater for general resize or see below?
myMovie.width = stage.stageWidth; // etc
}
private function fullScreen(e:MouseEvent):void {
try {
switch (stage.displayState) {
case StageDisplayState.FULL_SCREEN_INTERACTIVE:
/* If already in full screen mode, switch to normal mode. */
stage.displayState = StageDisplayState.NORMAL;
break;
default:
stage.fullScreenSourceRect = null;
// If not in full screen mode, switch to full screen mode.
stage.dispatchEvent(new Event(Event.RESIZE));
stage.displayState = StageDisplayState.NORMAL;
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
break;
}
} catch (err:SecurityError) {
// ignore
}
}
private function fullScreenRedraw(event:FullScreenEvent):void {
if ( event.fullScreen ) {
// FULLSCREEN TRUE
// - Or do I put my resize control options here to cater for the fullscreen as well?
myMovie.width = stage.stageWidth; // etc
var fScrField:TextField = new TextField();
fScrField.y = 480;
fScrField.text = "Redraw : True";
addChild(fScrField);
} else {
// NON FULLSCREEN
fScrField.text = "Redraw : False";
addChild(fScrField);
}
}
** Amended handlers as mentioned on adobe. But conflicting info on what goes where and why?!
private function activateHandler(event:Event):void {
trace("activateHandler: " + event);
}
private function fullScreenRedraw(event:FullScreenEvent):void {
if ( event.fullScreen ) {
// Add additional panels if set/sizes
var fScrField:TextField = new TextField();
fScrField.y = 480;
fScrField.text = "Redraw : True";
addChild(fScrField);
} else {
// Remove additional panels etc
fScrField.text = "";
fScrField.text = "Redraw : False";
addChild(fScrField);
}
}
Now I've been able to get it working in all manner of ways using the code above with variations but having spent days online googling there seems to be no efficiant or clear explanation which is best.
Any help on where this could be better optimised and made more efficient as I've kind of just crowbar'd it together from what I can work out.
Thanks in advance.
I think you're over-complicating it. Try that way:
// in your constructor:
fullScreenBtn.addEventListener(MouseEvent.CLICK, toggleFullScreen);
stage.addEventListener(FullScreenEvent.FULL_SCREEN, refreshStage);
stage.addEventListener(Event.RESIZE, refreshStage);
// then handle stage resize:
private function refreshStage(event:Event = null):void
{
if ( stage.displayState == StageDisplayState.NORMAL ) {
// handle stage in normal mode
} else {
// handle stage in full screen mode
}
}
// handle stage state toggle:
private function toggleFullScreen(event:Event = null):void
{
if ( stage.displayState == StageDisplayState.NORMAL )
stage.displayState = StageDisplayState.FULL_SCREEN;
else
stage.displayState = StageDisplayState.NORMAL;
}

How can I press keyboard to play vdo only once

I'm here again for your help :{ I have a question which I tried to google it but can't find the answer. Well,..May be there is an answer but I just can't make it works? I'm in a process of learning AS3 so let's say I'm still new here.
What I'm doing is making a keyboatd to respond with the vdo files I have. It is a very simple idea as press-n-play. Each keys has their vdos to play and if you press another button while the first one is still pressing, it'll play another vdo of its key. I have make this as boolean with function of keydown and keyup like this:
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;
var isLeft:Boolean = false;
var isRight:Boolean = false;
var video;
var nc;
var ns;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUP);
this.addEventListener(Event.ENTER_FRAME,playVid);
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = this;
video = new Video(550,400);
addChild(video);
video.attachNetStream(ns);
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
//ns.play(TomAndJerry.flv);
isLeft=true;
break;
case 39 :
//ns.play(westler.flv);
isRight = true;
break;
}
}
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
isLeft = false;
break;
case 39 :
isRight = false;
break;
}
}
function playVid(e:Event):void
{
if (isLeft)
{
trace(kk);
ns.play(westler.flv);
isLeft = false;
}
else if (isRight)
{
trace(PP);
ns.play(TomAndJerry.flv);
//isRight = false;
}
}
I have tried making a keydown function without using any boolean or those true of false things to just play a vdo. It worked but, I still have the same problem that I can't find a solution which is ....
When you hold down the keyboard button the vdo will keep start at the beginning.
All I want is to play the vdo even the key is press down. If the vdo ends then play again as loop but if the key is up the vdo will play until it ends.
And if there are more than one button are holding down just play the vdo of the lastest pressed button.
T-T"
Thanks.
Ps. I have tried removeEventListener but, it made every buttons' function gone.
your playvid function is called each frame so i think it's normal your video don't start, I think you could try to change your code as follow:
// add net status handler event to check the end of the video
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// remove this line
//this.addEventListener(Event.ENTER_FRAME,playVid);
/** a key is pressed **/
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
// start the video just if the video don't play
if(!isLeft) ns.play("TomAndJerry.flv");
// video left is playing
isLeft = true;
// video right isn't playing
isRight = false;
break;
case Keyboard.RIGHT:
// start the video just if the video don't play
if(!isRight) ns.play("westler.flv");
// video rightis playing
isRight = true;
// video left isn't playing
isLeft = false;
break;
}
}
/** a key is released **/
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
isLeft = false;
break;
case Keyboard.RIGHT:
isRight = false;
break;
}
}
/** net status change, verify if we reach the end of the video **/
function netStatusHandler(e:NetStatusEvent):void
{
// when netStatus code is NetStream.Play.Stop the video is complete
if (e.info.code == "NetStream.Play.Stop")
{
// right key is still pressed we loop the video
if( isRight ) ns.play("westler.flv");
// left key is still pressed we loop the video
else if( isLeft ) ns.play("TomAndJerry.flv");
}
}
I hope this will help you :)

Adding Time Delay to Character Movement

I've been looking to make an animation using ActionScript 3.0 in Adobe Flash Professional where a character (John) can be moved by the viewer using the arrow keys. I've made two sprites, John (the default standing character) and JohnLeg (the character with a raised leg), and I switch between them when the up key is pressed to make it look like he is walking. I've tried this by making one invisible and the other visible.
However, at the moment it only shows JohnLeg for 0 seconds, so I believe I need to set a time delay when he moves to show JohnLeg for half a second before switching back.
My code only considers the up key at the moment, and most of it was taken using the code snippets in Adobe Flash:
var upPressed:Boolean = false;
John.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_4);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_4);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_4);
function fl_MoveInDirectionOfKey_4(event:Event)
{
JohnLeg.visible = false;
JohnLeg.x = John.x
JohnLeg.y = John.y
if (upPressed)
{
JohnLeg.visible = true;
John.visible = false;
John.y -= 5;
//set time delay here
JohnLeg.visible = false;
John.visible = true;
}
}
function fl_SetKeyPressed_4(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
}
function fl_UnsetKeyPressed_4(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
You could try setTimeout.
Here it some help doc http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000602.html

Play a sound from library with the space bar in AS3

I've been trying to come up with a simple code that plays a sound "jump" from the library each time the spacebar is pressed, but no luck. Everything I try turns my fla/swf into a strobe light.
var spacebarDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, _keyHandler);
function _keyHandler(e:KeyboardEvent):void
{
if(e.keyCode == 32)
{
switch(e.type)
{
case KeyboardEvent.KEY_DOWN:
if(!spacebarDown)
{
spacebarDown = true;
// Play Sound.
var sfx:YourSound = new YourSound();
sfx.play();
}
break;
case KeyboardEvent.KEY_UP: spacebarDown = false; break;
}
}
}