AS3 VideoEvent Listener - Event.Ready - actionscript-3

I am trying to catch READY event of FLV Video Player. I am not quite sure what is going on with this codes. FLV player is imported by Adobe Flash and I need to start readyHandler whenever video is ready to play (Function will erase the "Video is loading" text). I used video_oynattir object flawlessly. However, these codes brake the animations...
video_oynattir.addEventListener(Event.READY, readyHandler);
function readyHandler(event:VideoEvent):void
{
trace("hurray");
}
And produce this error:
Accesss of possibly undefined property READY through a refferance with static type class*
If I comment out code segment. It executes perfectly.
Full code of stage as follows:
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.net.URLRequest;
import flash.net.navigateToURL;
var clickTAG:String = loaderInfo.parameters.clickTAG;
var displayType:String = loaderInfo.parameters.displayType;
video_oynattir.autoPlay = false;
if(displayType == "collapse") {
gotoAndStop("kapali");
}else{
openButton.visible = false;
gotoAndStop("acik");
}
video_button.addEventListener(MouseEvent.CLICK, videodurdur);
function videodurdur(e:MouseEvent):void {
if(video_oynattir.state == "playing") {
video_oynattir.stop();
}else{
video_oynattir.play();
}
}
video_oynattir.addEventListener(Event.READY, readyHandler);
function readyHandler(e:VideoEvent):void {
trace("at");
}
//Accesss of possibly undefined property READY through a refferance with static type class
closeButton.addEventListener(MouseEvent.CLICK, closeRichMedia);
function closeRichMedia(e:MouseEvent):void {
video_oynattir.stop();
ExternalInterface.call("dopushunlock");
gotoAndPlay(3);
openButton.visible = true;
}
openButton.addEventListener(MouseEvent.CLICK, openRichMedia);
function openRichMedia(e:MouseEvent):void {
ExternalInterface.call("dopushlock");
gotoAndStop(2);
openButton.visible = false;
}
clickButton.addEventListener(MouseEvent.CLICK, gotoLink);
function gotoLink(e:MouseEvent):void {
navigateToURL(new URLRequest(clickTAG), "_blank");
}
stop();

Please try to change the event type the EventListener listens to to "VideoEvent":
video_oynattir.addEventListener(VideoEvent.READY, readyHandler);
Make sure to import "fl.video.VideoEvent".

Related

Adding a symbol from the library to the stage using code

I'm new to coding and unfortunately my teacher is more programmer than teacher so he is very vague on how to do things. I'm aiming for something simple I have a symbol from my library dragged and dropped directly on the stage as my Background and in the code for the background object I'm trying to add a small arrow for the menu selector that you move with the mouse keys. I know its something simple I'm not understanding so if anyone can help that would be great!
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class BG extends MovieClip
{
public var select:Select = new Select ;
public function BG()
{
// constructor code
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
addChild(select);
select.x = 200;
select.y = 200;
}
private function addedToStage(ev:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
private function keyDownHandler(ev:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
select.y = 250;
}
if (event.keyCode == Keyboard.UP)
{
select.y = 200;
}
}
private function removedFromStage(ev:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
}
}
When you test your movie, you should be getting error messages like:
BG.as, Line 34 1120: Access of undefined property event.
The errors can give you a hint at what is going wrong. In this case, it's telling you that there is no property named "event". In your keyDownHandler function, you need to replace "event" with "ev" which is what you are naming your KeyboardEvent in the function declaration. So this should make your code work:
if (ev.keyCode == Keyboard.DOWN)
{
select.y = 250;
}
if (ev.keyCode == Keyboard.UP)
{
select.y = 200;
}
Also, you should use parenthesis when creating object instances. Even though it still compiles, you should be writing:
public var select:Select = new Select();

Type was not found or was not a compile-time constant?

I don't understand what is wrong with my code. When I debug it tells me: 1046: Type was not found or was not a compile-time constant: LocationChangeEvent. This is the code:
// imports
import flash.events.Event;
import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;
// setup variables
var _stageWebView:StageWebView;
var myAdvertURL:String = "http://terrypaton.com/ads/exampleAdvert.html";
//
function createAd(event:MouseEvent):void {
// check that _stageWebView doersn't exist
if (! _stageWebView) {
_stageWebView = new StageWebView () ;
// set the size of the html 'window'
_stageWebView.viewPort = new Rectangle(0,0,480,80);
// add a listener for when the content of the StageWebView changes
_stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,onLocationChange);
// start loading the URL;
_stageWebView.loadURL(myAdvertURL);
}
// show the ad by setting it's stage property;
_stageWebView.stage = stage;
}
function toggleAd(event:MouseEvent):void {
trace("toggling advert",_stageWebView);
// check that StageWebView instance exists
if (_stageWebView) {
trace("_stageWebView.stage:"+_stageWebView.stage);
if (_stageWebView.stage == null) {
//show the ad by setting the stage parameter
_stageWebView.stage = stage;
} else {
// hide the ad by nulling the stage parameter
_stageWebView.stage = null;
}
} else {
// ad StageWebView doesn't exist - show create it
createAd(null);
}
}
function destroyAd(event:MouseEvent):void {
// check that the instace of StageWebView exists
if (_stageWebView) {
trace("removing advert");
// destroys the ad
_stageWebView.stage = null;
_stageWebView = null;
}
}
function onLocationChange(event:LocationChangeEvent):void {
// check that it's not our ad URL loading
if (_stageWebView.location != myAdvertURL) {
// destroy the ad as the user has kindly clicked on my ad
destroyAd(null);
// Launch a normal browser window with the captured URL;
navigateToURL( new URLRequest( event.location ) );
}
}
// setup button listeners
createAdBtn.addEventListener(MouseEvent.CLICK,createAd);
toggleAdBtn.addEventListener(MouseEvent.CLICK,toggleAd);
destroyAdBtn.addEventListener(MouseEvent.CLICK,destroyAd);
Is there a way to change this code to make it work, or this code can't be used any longer?
According to the reference, LocationChangeEvent should indeed be in flash.events if compiled in AS3. I'm not sure why it's not found.
Try compiling a simpler example:
import flash.events.LocationChangeEvent;
var ev:LocationChangeEvent;

Writing an 'if' statement in ActionScript

Is there a way of writing an if statement that involves the following?
if (MovieClip1 reaches last frame)
{
addChild(MovieClip2)
removeChild(MovieClip1)
}
Basically, all I want to happen is when my MovieClip finishes, it will change to another MovieClip or image. I know it is probably very simple, but how can I do it?
All code runs on command. So your conditional will run immediately and never run again.
Instead, you use events. You attach a listener to an event and when that event fires (is "dispatched" to use correct vocabulary), your code is called again.
var movieClip:MovieClip = new MovieClip();
movieClip.addEventLister( Event.ENTER_FRAME, this.enterFrameHandler ); //will be called on every frame enter
function enterFrameHandler( e:Event ):void {
if ( movieClip.currentFrame == movieClip.totalFrames ) {
// do something
}
}
So you listen for each new frame and in that handler, you check if it is the last frame or not.
As an extra tidbit, the standard naming convention for AS3 is to use lowercase, camelcase (thisIsAnExampleOfthat) for all objects. Package names should be in all lowercase and only Class names should be capitalized.
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Main extends MovieClip
{
var firstScene:FirstScene = new FirstScene;
var movement:Movement = new Movement;
var green:Green = new Green;
public function Main()
{
addChild(firstScene);
firstScene.addEventListener(KeyboardEvent.KEY_DOWN, mClick);
movement.addEventListener(Event.ENTER_FRAME, endChange);
}
function mClick(event:KeyboardEvent):void
{
if (event.keyCode == 77);
{
addChild(movement);
removeChild(firstScene);
}
}
function endChange(event:Event):void
{
if (movement.currentFrame == movement.totalFrames)
{
addChild(green);
removeChild(movement);
}
}
}
}

Space bar and arrow keys on Radio buttons trigger TypeError: Error #1009

I've just discovered a bug in my app, it looks like a Flash player bug, i would like to know if anyone has found a workaround or something.
I've some radio buttons groups in my app. If I press the arrow keys while holding the spacebar pressed, it ends up triggering
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.controls::LabelButton/drawIcon()
at fl.controls::LabelButton/draw()
at fl.controls::RadioButton/draw()
at fl.core::UIComponent/callLaterDispatcher()
If found this thread that describes my same situation.
Do you think there's any workaround for this? Is it really a flash bug?
So, I've investigated this problem and found, that there is an error in standard fl components. It is not only about RadioButton. If you hold SpaceBar and then switch current UI element by clicking "TAB" button - you will receive similar error.
To solve it, you should fix source of the component: in class LabelButton, method keyUpHandler replace code
setMouseState(oldMouseState);
with
if (oldMouseState!==null) {
setMouseState(oldMouseState);
}
and add override:
override protected function focusOutHandler(event:FocusEvent):void {
if (oldMouseState) {
keyUpHandler(new KeyboardEvent(KeyboardEvent.KEY_UP, true, true, 0, Keyboard.SPACE));
}
super.focusOutHandler(event);
}
If you don't know, how you can do it:
just copy Adobe Flash CS5 install folder\Common\Configuration\Component Source\ActionScript 3.0\User Interface\fl\controls\LabelButton.as to another directory with the same package structure \fl\controls\LabelButton.as
then you need include this duplicated structure to project.
It is not very good way, but in this case it really fixes this error. BTW, I'll send it to Adobe.
actually its not a flash bug its component's bug. if space bar is no need for your radio buttons(i think its useless) you can disable it with creating your own MyRadioButton which extends RadioButton class this will be disable space key input
package {
import flash.events.KeyboardEvent;
import fl.controls.RadioButton;
import flash.ui.Keyboard;
public class MyRadioButton extends RadioButton{
public function MyRadioButton() {
super();
}
override protected function keyUpHandler(event:KeyboardEvent):void {
if(event.keyCode != Keyboard.SPACE){
super.keyUpHandler(event);
}
}
override protected function keyDownHandler(event:KeyboardEvent):void {
if(event.keyCode != Keyboard.SPACE){
super.keyDownHandler(event);
}
}
}
}
just you need to change class property fl.controls.RadioButton to MyRadioButton from your library element:"RadioButton". (i assume MyRadioButton is near fla)
------>edit
here is another solution without disabling spacebar. this time when user makes anykeydown before spacebar keyup code runs spacebar keyup before anykeydown. and focus get removed from radio button also you can add anyother solutions like if spacebar keystate is down donot let any keydown.
package {
import flash.events.KeyboardEvent;
import fl.controls.RadioButton;
import flash.ui.Keyboard;
import flash.events.Event;
public class MyRadioButton extends RadioButton{
private var _isSpaceDown:Boolean = false;
public function MyRadioButton() {
super();
}
override protected function handleChange(event:Event):void {
if (_isSpaceDown) {
keyUpHandler(new KeyboardEvent(KeyboardEvent.KEY_UP, true, true, 0, Keyboard.SPACE));
setMouseState('up');
}
super.handleChange(event);
}
override protected function keyUpHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.SPACE){
if(_isSpaceDown){
_isSpaceDown = false;
}else{
return;
}
}
super.keyUpHandler(event);
}
override protected function keyDownHandler(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.SPACE){
_isSpaceDown = true;
}else{
if(_isSpaceDown){
var e:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN);
e.keyCode = Keyboard.SPACE;
super.keyUpHandler(e);
_isSpaceDown = false;
}
}
super.keyDownHandler(event);
}
}
}

Accessing Function on MovieClip from Document Class

[NOTE: Right off the bat, I already know the popular theory about "no code on MovieClips," so please don't reply with that (or
downvote because of it). Every project is different. I just need the answer.]
I have a project in Adobe Flash CS5.5, Adobe AIR 3, and ActionScript 3.
I need to call a function on the main timeline of the project that the document class is linked to from inside the document class. For the sake of example, let's call this function "Ring()".
How do I call this function, "Ring()" from inside my document class?
Put the function you want to call in your document class, and dispatch a custom event (or any event, if the code is readable) from the timeline of the object and listen for that event on your document class.
So the code breakdown would look like this:
On some frame of the timeline in your document (should work on any object):
var customEvent:Event = new Event(Event.COMPLETE);
this.dispatchEvent(customEvent);
In your document class:
public function DocumentClass()
{
// get the reference to the object
lolcats.objectThatDispatchesEvent.addEventListener(Event.COMPLETE, _customHandler);
}
protected function _customHandler(event:Event):void
{
// FUNCTION NAMES SHOULD START WITH LOWERCASE! ^_^
Ring();
}
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
Basically you register any string that defines your event, Event.COMPLETE evaluates to "complete", you can just register whatever you want, like:
var custEvent = new Event("anyCustomString");
this.dispatchEvent(custEvent);
// catch it with
addEventListener("anyCustomString", _handler);
Well, since you seem to be using some oldskool ninja techniques, I would suggest that you should keep it simple and straightforward.
Say you have some functions on the main timeline:
function Ring1():String
{
return "Ring1() called!";
}
var Ring2:Function = function () : String
{
return "Ring2() called!";
};
The scenario for a document class of the said timeline would be like this:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.getQualifiedClassName;
import flash.utils.describeType;
public class Test extends MovieClip
{
public function Test()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private function onMouseDown(event:MouseEvent):void
{
trace(getQualifiedClassName(this)+".onMouseDown()");
try {
var ring1:Function = this["Ring1"] as Function;
var ring2:Function = this["Ring2"] as Function;
} catch (error:Error) {
// ignore
}
if (ring1 != null) {
trace("\t", "Ring1", "=", ring1);
trace("\t", ring1());
} else {
trace("\t", "Ring1() function not found in "+this+"!");
}
if (ring2 != null) {
trace("\t", "Ring2", "=", ring2);
trace("\t", ring2());
} else {
trace("\t", "Ring2() function not found in "+this+"!");
}
// for your interest:
var doc:XML = describeType(this);
var ring1Node:XML = doc.descendants("method").(#name == "Ring1")[0];
var ring2Node:XML = doc.descendants("variable").(#name == "Ring2")[0];
trace("declaration of Ring1:", ring1Node.toXMLString());
trace("declaration of Ring2:", ring2Node.toXMLString());
// so, you may probably make use of reflection
// unless you need to reference dynamic members on the main timeline
}
}
}
See comments in the code above.