As3 loading blank image - actionscript-3

I'm having problems loading an image from an externall source (this was working but we changed the server to a https and made some adjustments).
Now, when I loaded the image, there are no errors, but the width, height are all 0. Also the image is blank.
I tried loading with this script some random internet image and it worked fine. However with the original - (https://www.lovemarks.co/images/be50fe37eac192fb7c0d17353f5ef993a.jpg) - it doesn't work.
var somethingLoaded:Boolean = true;
var actualPic:*;
var pictLdrX:Loader = new Loader();
var pictURLX:String = 'https://www.lovemarks.co/images/be50fe37eac192fb7c0d17353f5ef993a.jpg';
var pictURLReqX:URLRequest = new URLRequest(pictURLX);
var loaderContext:LoaderContext = new LoaderContext();
pictLdrX.load(pictURLReqX);
pictLdrX.contentLoaderInfo.addEventListener(Event.INIT , loadedRemember)
function loadedRemember(event:Event){
var targetLoader:Loader = Loader(event.target.loader);
var newmc:MovieClip = new MovieClip();
addChild(newmc);
newmc.addChild(targetLoader.content);
newmc.x = -targetLoader.width/2;
newmc.y = -targetLoader.height/2;
trace ('w+'+targetLoader.width);
trace ('h+'+targetLoader.height);
trace ('x+'+newmc.x);
trace ('y+'+newmc.y);
}

The way would do it is to first listen for Event.COMPLETE instead of Event.INIT:
pictLdrX.contentLoaderInfo.addEventListener(Event.COMPLETE , loadedRemember);
Then, instead of referencing the actual loader in your handler, just reference the image directly by first casting the Event.target.content as a Bitmap:
function loadedRemember(event:Event)
{
trace("loadedRemember()");
var newmc:Bitmap = Bitmap(event.target.content);
addChild(newmc);
// Your math to center may need to be adjusted
// to account for such a big image size
newmc.x = -newmc.width/2;
newmc.y = -newmc.height/2;
trace ('w+'+newmc.width + '\nh+'+newmc.height + '\nx+'+newmc.x + '\ny+'+newmc.y);
}
Additionally check for any security errors related to cross domain within your output window.

Related

How do I get images to update dynamically with a slider in actionscript 3?

Here is my dilemma. We use image stacks a lot here with alpha channels. I want to call up images in series using a slider. I am getting a concatenated string a pull up an image from my folder. But I can not get the images to load dynamically when I slide the slider bar.
I am using the frame number to change the file name to pull up the next frame. I can get the image to show up if I put the loader outside of the function, but when I put the loader in the function I get error after error saying the file cannot be located.
Here is the code as it currently stands :
var imgLoader = new Loader();
var str1 = String (".jpg");
var sliderValue:uint = mySlider.sliderKnob.x / 3;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
sliderValue = mySlider.sliderKnob.x / 3;
status_txt.text = "Slider position is: "+sliderValue;
fileText.text = "Sketch_0"+sliderValue+".jpg";
imgLoader.load( new URLRequest ( "Sketch_0"+sliderValue+".jpg"));
addChild(imgLoader);
setChildIndex(imgLoader, 0);
}
First thing up, you need to load one image without that slider stuff and see if it loads. Verify the paths if not. Relative paths, as you put them, will work relatively to the folder where SWF is (or where the HTML page, containing the SWF, is).
Second, relieve the heavy burden you are trying to put on the Flash Player.
addEventListener(Event.ENTER_FRAME, onFrame);
var nextName:String;
function onFrame(e:Event):void
{
var anIndex:int = mySlider.sliderKnob.x / 3;
var aName:String = "Sketch_0" + anIndex + ".jpg";
status_txt.text = "Slider position is: " + anIndex;
fileText.text = aName;
// Flash Player does not load images instantly,
// so lets request new image only if user stopped moving the slider.
if (aName == nextName)
{
loadNext(nextName);
}
else
{
nextName = aName;
}
}
var currentName:String;
var currentImage:Loader;
function loadNext(value:String):void
{
// If the requested file is the same as current, just ignore the request.
if (value == currentName) return;
currentName = value;
// Dispose of the previous image in a proper way.
if (currentImage)
{
removeChild(currentImage);
currentImage.unloadAndStop(true);
currentImage = null;
}
// Create a new one.
var aRequest:URLRequest = new URLRequest(currentName);
currentImage = new Loader();
currentImage.load(aRequest);
addChild(currentImage);
}

Loading more than one child image AS3

I'm trying to load 2 images in to my flash file but getting an error - new to this AS3 malarkey any help would be appreciated!
Getting error: 5 1151: A conflict exists with definition request in namespace internal.
var myImage:String = dynamicContent.Profile[0].propImage.Url;
var myImage2:String = dynamicContent.Profile[0].prop2Image.Url;
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
var myImageLoader2:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage2));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
if(this.currentFrame == 1) {
addChildAt(myImageLoader, 2);
}
if(this.currentFrame == 2) {
addChildAt(myImageLoader2, 2);
}
It's usually a good idea to move duplicate code into its own function instead of doing copy + paste:
function loadImage(file:String, x:Number, y:Number):StudioLoader{
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(file);
myImageLoader.load(request);
myImageLoader.x = x;
myImageLoader.y = y;
return myImageLoader;
}
addChild(loadImage(enabler.getUrl(myImage1),17,0));
addChild(loadImage(enabler.getUrl(myImage2),17,0));
That's not just giving you better structured code but also fixes your duplicate variable definition issue because what's defined locally inside a function stays inside the function.
This might provide some insight:
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html
You can't create a new variable with the same name as an existing one, in your case I'm speaking about your URLRequest request, so to avoid this type of error you can do like this :
var myImageLoader2:StudioLoader = new StudioLoader();
// assing a new URLRequest to an existing var
request = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Or :
var myImageLoader2:StudioLoader = new StudioLoader();
// create a new URLRequest var, so the name should be different
var request2:URLRequest = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request2);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Additionally, I notice in the second block that you declare myImageLoader2 but you then use the original myImageLoader to do the load request. So even if you do declare a new URLRequest, you wont get both images loaded.
Akmozo's solution has this corrected.

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader

What is wrong with my JPGEncoder

Here is my code
if (event.target.content is Bitmap)
{
infotext.text = "got something";
var image:Bitmap = Bitmap(event.target.content);
var bitmapData:BitmapData = image.bitmapData;
this.addChild(image);
var j:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = new ByteArray();
bytes=j.encode(bitmapData);
}
else
{
throw new Error("What the heck bob?");
}
When I run a debug session everything works fine till it reaches to the line
bytes=j.encode(bitmapData);
after that nothing happens and my program just goes into limbo Please help
I made changes to the code as per your suggestion
var myImage:Bitmap =Bitmap(e.target.content);
var bitmapData:BitmapData = new BitmapData(myImage.width,myImage.height,true,0xffffffff);
bitmapData.draw(myImage);
var encoder:JPGEncoder = new JPGEncoder();
var bytes:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);
but it gets stuck again after
var bytes:ByteArray = encoder.encode(bitmapData);
What am i doing wrong here?
Pretty sure I ran into this same issue a long time ago so I pulled up my code from then that I got to work.
After reviewing the code the only thing I see different is I construct the bitMapData first and assign the image via the load function.
So I think your issue is with the construction of the bitmapData var.
The following code block was cut down from a function I created that did a lot of other image manipulation.
So basically it is a cut down version and untested but it should work.
var myImage:Image = new Image();
myImage.load( Bitmap(event.target.content) );
var bitmapData:BitmapData = new BitmapData(myImage.width, myImage.height, true, 0xffffffff );
bitmapData.draw(myImage);
var encoder:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(bitmapData);
this.addChild(myImage);
Just in case anyone runs into this, Make sure you are using the bytearray JPEGEncoder class, it's faster: http://www.bytearray.org/?p=775
the as3core is JPGEncoder not JPEGEncoder. JPG vs JPEG... this got me also.

Unloading a Loader with actionscript 3

Hello and thank you very much for looking at this. I've spent too many hours struggling.
The code below loads a slideshow of four images, along with thumbnails for those images. It works fine.
I've added a button called "invis_button", that when pressed is supposed to remove the 3 loaders that make up the slideshow, using the removeChild command for each loader.
But this is the problem, there are 3 loaders involved in the slide-show. The removeChild command successfully removes one of the loaders (named "loader3"), but not the other two ("container3", and "thumbLoader3"). It returns an error stating "access of undefined property thumbLoader3" or "Container3".
Can someone tell me why this is ? Or better still, how to make that button (invis_button) unload the entire slide-show.
var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"];
var thumbX3:Number = -375;
var thumbY3:Number = 220;
var loader3:Loader = new Loader();
loader3.load(new URLRequest("assets/ad_bona1.jpg"));
addChild(loader3);
loader3.alpha = 0;
loadThumbs3();
function loadThumbs3():void
{
var thumbLoader3:Loader;
var container3:Sprite = new Sprite();
addChild(container3);
container3.buttonMode = true;
for(var i3:uint = 0; i3 < images3.length; i3++)
{
thumbLoader3 = new Loader();
thumbLoader3.load(new URLRequest("assets/thumbs/" + images3[i3]));
thumbLoader3.x = thumbX3;
thumbLoader3.y = thumbY3;
thumbX3 += 85;
container3.addChild(thumbLoader3);
thumbLoader3.addEventListener(MouseEvent.CLICK, thumbClicked3);
}
}
function thumbClicked3(event:MouseEvent):void
{
var path3:String = event.currentTarget.contentLoaderInfo.url;
path3 = path3.substr(path3.lastIndexOf("/") + 1);
loader3.load(new URLRequest("assets/" + path3));
}
///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason
invis_button.addEventListener(MouseEvent.CLICK, unload_loaders);
function unload_loaders(event:MouseEvent):void{
removeChild(loader3);
removeChild(thumbLoader3);
removeChild(container3);
}
Not sure if this is the entire reason behind what you're observing... but for starters, "thumbloader3" and "container3" are scoped locally to the loadThumbs3() method, which means once you finish executing the function, Flash's handles to those objects are lost (not to mention being in an entirely different scope)... try creating class-level properties for those two. Once that's done you should be able to successfully remove them from the stage later on.
I hope that you're also properly destroying your objects, and for the sake of brevity you just chose to omit that code above.
I've edited the code you had above & put the properties into the proper scope. (the multiple copies of thumbLoader3 are now collected inside of a vector (specialized array) so that they can be properly addressed when it comes time to destroy them)
I also wrote you a proper destroy method. ;)
I haven't tried it on my own machine, but give it a spin & see how it goes.
var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"];
var thumbX3:Number = -375;
var thumbY3:Number = 220;
// begin new instance properties..
// created a new property, allowing you to group (and hold on to) the multiple thumbLoaders
var thumbLoader3Vector:Vector.<Loader> = new Vector.<Loader>();
var container3:Sprite;
// end new instance properties
var loader3:Loader = new Loader();
loader3.load(new URLRequest("assets/ad_bona1.jpg"));
addChild(loader3);
loader3.alpha = 0;
loadThumbs3();
function loadThumbs3():void
{
// this is where container3 used to be declared
container3 = new Sprite();
addChild(container3);
container3.buttonMode = true;
for(var i3:uint = 0; i3 < images3.length; i3++)
{
var tPtr:int = thumbLoader3Vector.length;
thumbLoader3Vector.push(new Loader());
// this is where thumbLoader3 used to be declared & instantiated
thumbLoader3Vector[tPtr].load(new URLRequest("assets/thumbs/" + images3[i3]));
thumbLoader3Vector[tPtr].x = thumbX3;
thumbLoader3Vector[tPtr].y = thumbY3;
thumbX3 += 85;
container3.addChild(thumbLoader3Vector[tPtr]);
thumbLoader3Vector[tPtr].addEventListener(MouseEvent.CLICK, thumbClicked3);
}
}
function thumbClicked3(event:MouseEvent):void
{
var path3:String = event.currentTarget.contentLoaderInfo.url;
path3 = path3.substr(path3.lastIndexOf("/") + 1);
loader3.load(new URLRequest("assets/" + path3));
}
///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason
invis_button.addEventListener(MouseEvent.CLICK, unload_loaders);
function unload_loaders(event:MouseEvent):void{
// since the thumbLoader3 Loaders are children of container3 in the display list, we need to remove them first
for(var $i:uint = 0;$i<thumbLoader3Vector.length;$i++)
{
removeChild(thumbLoader3Vector[$i]);
// also make sure you remove the listener, so that the object will be picked up by garbage collection
thumbLoader3Vector[$i].removeEventListener(MouseEvent.CLICK, thumbClicked3);
}
// and then just set the entire vector to null
thumbLoader3Vector = null;
// remove the loader3 object & set it to null
removeChild(loader3);
loader3 = null;
// remove the container3 object & set it to null
removeChild(container3);
container3 = null;
}