So I have a class that should send data to an external php file.
Here is my code:
package {
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.system.Security;
import flash.system.Capabilities;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.AsyncErrorEvent;
import flash.events.SecurityErrorEvent;
public dynamic class myClass extends Sprite {
public static function showAd(mainParent:Sprite, devId:int, adWidth:int, adHeight:int, posX:int, posY:int):myClass {
var vars:URLVariables = new URLVariables();
vars["sandboxType"] = Security.sandboxType;
vars["ver"] = Capabilities.version;
vars["devid"] = devId;
// Init connection to the server
var zadsReq:URLRequest = new URLRequest("http://...");
zadsReq.contentType = "application/x-www-form-urlencoded";
zadsReq.method = URLRequestMethod.POST;
zadsReq.data = vars;
var loader:Loader = new Loader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
self.addChild(loader);
loader.addEventListener(Event.COMPLETE, dataLoaded1);
loader.load(zadsReq);
return self;
}
public static function dataLoaded1(evt:Event):void {
trace('ok');
}
}
}
But I get this error:
1119: Access of possibly undefined property dataFormat through a reference with static type flash.display:Loader.
As you can see, I have already imported flash.net.URLLoaderDataFormat. So what could be the issue here?
The issue is that you are using a Loader object when what you need is URLLoader. For your reference, Loader is used to load local files (swf, gif, jpg, etc).
So, change the relevant part of your code to use a URLLoader rather than just Loader:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, dataLoaded1);
loader.load(zadsReq);
Related
I'm trying to load data from a website in my AS3 program. However, urlLoader.load() never finishes, yet the program runs without any errors.
Here's my code:
Main.as:
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import ttTextField;
public class Main extends Sprite
{
private var textField:ttTextField = new ttTextField("", false);
public function Main():void
{
var urlRequest:URLRequest = new URLRequest("http://itch.io/api/1/API_KEY_REMOVED_FOR_SECURITY/my-games");
urlRequest.method = URLRequestMethod.GET;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
urlLoader.load(urlRequest);
stage.addChild(textField);
}
private function completeHandler(event:Event):void
{
textField.text = event.target.data;
}
}
ttTextField.as:
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormatAlign;
import flash.text.TextFormat;
public class ttTextField extends TextField
{
[Embed(source = "../files/Fixedsys500c.ttf", embedAsCFF = "false", fontName = "fixedSys")] private var fontClass:Class;
private var textFormat:TextFormat = new TextFormat("fixedSys", 24);
public function ttTextField(string:String, centered:Boolean)
{
if (centered)
{
textFormat.align = TextFormatAlign.CENTER;
}
else
{
textFormat.align = TextFormatAlign.LEFT;
}
string = string;
this.autoSize = TextFieldAutoSize.LEFT;
this.mouseEnabled = false;
this.embedFonts = true;
this.textColor = 0xFFFFFF;
this.defaultTextFormat = textFormat;
this.text = string;
}
}
ttTextField can display regular strings, so it doesn't seem like this class is the issue. I just included it for completion's sake.
I'm also building and running using Sublime Text 2. My sublime-build file is as follows:
{
"cmd": ["C:\\Users\\Dan\\Documents\\flex_sdk_4.6\\bin\\mxmlc.exe", "-output=C:\\Users\\Dan\\TTTT\\TTTT.swf", "-default-background-color=#00FF00", "-default-size=800,600", "C:\\Users\\Dan\\TTTT\\src\\Main.as", "-static-link-runtime-shared-libraries=true"],
"file_regex": "(.*)[(](\\d+)[)]:(?: col: (?:\\d+))? *Error: (.*)",
"selector": "source.actionscript",
"variants":
[
{
"name": "Run",
"shell": true,
"cmd": ["C:\\Users\\Dan\\TTTT\\TTTT.swf"]
}
]
}
I've tried running the program in both Flash Player and Google Chrome. Every time the output is just a blank screen.
How would I get my URLLoader to actually load the URL? I can provide more information if needed. Thank you for reading.
I think that simply your Main class should extend from MovieClip and not Sprite :
public class Main extends MovieClip
{
// ...
}
And to debug your project in the browser, you can use a flash player debug version which you can download from here.
Hope that can help.
Im Working on Air Application and heres my Class
package
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.StageVideoAvailabilityEvent;
import flash.geom.Rectangle;
import flash.media.StageVideo;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
public class presentation extends Sprite
{
private const VIDEO_FILE_URL:String = "assets/Presentation_Demo_02.mp4";
private var video:Video;
private var stageVideo:StageVideo;
private var nc:NetConnection;
private var ns:NetStream;
private var streamClient:Object;
public function presentation()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//stage.displayState = StageDisplayState.FULL_SCREEN;
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(event:Event):void
{
trace("All Works");
initStream();
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(Event.RESIZE, stageResize);
addEventListener(Event.ENTER_FRAME,update);
}
private function initStream():void
{
streamClient = new Object();
streamClient.onMetaData = onMetaData;
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = streamClient;
addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY,onChange);
}
private function onChange(event:StageVideoAvailabilityEvent):void
{
trace(event.availability);
}
private function stageResize(event:Event):void
{
}
private function update(event:Event):void
{
}
public function onMetaData(e:Object):void
{}
}
}
on the line where addEventListener for StageVideoAvailabilityEvent i got Error in Flash Builder
Type was not found or was not a compile-time constant: StageVideoAvailabilityEvent.
what can i do
Be sure that you are using a version of Adobe AIR that supports StageVideo. (I recommend you to download the latest version, 17).
Update your Adobe AIR Application Description file to the respective AIR version. (in this case 17)
<application xmlns="http://ns.adobe.com/air/application/17.0">
Also, be sure that you add an extra compiler argument to indicate the respective SWF version (fro AIR 17, should be 28:
-swf-version=28
Call:
import fl.video.*
import NSPv4;
var playVid:NSPv4 = new NSPv4();
playVid.playVideo(this.parent);
Class:
package
{
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.events.*;
import flash.net.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import fl.video.*;
public class NSPv4 extends MovieClip
{
var __PARENT;
var __PARENT1;
public function playAudio(_PARENT):void
{
__PARENT = _PARENT;
var audioLabel:String = _PARENT.currentLabel;
var audioLabelNum:String = audioLabel.replace("sct","audio/");
audioLabelNum += ".mp3";
var vo:Sound = new Sound(new URLRequest(audioLabelNum));
var channel:SoundChannel = new SoundChannel();
channel = vo.play();
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
public function playVideo(_PARENT1):void
{
__PARENT1 = _PARENT1;
var videoLabel:String = _PARENT1.currentLabel;
var videoLabelNum:String = videoLabel.replace("sct","audio/");
videoLabelNum += ".f4v";
var videoPlayer:FLVPlayback = new FLVPlayback();
videoPlayer.source = videoLabelNum;
_PARENT1.addChild(videoPlayer);
}
public function onPlaybackComplete(event:Event):void
{
SoundMixer.stopAll();
MovieClip(__PARENT).nextFrame();
}
}
}
Can call the playAudio with no problems but when I try playVideo() I get the following error:
1195 Attempted access of inaccessible method playVideo through a reference with static type NSPv4
I set up a separate class for the video to stand alone without the playAudio() function and this works too.
Most confused!
I've been struggling with a Flash preloader. I just want it to update the text on the screen with the current percentage. Now it basically works as the trace outputs the correct percentages, but it won't update the textfield I have on the screen. Once the trace gets to 100% however, the code does output "100" on the screen, but not until it's all loaded. I don't have the Flash IDE and am just using pure Actionscript with FlashDevelop. Here's my code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Loader;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.display.Loader;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
public class Main extends Sprite
{
public var myLoader:Loader = new Loader();
public var image:String = "tmp/Avengers-poster.jpg";
private var Title:TextField;
private var txt:String;
private var Form:TextField;
public function Main():void {
textbox("This is the title","box",100,100,200,30);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);
var fileRequest:URLRequest = new URLRequest(image);
myLoader.load(fileRequest);
}
public function onProgressStatus(e:ProgressEvent):void {
// this is where progress will be monitored
var perc:int = Math.ceil((e.bytesLoaded/e.bytesTotal)*100);
txt = perc.toString();
Title.text = txt;
addChild (Title);
trace(perc);
}
public function onLoaderReady(e:Event):void {
// the image is now loaded, so let's add it to the display tree!
addChild(myLoader);
}
private function textbox (title_str:String,form_name:String,x:int,y:int,width:int,height:int):void {
Title = new TextField ();
Title.text = title_str;
Title.selectable = false;
Title.setTextFormat (new TextFormat ("Arial", 12, 0x777777, true));
txt = ".";
Title.x = x;
Title.y = y;
addChild (Title);
}
}
}
Thanks for your help.
Darryl
This will work. Don't recreate the TextField on each status. You only need to addChild once and update the reference to it.
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
public class Main extends Sprite
{
public var myLoader:Loader = new Loader();
public var image:String = "http://apod.nasa.gov/apod/image/0605/titan5km_huygens_big.jpg";
private var Title:TextField;
private var Form:TextField;
public function Main():void {
createTextField("This is the title","box",100,100,200,30);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);
var fileRequest:URLRequest = new URLRequest(image);
myLoader.load(fileRequest);
}
public function onProgressStatus(e:ProgressEvent):void {
// this is where progress will be monitored
var perc:int = Math.ceil((e.bytesLoaded/e.bytesTotal)*100);
Title.text = perc.toString();
}
public function onLoaderReady(e:Event):void {
// the image is now loaded, so let's add it to the display tree!
addChild(myLoader);
}
private function createTextField (title_str:String,form_name:String,x:int,y:int,width:int,height:int):void {
Title = new TextField();
Title.text = title_str;
Title.selectable = false;
Title.setTextFormat (new TextFormat ("Arial", 12, 0x777777, true));
Title.text = ".";
Title.x = x;
Title.y = y;
addChild (Title);
}
}
}
I am streaming flv file trough vlc media player running as http streaming server. So I am able to get the bytes but how to decode them?
Shoud I take float from the URLStream with readFloat() or plain bytes with readBytes()?
So long as only flv's are streaming, you will want to use the NetStream.appendBytesAction and NetStream.appendBytes for http streaming flv playback. Checkout the following blog post at ByteArray.org and also the quick example below:
AppendBytes
Playback Initialization:
var video:Video = new Video(width, height);
var video_nc:NetConnection = new NetConnection();
var video_ns:NetStream = new NetStream();
video_nc.connect(null);
video_ns.play(null);
video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
video.attachNetStream(video_ns);
ProgressEvent.PROGRESS Handler:
video_ns.appendBytes(bytesAvailable);
This is essentially the jist of it, bytesAvailable will represent the read bytes from the event data buffer. A full example is listed below:
package
{
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.ProgressEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
[SWF(width="1280", height="720")]
public class NetStreamAppendBytes extends Sprite
{
var video:Video;
var video_nc:NetConnection;
var video_ns:NetStream;
var video_stream:URLStream;
public function NetStreamAppendBytes()
{
super();
video_nc = new NetConnection();
video_nc.connect(null);
video_ns = new NetStream(video_nc);
video_ns.client = this;
video_ns.addEventListener(NetStatusEvent.NET_STATUS, ns_statusHandler);
video = new Video(1280, 720);
video.attachNetStream(video_ns);
video.smoothing = true;
video_ns.play(null);
video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
video_stream = new URLStream();
video_stream.addEventListener(ProgressEvent.PROGRESS, videoStream_progressHandler);
video_stream.load(new URLRequest("path_to_flv"));
addChild(video);
}
private function ns_statusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
}
private function videoStream_progressHandler(event:ProgressEvent):void
{
var bytes:ByteArray = new ByteArray();
video_stream.readBytes(bytes);
video_ns.appendBytes(bytes);
}
}
}
Best of luck!