ActionScript code problems - actionscript-3

I have only recently started to learn ActionScript 3.0 I was practising in Flash and i ran into this problem:
Scene 1, Layer 'Layer 1', Frame 1, Line13 1119: Access of possibly undefined property dosomething through a reference with static type flash.net:SharedObject.
What i am tring to do is use the SharedObject.send method to send a message obviously. I have edited some server side code in my main.asc file. And i am trying to pass in the doSomething function but i get that compile error.
Any advice would be appreciated for a novice like myself.
The code is below:
import flash.net.NetConnection;
import flash.net.SharedObject;
var nc:NetConnection = new NetConnection();
nc.connect("rtmp:/exampletest/");
var so:SharedObject = SharedObject.getRemote("foo", nc.uri, true);
so.connect(nc);
so.dosomething = new function(str) {

If you want to pass a function between SWFs, attach the function to the .data member of the SharedObject returned by SharedObject.getLocal/Remote, not the SharedObject itself.
So:
so.data.doSomething = yourFunction
...should work. I'm not exactly sure what you're trying to achieve, does this sound like a solution?

Related

StageWebView in an AS3-Script

I allreday read a lot of helpful stuff in that forum. Now it's my first time I ask for specific help.
I'm pretty new to Flash and have a problem I struggle for more then a week now. The most efficient and elegant way for my problem is to put a StageWebView-Call into an as-File.
That would be the plan:
In my flash-File: Show a PDF document "xyz" and put it on the stage.
I alreday tried it with Switch-Case - But then I have trouble to get rid of the PDF's.
That was my Idea:
First the new as-File...
package {
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public class mypdf {
public var MyWebView:StageWebView
public var file:String
public var pdf:File
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
MyWebView.loadURL(pdf.nativePath);
}
}
}
Than I want to call that in my flash-File...
stop();
var mynewpdf:mypdf = new mypdf("test.pdf");
Two erros are shown:
1120: Access of undefined property error ActualFile (at Marking #1)
1120: Access of undefined property error Stage (at Marking #2)
With a lot more work I could avoid the first error by defining a lot of different as-Scripts for each pdf.
My main problem is the second error.
It would be really nice if someone had any good ideas.
Bye,
Stephan
The second error means that you need to pass the stage to the web view. Either pass it to mypdf class as parameter, or make mypdf DisplayObject (extend Sprite for example) and add it to stage.
I'm not sure this will solve your issue anyways - I think StageWebView can simply display html. The PDF is displayed in your browser because an external plugin for that is launched.
In AIR the situation seems different: http://sujitreddyg.wordpress.com/2008/01/04/rendering-pdf-content-in-adobe-air-application/
StageWebView is wont support for nativePath, instead of using this, you can try with pdf.url. And StageWebView also having support for open .pdf files.
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
addChild( MyWebView );
MyWebView.loadURL(pdf.url);
}
Because, StageWebView will support file:/// format, but in nativePath we got C://.., so, this will help you. Or
Simply convert your StageWebView to Display object, and then added it to your container by using addElement().
You can convert it by,
var _view:SpriteVisualElement = new SpriteVisualElement();
_view.addChild( MyWebView);
this.addElement( view );
To test by, simply call this method in added_to_stage method to test if stage is having or not. This error will come if stage is not setted means also.

LoadVariables doesn't work as it is on Adobe website

I am trying to learn and get used to ActionScript. I have a simple task: to use the loadVariables(); function. I tried to use the Adobe's official reference, but as usual, the references code doesn't work as it is there. Here is my code:
import flash.events.MouseEvent;
function buttonClicked( evt:MouseEvent ) {
this.createEmptyMovieClip( "variables_mc", this.getNextHighestDepth() );
loadVariables("info.txt", variables_mc);
trace( variables_mc.username );
}
this.myButton.addEventListener( MouseEvent.CLICK, buttonClicked );
I get three errors from the debugger:
#1 1180: Call to a possibly undefined method loadVariables.
#2 1120: Access of undefined property variables_mc.
#3 1120: Access of undefined property variables_mc.
Here is the page where I have taken the code from: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000575.html
Can anyone help me?
Thank you, and a Happy New Year!
So as stated in the comments AS2.0 holds the methods createEmptyMovieClip() and loadVariables(), since AS3.0 does not contain these global functions you'll need to use the URL libraries instead.
For example:
//declare target movieclip
var variables_mc:MovieClip;
function buttonClicked( evt:MouseEvent ) {
variables_mc = new MovieClip();
getVariables();
}
function getVariables():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener( Event.COMPLETE, setVariables );
urlLoader.load( new URLRequest( "info.txt" ) );
}
function setVariables( e:Event ):void {
//then let's say you have userName=testUser in your info.txt
var urlVars:URLVariables = new URLVariables( e.target.data );
variables_mc.name = urlVars.userName;
}
Edit: I should explain what is happening here a little, URLLoader is the actionscript class used for loading files see: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html
Once the file has been loaded setVariables() is called and URLVariables acts as a dynamic class which sets properties to the files variables. So in this case our text file contains the userName=testUser. URLVariables runs a method called decode() from the constructor with the parameter e.target.data and creates all the necessary fields. Lastly you can access them, in our case urlVariables.userName.
Anyway I hope this helps, I've only used this a couple of times as it is usually best to create a class that handles all this for you.

error message asking for Properties onMetaData when load external vdo BUT, vdo can play anyway

I'm trying to figure out how to remove the message error and what cause it.
I created a code for loading external vdo to play on Flash. Coding it inside the Action Script panel is fine so I try to make it as a class. I moved all the code and put it in a class and it works fine too. But, the error message appeared ! even though the file could play correctly.
The error says:
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onMetaData. error=ReferenceError: Error #1069: Property onMetaData not found on vdoloader and there is no default value.
at vdoloader()
This is my code
package {
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
public class vdoloader extends Sprite {
var video;
var nc;
var ns;
public function vdoloader() {
// constructor code
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client=this;
video = new Video(550,400);
addChild (video);
video.attachNetStream(ns);
ns.play("westler.flv");
}
}
}
And then I tried to put something in that vdoloader(), it said something like: "expected 1, got 0."
It is exactly that - you are not handling the meta data event by implementing a function onMetaData. The client of your player is "this", so you should have a public function onMetaData in your class.
An please oh please, use an uppercase first letter for your class name...
EDIT:
In your code you are assigning this as the netstream's client (source: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html):
Associate a client property with an event handler to receive the data
object. Use the NetStream.client property to assign an object to call
specific data handling functions. The object assigned to the
NetStream.client property can listen for the following data points:
onCuePoint(), onImageData(), onMetaData(), onPlayStatus(),
onSeekPoint(), onTextData(), and onXMPData(). Write procedures within
those functions to handle the data object returned from the stream
during playback. See the NetStream.client property for more
information.
So now you just need to create a function onMetaData(md:Object) that should handle the event within the very same class, i.e. vdoloader (<=as you are passing this as the client). You can check the docs how to do it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#event:onMetaData. I think that Lee Brimelow had a tutorial about this on his site - http://www.gotoandlearn.com. (I am not sure about this but I guess it is worth a try if you are interested)

AS3 TypeError: Error #1009

I am trying to create a web application with multiple scenes, the error appears when I try to access the next scene with a button I created that contains multiple EventListeners for animation purposes.
The Button did bring me to the next scene, but the error still occurs. After tracing and debugging, the error seems to occur at the Mouse_Out event.
I am still very new to AS3, so can someone please explain to me where my code went wrong and if possible, correct the error for me or is there a better way of writting the code? Thanks in advance.
The Error Involved:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.transitions::Tween/setPosition()
at fl.transitions::Tween/set position()
at fl.transitions::Tween()
at Portfolio_fla::MainTimeline/about_btnOut()
My Code:
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
about_btn.buttonMode = true;
about_btn.mouseChildren = false;
about_btn.alpha = 0.3;
about_btn.addEventListener(MouseEvent.MOUSE_OVER, about_btnOver);
function about_btnOver(event:MouseEvent):void
{
var AboutAlphaOver:Tween = new Tween(about_btn,"alpha",Regular.easeIn,0.3,1,0.1,true);
}
about_btn.addEventListener(MouseEvent.MOUSE_OUT, about_btnOut);
function about_btnOut(event:MouseEvent):void
{
var AboutAlphaOut:Tween = new Tween(about_btn,"alpha",Regular.easeIn,1,0.3,0.1,true);
}
about_btn.addEventListener(MouseEvent.CLICK, about_btnClick);
function about_btnClick(event:MouseEvent):void
{
gotoAndPlay(1,"About");
}
Try to change your Tween code:
var AboutAlphaOut:Tween = new Tween(about_btn,"alpha",Regular.easeIn,1,0.3,0.1,true);
To:
var AboutAlphaOut:Tween = new Tween(event.currentTarget,"alpha",Regular.easeIn,1,0.3,0.1,true);
I'm not sure if it will work (I'm not too familiar with Flash IDE), but I think it's possible that you are getting error because other scene doesn't have a reference to a button. With event.currentTarget, you will search for reference in the event, so it should find it in any case.
BTW: You shouldn't name your variables starting by capital letter. That way you will more easily distinguish objects from classes.

compiler errors when loading in XML data into flash

Hi im trying to learn flash actionscript 3.0 basically i just want to learn how to put 1 simple picture into a flash document using XML so far ive got
<Gallery>
<IMAGE TITLE="Picture">Desert.jpg</IMAGE>
</Gallery>
thats my XML code DESERT is a picture on my laptop from the sample pictures
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myXML:XML;
var imageLoader:URLLoader = new URLLoader();
imageLoader.load(new URLRequest("pictest.xml"));
imageLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
trace(myXML);
this is my flash code when i run it i get a ton of errors which im confused about im new to this so any help would be appreciated also the myLoader is a textarea box with the instance name imageLoader
compiller errors im getting are :
A conflict exists with the definition myXML in namespace internal
A conflict exists with the definition imageLoader in namespace internal
duplicate function definition
Thanks in advance Rhys
What all these errors say is that you have written your code (same code you posted) more then once. In AS3 it is illegal (semi-legal in certain circumstances) to declare same variable or function more then once in the same scope - this is why you get the error.
So, check that other frames don't declare myXML, imageLoader and processXML again.