as3 preloader stop loaded swf from caching? - actionscript-3

sorry if this has been answered elsewhere but I can't find anything matching.
I have two swfs; a preloader (let's call it A) and some content (B). A loads B and adds it as a child. Everything is working beautifully (you can even see it here).
There's just one little problem I'm having. Normally when loading, say an image, into flash using the URLLoader Class, I add + "?" + new Date().getTime() to the URLRequest to force flash to load the latest version of the target, in other words to stop it using a cached version. Now, when I try to do this to the Loader that adds B to A, it can't find the URL (#2035 URL not found). So my question is: is what I'm trying to do possible, or should I take another approach to stopping B from caching?
Here's the preloader code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
}
addChild(perc_text);
main_movie.load(new URLRequest("claudia_summers.swf"+ "?" + new Date().getTime()));
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}

Not sure how/why, but adding String() around new Date().getTime() and giving main_movie an IOErrorEvent listener resolves the issue. I think I have a bug somewhere, as this surely should have worked without these additions, no? Anyway thanks for all your help!
Working code:
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.net.URLLoader;
import flash.text.TextField;
import flash.events.IOErrorEvent;
public class claude_loader extends MovieClip
{
public var main_movie:Loader = new Loader();
public var rss_loader:URLLoader;
private var perc_text:TextField = new TextField();
public function claude_loader()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
with (perc_text)
{
x = this.stage.stageWidth / 2;
y = this.stage.stageHeight / 2;
textColor = 0xFFFFFF;
}
addChild(perc_text);
main_movie.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,catch_error);
main_movie.load(new URLRequest("claudia_summers.swf" + "?" + String(new Date().getTime())));
function catch_error(e:IOErrorEvent):void
{
main_movie.load(new URLRequest("claudia_summers.swf"));
perc_text.text = "Failed non-cache load"
}
main_movie.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
main_movie.contentLoaderInfo.addEventListener(Event.COMPLETE,on_complete);
}
public function load_progress(e:ProgressEvent):void
{
var perc:Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
perc_text.text = "Loading " + perc + "%";
if (e.bytesLoaded == e.bytesTotal)
{
perc_text.text = "Loading done";
}
}
public function on_complete(e:Event):void
{
rss_loader = new URLLoader(new URLRequest("http://news.sulsc.org/feed"));
rss_loader.addEventListener(Event.COMPLETE,rss_complete);
perc_text.text = "Loading RSS";
}
public function rss_complete(e:Event):void
{
MovieClip(main_movie.content).rss_xml = XML(e.target.data);
addChild(main_movie);
}
}
}

Related

Actionscript 3 - Netstream get published name

I have a netstream and I want to publish a stream with its own name, but multiple streams might be published at the same time. What causes the conflict is that all the streams have the same name because I can't figure out how to get other streams' names from other clients and play them, and publish my stream with a different name.
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.media.Microphone;
import flash.media.Sound;
var Mic:Microphone;
var aud:Sound;
var nc:NetConnection = new NetConnection;
nc.client = this;
var istream:NetStream;
var ostream:NetStream;
Security.showSettings("2");
if(Microphone.names.length <= 0) {
// no microphone
} else {
Mic = Microphone.getMicrophone();
Mic.rate = 44;
Mic.setUseEchoSuppression(true);
Mic.setLoopBack(false);
Mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
Mic.addEventListener(StatusEvent.STATUS, statusHandler);
}
icon_btn.addEventListener(MouseEvent.MOUSE_DOWN, talkDown);
icon_btn.addEventListener(MouseEvent.MOUSE_UP, talkUp);
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.addEventListener(NetGroup.MulticastStream.PublishNotify
nc.connect("rtmp://server.xxxxxxxxxxxxx.us:1935/oflaDemo");
function talkDown(e:MouseEvent):void {
ostream.publish("mcp");
istream.receiveAudio(false);
}
function talkUp(e:MouseEvent):void {
ostream.close();
istream.receiveAudio(true);
timer.stop();
}
function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code)
ostream = new NetStream(nc);
istream = new NetStream(nc);
istream.play("mcp");
ostream.attachAudio(Mic);
}
function activityHandler(event:ActivityEvent):void {
trace("activityHandler: " + event);
}
function statusHandler(event:StatusEvent):void {
trace("statusHandler: " + event);
}
By reading the code, I'm pretty sure you figured out what the conflict is.

AS3 Preloader not updating screen

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);
}
}
}

AS3 to get children in Gestureworks TouchSprite working

Recently just trying around with the Gestureworks multitouch.
Here I have some problem about button inside a TouchSprite, below is my code:
package
{
import com.gestureworks.core.GestureWorks;
import com.gestureworks.core.TouchSprite;
import com.gestureworks.events.GWGestureEvent;
import com.gestureworks.events.GWTouchEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Stage;
import flash.events.Event;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
public class Main extends GestureWorks
{
private var myDisplay:TouchSprite;
public function Main():void
{
super();
key = "xxx";
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
}
override protected function gestureworksInit():void
{
myDisplay = new TouchSprite();
myDisplay.gestureReleaseInertia = true;
myDisplay.gestureEvents = true;
myDisplay.disableNativeTransform = false;
myDisplay.disableAffineTransform = false;
myDisplay.mouseChildren = true;
myDisplay.gestureList = {"n-drag":true,"n-scale":true};
myDisplay.addChild(boliviaMap);
myDisplay.addChild(mapPink);
addChild(myDisplay);
myDisplay.addEventListener(GWGestureEvent.DRAG, gestureDragHandler);
myDisplay.addEventListener(GWGestureEvent.SWIPE, gestureSwipeHandler);
mapPink.addEventListener(TouchEvent.TOUCH_TAP, tapPink);
}
private function tapPink(e:TouchEvent):void
{
trace("PINK");
indicatorTxt.text = "PINK";
}
private function gestureDragHandler(event:GWGestureEvent):void
{
trace("g drag: ", event.value.dx, event.value.dy);
event.target.$x += event.value.dx;
event.target.$y += event.value.dy;
indicatorTxt.text = "g drag: " + event.value.dx + " " + event.value.dy;
}
}
}
apparently the myDisplay.mouseChildren = true; will make myDisplay not able to drag or scale anymore.
Before adding your bigMap button set the mouseChildren property of myDisplay to true:
myDisplay.mouseChildren = true;
This should pass mouseEvents to objects inside.
Also, depending on the effect you want (I'm assuming this is for a multi-touch screen of somesort), I'd suggest you should probably use a TouchEvent.TOUCH_TAP on bigMap instead of MouseEvent.CLICK(MouseEvents arent multi-touch and won't be responsive if there is some kind of touch on another part of the screen at the same time).
Edit:
Assuming boliviaMap and mapPink aren't TouchMovieClips or TouchSprites then do something like this:
myDisplay.graphics.beginFill(0x555555, 1);
myDisplay.graphics.drawRect(0,0,500,500);
myDisplay.graphics.endFill();
Then add your child assuming boliviaMap and mapPink are less than 500x500px:
myDisplay.addChild(boliviaMap);
myDisplay.addChild(mapPink);
Edit 2- A working example(for me anyways) combining TouchEvents with GWEvents:
package {
import flash.display.MovieClip;
import com.gestureworks.core.GestureWorks;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import com.gestureworks.core.TouchSprite;
import flash.display.Sprite;
import flash.events.TouchEvent;
public class Main extends GestureWorks {
public function Main() {
super();
key = "INSERT YOUR GW KEY HERE";
Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
}
override protected function gestureworksInit():void
{
trace("GestureWorks has initialized");
var myTouchSprite:TouchSprite = new TouchSprite();
myTouchSprite.graphics.beginFill(0x555555,1);
myTouchSprite.graphics.drawRect(0,0,100,100);
myTouchSprite.graphics.endFill();
myTouchSprite.gestureReleaseInertia = false;
myTouchSprite.gestureEvents = true;
myTouchSprite.disableNativeTransform = false;
myTouchSprite.disableAffineTransform = false;
myTouchSprite.gestureList = {"n-drag":true, "n-rotate":true};
myTouchSprite.mouseChildren = true;
var myTappableSprite:Sprite = new Sprite();
myTappableSprite.graphics.beginFill(0xAD3059,1);
myTappableSprite.graphics.drawRect(30,30,40,40);
myTappableSprite.graphics.endFill();
myTappableSprite.addEventListener(TouchEvent.TOUCH_TAP, tappedThis);
//myTouchSprite.addChild(myTappableSprite);
myTouchSprite.addChild(myTappableSprite);
addChild(myTouchSprite);
}
public function tappedThis(event:TouchEvent){
trace("You tapped this");
}
}
}
I should also add that this will only work on a screen/device that triggers TouchEvents(if you're clicking myTappableSprite with a mouse it won't trigger the event).

NetStream Info Returning 0's (Icecast Stream)

I've searched high and low for a fix to this issue, but cannot find ANYTHING on why almost all the properties are being returned as 0.
I am using FLV wrapping to pull a live audio stream from Icecast (since Adobe, 15 years later, still haven't fixed their live audio memory leak issue). It works great, everything functions perfectly.
However, I'm wanting to create a bandwidth monitor (for my iPhone port, and for my normal Flash player)... But whenever I retrieve netStreamInfo it returns as 0! For dataBytesPerSecond, audioBytesPerSecond, byteCount, dataByteCount, nearly EVERY SINGLE PROPERTY returns 0. I have this run on a 1-second timer.
Here's the total info output:
currentBytesPerSecond=0
byteCount=0
maxBytesPerSecond=0
audioBytesPerSecond=0
audioByteCount=0
videoBytesPerSecond=0
videoByteCount=0
dataBytesPerSecond=0
dataByteCount=0
playbackBytesPerSecond=16296.296296296296
droppedFrames=0
audioBufferLength=0.072
videoBufferLength=0
dataBufferLength=0
audioBufferByteLength=1540
videoBufferByteLength=0
dataBufferByteLength=0
srtt=0
audioLossRate=0
videoLossRate=0 Data Bytes Per Second
That output was about 5 minutes in. I noted the playBackBytesPerSecond never changed, and the audioBufferByteLength liked to switch between 1540 and 23xx randomly.
Can anyone pleaaase help me out here?
My actionscript:
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.events.NetStatusEvent;
import flash.utils.Timer;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.display.Loader;
import flash.errors.IOError;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.NetStreamInfo;
import flash.utils.ByteArray;
import flash.media.*
public class soundContainer extends Sprite
{
public var slider:SliderMC = new SliderMC();
private var _video:Video;
private var _stream:NetStream;
private var _playbackTime:TextField;
private var _duration:uint;
private var _timer:Timer;
private var _soundChannel:SoundChannel;
public var audioTransform:SoundTransform = new SoundTransform();
public var _URL:String;
public var flvUrl:String = "s";
public var dragging:Boolean = false;
public var rectangle:Rectangle = new Rectangle(0,0,100,0);
private var ba:ByteArray;
private var bn;
public function soundContainer() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void
{
ba = new ByteArray();
slider.x = 73.85;
slider.y = 10.95;
addChild(slider);
slider.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
_timer = new Timer(1000);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
public function dragIt(e:MouseEvent):void
{
slider.slider_mc.startDrag(false, rectangle);
dragging = true;
slider.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);
}
public function dropIt(e:MouseEvent = null):void
{
if (dragging)
{
slider.slider_mc.stopDrag();
dragging = false;
}
}
public function adjustVolume(e:Event):void
{
var vol:Number = slider.slider_mc.x / 100;
var st:SoundTransform = new SoundTransform(vol);
SoundMixer.soundTransform = st;
}
public function playMyFlv(flvUrl)
{
_URL = flvUrl;
_video = new Video();
var connection:NetConnection = new NetConnection();
connection.connect(null);
_stream = new NetStream(connection);
_stream.soundTransform = audioTransform;
_stream.play(flvUrl);
var Client:Object = new Object();
_stream.client = Client;
_video.attachNetStream(_stream);
addChild(_video);
}
public function stopMyFlv()
{
SoundMixer.stopAll();
trace("stop");
try
{
_stream.close();
}
catch (error:IOError)
{
}
}
private function onNetStatus(e:NetStatusEvent)
{
}
private function onTimer(t:TimerEvent):Number
{
trace(_stream.info + " Data Bytes Per Second");
}
public function onIOError(e:IOError)
{
trace("Failed to load");
}
}
}

add a swf to play on top of another swf?

I need to have a swf play on top of another swf after a certain amount of time.
can someone provide a code to help me out?
Thanks in advance!
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.utils.getTimer;
public class Test extends Sprite
{
private var _loader : Loader;
private var _loadedSWF : MovieClip;
private var _timer : Timer;
public function Test()
{
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.load(new URLRequest("OtherSWF.swf"));
_timer = new Timer(1000, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
}
private function onLoadComplete(event : Event) : void
{
trace("onLoadComplete: " + getTimer());
// add to displaylist
_loadedSWF = event.currentTarget.content;
_loadedSWF.stop();
_loadedSWF.visible = false;
addChild(_loadedSWF);
// start timer
_timer.start();
}
private function onTimerComplete(event : TimerEvent) : void
{
trace("onTimerComplete: " + getTimer());
_loadedSWF.visible = true;
_loadedSWF.play();
}
}
}
http://kb2.adobe.com/cps/141/tn_14190.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html