I have done a simple code for a slideshow that dynamicly loads images from a subfolder, depending on what is listet in a text file.
the code:
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class slideshow extends MovieClip {
public var listLoader:URLLoader;
public var newImgList:Array;
public var imgX:int = 0;
public var container:MovieClip;
public function slideshow() {
container = new MovieClip();
stage.addChild(container);
listLoader = new URLLoader(new URLRequest("./slideshow.txt"));
listLoader.addEventListener(Event.COMPLETE, initImages);
}
public function initImages(event:Event):void {
var imgList:Array = listLoader.data.replace(/^\s+/,"").replace(/\s+$/,"").split(/\s+/);
newImgList = new Array();
for(var line:int = 0; line < imgList.length; line++ ) {
if(imgList[line].indexOf(".png") != -1 || imgList[line].indexOf(".jpg") != -1) {
newImgList.push(imgList[line]);
}
}
loadImage();
}
public function loadImage():void {
for(var loaderNum = 0; loaderNum < newImgList.length; loaderNum++) {
var imgLoader = new Loader();
imgLoader.load(new URLRequest("./slideshow/" + newImgList[loaderNum]));
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgInit);
}
}
public function imgInit(event:Event):void {
var imgBmp:BitmapData = event.target.content.bitmapData;
var img:Bitmap = new Bitmap(imgBmp);
img.height = 150;
img.scaleX = img.scaleY;
img.y = 0;
img.x = imgX;
imgX = (imgX + img.width + 10);
container.addChild(img);
}
}
well actually it works fine for me except for that the images are displayed in a almost random order.
i guess the code is loading some images too slow, so some of them are added to the movieclip although they are loaded after the one that should go next.
so what i mean :
1.png is loaded
1.png is added
2.png is loaded
3.png is loaded
3.png is added
2.png is added
so my question:
is there any other propper/better way to make that slideshow load images from a textfile where just the full names of the images ( that are in a subfolder ) are listet ?
thanks for any suggestions.
g.r.
Queue your image loading to have them ordered.
Rough example:
var queue:Array = []; // Populated from your text/whatever file.
// If you want the images to load from first to last you will need
// to use queue.reverse() once you get the filenames.
/**
* Beings loading the next image in queue.
* Ignored if the queue has no remaining items.
*/
function loadNext():void
{
if(queue.length > 0)
{
var imgSrc:String = queue.pop();
var ldr:Loader = new Loader();
ldr.load(new URLRequest(imgSrc));
ldr.addEventListener(Event.COMPLETE, _done);
}
}
/**
* Called once a Loader instance has finished loading a resource.
* #param e Event.COMPLETE.
*/
function _done(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, _done);
container.addChild(e.target as DisplayObject);
// Begin loading the next image in queue.
loadNext();
}
Related
i'm building a flash desktop App where the user clicks on a button and opens a SWF file (a game) in a new window, i used a NativeWindow to achieve it, i did the folowing:
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
windowOptions.type = NativeWindowType.NORMAL;
var newWindow:NativeWindow = new NativeWindow(windowOptions);
newWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
newWindow.stage.align = StageAlign.TOP_LEFT;
newWindow.bounds = new Rectangle(100, 100, 2000, 800);
newWindow.activate();
var aLoader:Loader = new Loader;
aLoader.load(new URLRequest(path));
newWindow.stage.addChild(aLoader);
the result is this:
the game doesn't take the whole space available. When i change the "NO_SCALE" to "EXACT_FIT":
newWindow.stage.scaleMode = StageScaleMode.EXACT_FIT;
i got this:
it only shows the top-left corner of the game. I also tried "SHOW_ALL" and "NO_BORDER". I got the same result as "EXACT_FIT".
When i open the SWF file of the game separatly it's displayed normally:
any idea how can i display the SWF game as the image above?
The best solution for that is to have the dimensions of your external game in pixel. you have to stretch the loader to fit it in your stage. if you are try to load different games with different sizes to your stage, save the swf dimensions with their URL addresses.
var extSWFW:Number = 550;
var extSWFH:Number = 400;
var extURL:String = "./Data/someSwf.swf";
var mainStageWidth:Number = 1024;
var mainStageHeight:Nubmer = 768;
var aLoader:Loader = new Loader;
aLoader.load(new URLRequest(extURL));
newWindow.stage.addChild(aLoader);
aLoader.scaleY = aLoader.scaleX = Math.min(mainStageWidth/extSWFW,mainStageHeight/extSWFH);
It is not very difficult to figure dimensions of a loaded SWF because it is engraved into it's header, you can read and parse it upon loading. Here's the working sample:
package assortie
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class Dimensions extends Sprite
{
private var loader:Loader;
public function Dimensions()
{
super();
loader = new Loader;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("outer.swf"));
}
private function onLoaded(e:Event):void
{
var aBytes:ByteArray = loader.contentLoaderInfo.bytes;
var aRect:Rectangle = new Rectangle;
var aRead:BitReader = new BitReader(aBytes, 8);
var perEntry:uint = aRead.readUnsigned(5);
aRect.left = aRead.readSigned(perEntry) / 20;
aRect.right = aRead.readSigned(perEntry) / 20;
aRect.top = aRead.readSigned(perEntry) / 20;
aRect.bottom = aRead.readSigned(perEntry) / 20;
aRead.dispose();
aRead = null;
trace(aRect);
}
}
}
import flash.utils.ByteArray;
internal class BitReader
{
private var bytes:ByteArray;
private var position:int;
private var bits:String;
public function BitReader(source:ByteArray, start:int)
{
bits = "";
bytes = source;
position = start;
}
public function readSigned(length:int):int
{
var aSign:int = (readBits(1) == "1")? -1: 1;
return aSign * int(readUnsigned(length - 1));
}
public function readUnsigned(length:int):uint
{
return parseInt(readBits(length), 2);
}
private function readBits(length:int):String
{
while (length > bits.length) readAhead();
var result:String = bits.substr(0, length);
bits = bits.substr(length);
return result;
}
static private const Z:String = "00000000";
private function readAhead():void
{
var wasBits:String = bits;
var aByte:String = bytes[position].toString(2);
bits += Z.substr(aByte.length) + aByte;
position++;
}
public function dispose():void
{
bits = null;
bytes = null;
}
}
I am loading an external swf file into a parent swf file.
Previously, I was getting error 1009 and fixed that by using a listener event to add the swf to the stage before running any scripts.
The swf however fails to load completely when embedded into a parent swf as seen in this URL
http://viewer.zmags.com/publication/06b68a69?viewType=pubPreview#/06b68a69/1
Here is the code I am using.
Thank you for any input.
package
{
import com.greensock.TweenLite;
import flash.display.DisplayObject;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;
public class slider5 extends Sprite
{
public var thumbPath:String = "Trailchair_thumb";
public var featPath:String = "Trailchair";
public var sliderIndex:Number = 1;
public var currBg:Bitmap = new Bitmap();
public var thumbCount:Number = 8;
public var thumbHolder:Sprite = new Sprite();
public var thumbMask:Sprite = new Sprite();
public var thumbX:Number = 0;
public var thmPadding:Number = 10;
public var thmWidth:Number;
public var navLeft:Sprite = new Sprite();
public var navRight:Sprite = new Sprite();
public var timer:Timer = new Timer(5000,0);
public var sliderDir:String = "fwd";
public function slider5()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(e:Event):void{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
//THE BACKGROUND IMAGE
currBg.alpha = 1;
stage.addChildAt(currBg, 0);
changeBg(sliderIndex);
//The thumbMask a sprite with graphic rectangle
thumbMask.x = 87;
thumbMask.y = 572;
thumbMask.graphics.beginFill(0xFFFFFF);
thumbMask.graphics.drawRect(0,0, 406, 181);
stage.addChildAt(thumbMask, 2);
//The thumbSlider
thumbHolder.x = 228;
thumbHolder.y = 573;
stage.addChildAt(thumbHolder, 1);
thumbHolder.mask = thumbMask;
buildThumbs();
//add the nav
navLeft.x = 100;
navLeft.y = 609;
navRight.x = 496;
navRight.y = 609;
stage.addChildAt(navLeft, 4);
stage.addChildAt(navRight, 4);
var navBmp:Bitmap = new Bitmap();
navBmp.bitmapData = new navarrow(109,109);
var navBmp_Rt:Bitmap = new Bitmap();
navBmp_Rt.bitmapData = new navarrow(109,109);
navLeft.addChild(navBmp);
navLeft.scaleX *= -1;
navRight.addChild(navBmp_Rt);
navLeft.useHandCursor = true;
navLeft.buttonMode = true;
navRight.useHandCursor = true;
navRight.buttonMode = true;
navLeft.name = "left";
navRight.name = "right";
navLeft.addEventListener(MouseEvent.CLICK, navClick);
navRight.addEventListener(MouseEvent.CLICK, navClick);
//add the active item frame
var frame:Sprite = new Sprite();
frame.x = 226;
frame.y = 570;
frame.graphics.lineStyle(10, 0x000000);
frame.graphics.drawRect(0,0,131, 181);
stage.addChildAt(frame, 6);
timer.addEventListener(TimerEvent.TIMER, timeEvt);
timer.start();
}
public function changeBg(index):void
{
//set the first slide from our library and add to the stage
var currBg_Class:Class = getDefinitionByName( featPath + index ) as Class;
currBg.bitmapData = new currBg_Class(597,842);
//fade it in
TweenLite.from(currBg, 0.5, {alpha:0});
}
public function buildThumbs():void
{
var currThm:Class;
for (var i:uint = 1; i<=thumbCount; i++)
{
currThm = getDefinitionByName( thumbPath + i ) as Class;
var thmBmp:Bitmap = new Bitmap();
thmBmp.bitmapData = new currThm(126,176);
thmBmp.x = thumbX;
thumbHolder.addChild(thmBmp);
thumbX += thmBmp.width + thmPadding;
}
thmWidth = thmBmp.width + thmPadding;
}
public function navClick(e):void
{
timer.reset();
timer.start();
var dir:String = e.currentTarget.name;
if (dir=="left" && thumbHolder.x < 228 )
{
sliderIndex--;
TweenLite.to(thumbHolder, 0.5, {x:thumbHolder.x + thmWidth});
//thumbHolder.x = thumbHolder.x + thmWidth;
}
else if (dir=="right" && thumbHolder.x > - 724 )
{
sliderIndex++;
TweenLite.to(thumbHolder, 0.5, {x:thumbHolder.x - thmWidth});
//thumbHolder.x = thumbHolder.x - thmWidth;
}
if (sliderIndex == thumbCount)
{
sliderDir = "bk";
}
if (sliderIndex == 1)
{
sliderDir = "fwd";
}
changeBg(sliderIndex);
}
public function timeEvt(e):void
{
if (sliderDir == "fwd")
{
navRight.dispatchEvent(new Event(MouseEvent.CLICK));
}
else if (sliderDir == "bk")
{
navLeft.dispatchEvent(new Event(MouseEvent.CLICK));
}
}
}
}
If you still need it you can try these two suggestions. Note I didnt know about Zmags and initially assumed that it was your own domain name. That's why I suggested you use the Loader class. It worked for me when I did a test version of a parent.swf' that loaded a test 'child.swf' containing your code. It actually loaded the child swf without problems.
Change from extending Sprite to extending MovieClip
Avoid checking for added to stage in this project
Explanations:
Extending MovieClip instead of Sprite
I Long story short Flash wont like your swf extending Sprite then being opened by a parent loader that extends Movieclip. The ZMag player will be extending MovieClip. It's logical and the docs do confirm this in a way. Whether it fixes your issue or not just keep it MovieClip when using ZMags.
Avoiding Stage referencing in your code..
Looking at this Zmags Q&A documentaion:
http://community.zmags.com/articles/Knowledgebase/Common-questions-on-flash-integration
Looking at Question 4.. In their answer these two stand out.
Reference of the stage parameter in the uploaded SWF conflicting with the publication
Badly or locally referenced resources in the SWF you uploaded which cannot be found
Is it really necessary to have an added_to_stage check in this? If it wont hurt then I suggest dropping the stage_added checking from function slider5() and instead cut/paste in there the code you have from the function onAddedToStage(e:Event).
Hope it helps.
I'm not familliar with flash neither action script, but i have to use a flash applet in my website, here is the object demo; http://www.weberdesignlabs.com/demos/flash_10_coverflow/
So the problem is, i want to disable the blank borders around each image, and also in the image reflection.
I think that when i'll get the image border itself to be disabled, the reflection border will be disabled too.
I of course have the .fla and all .as needed to modify that project.
What i tried, with succes is to set the blank border from 4 to 0 by modifing this line in the .as file called "Coverflow.as";
private var padding:Number=4;
The border will be effectively thinner, but the border is still here.
I paste you the Coverflow.as script code:
////////////////////////////////////////////
// Project: Flash 10 Coverflow
// Date: 10/3/09
// Author: Stephen Weber
////////////////////////////////////////////
package {
////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.display.BlendMode;
import flash.display.Loader;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.IOErrorEvent;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.navigateToURL;
import flash.display.Stage;
import flash.utils.setTimeout;
//TweenLite - Tweening Engine - SOURCE: http://blog.greensock.com/tweenliteas3/
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
public class Coverflow extends Sprite {
////////////////////////////////////////////
// VARIABLES
////////////////////////////////////////////
// size of the stage
private var sw:Number;
private var sh:Number;
private var background:Background;
// padding between each cover, can be customed via xml
private var coverflowSpacing:Number=30;
// transition time for movement
private var transitionTime:Number=0.75;
// the center of the stage
private var centerX:Number;
private var centerY:Number;
// store each image cover's instance
private var coverArray:Array=new Array();
// title of each image
private var coverLabel:CoverflowTitle = new CoverflowTitle();
// the slider under the image cover
private var coverSlider:Scrollbar;
// how many image covers
private var coverflowItemsTotal:Number;
// how to open the link
private var _target:String;
// size of the image cover
private var coverflowImageWidth:Number;
private var coverflowImageHeight:Number;
//Holds the objects in the data array
private var _data:Array = new Array();
// the y position of the item's title
private var coverLabelPositionY:Number;
//Z Position of Current CoverflowItem
private var centerCoverflowZPosition:Number=-125;
// display the middle of the cover or not
private var startIndexInCenter:Boolean=true;
// which cover to display in the beginning
private var startIndex:Number=0;
// the slide's Y position
private var coverSlidePositionY:Number;
//Holder for current CoverflowItem
private var _currentCover:Number;
//CoverflowItem Container
private var coverflowItemContainer:Sprite = new Sprite();
//XML Loading
private var coverflowXMLLoader:URLLoader;
//XML
private var coverflowXML:XML;
// the image cover's white border padding
private var padding:Number=0;
// stage reference
private var _stage:Stage;
//reflection
private var reflection:Reflect;
//Reflection Properties
private var reflectionAlpha:Number;
private var reflectionRatio:Number;
private var reflectionDistance:Number;
private var reflectionUpdateTime:Number;
private var reflectionDropoff:Number;
////////////////////////////////////////////
// CONSTRUCTOR - INITIAL ACTIONS
////////////////////////////////////////////
public function Coverflow(_width:Number, _height:Number, __stage:Stage = null):void {
_stage=__stage;
sw=_width;
sh=_height;
centerX=_width>>1;
centerY=(_height>>1) - 20;
loadXML();
//Grabs Background color passed in through FlashVars
var backgColor:String = _stage.loaderInfo.parameters["backgroundColor"];
if(backgColor == null) {
//Black
backgColor = "0x000000";
//White
//backgColor = "0xFFFFFF";
}
//Creates Background MovieClip
background = new Background();
//Set Background To Provided Width/Height
background.width = _width;
background.height = _height;
//Adds background MovieClip to DisplayList
addChild(background);
//Tints Background MovieClip with provided tint
TweenPlugin.activate([TintPlugin]);
TweenLite.to(background, 0, {tint:backgColor});
//Grabs Background color passed in through FlashVars
var labelColor:String = _stage.loaderInfo.parameters["labelColor"];
//Check for value and then default
if(labelColor == null) {
//Black
//labelColor = "0x000000";
//White
labelColor = "0xFFFFFF";
}
//Tint Coverflow label to color provided
TweenLite.to(coverLabel, 0, {tint:labelColor});
if (_stage) {
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
}
////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////
private function keyDownHandler(e:KeyboardEvent):void {
if (e.keyCode==37||e.keyCode==74) {
clickPre();
}
if (e.keyCode==39||e.keyCode==75) {
clickNext();
}
// 72 stand for "H" key, 191 stand for "?" key
if (e.keyCode==72||e.keyCode==191) {
}
}
// display the previous image
private function clickPre(e:Event=null):void {
_currentCover--;
if (_currentCover<0) {
_currentCover=coverflowItemsTotal-1;
}
coverSlider.value=_currentCover;
gotoCoverflowItem(_currentCover);
}
// display the next image
private function clickNext(e:Event=null):void {
_currentCover++;
if (_currentCover>coverflowItemsTotal-1) {
_currentCover=0;
}
coverSlider.value=_currentCover;
gotoCoverflowItem(_currentCover);
}
// loading the XML
private function loadXML():void {
//Loads XML passed through FlashVars
var xml_source:String = _stage.loaderInfo.parameters["xmlPath"];
//If XML not found through FlashVars then defaults to xml path below
if(xml_source == null) {
xml_source = 'xml/data.xml';
}
// loading the cover xml here
coverflowXMLLoader = new URLLoader();
coverflowXMLLoader.load(new URLRequest("xml/data.xml"));
coverflowXMLLoader.addEventListener(Event.COMPLETE, coverflowXMLLoader_Complete);
coverflowXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, coverflowXMLLoader_IOError);
}
// parse the XML
private function coverflowXMLLoader_Complete(e:Event):void {
coverflowXML=new XML(e.target.data);
coverflowItemsTotal=coverflowXML.cover.length();
coverflowSpacing=Number(coverflowXML.#coverflowSpacing);
coverflowImageWidth=Number(coverflowXML.#imageWidth);
coverflowImageHeight=Number(coverflowXML.#imageHeight);
coverLabelPositionY=Number(coverflowXML.#coverLabelPositionY);
coverSlidePositionY=Number(coverflowXML.#coverSlidePositionY);
transitionTime=Number(coverflowXML.#transitionTime);
centerCoverflowZPosition=Number(coverflowXML.#centerCoverflowZPosition);
//Image Border
padding = Number(coverflowXML.#imagePadding)
//Reflection Attributes
reflectionAlpha=Number(coverflowXML.#reflectionAlpha);
reflectionRatio=Number(coverflowXML.#reflectionRatio);
reflectionDistance=Number(coverflowXML.#reflectionDistance);
reflectionUpdateTime=Number(coverflowXML.#reflectionUpdateTime);
reflectionDropoff=Number(coverflowXML.#reflectionDropoff);
startIndex=Number(coverflowXML.#startIndex);
startIndexInCenter = (coverflowXML.#startIndexInCenter.toLowerCase().toString()=="yes");
_target=coverflowXML.#target.toString();
for (var i=0; i<coverflowItemsTotal; i++) {
//Make An Object To Hold Values
var _obj:Object = new Object();
//Set Values To Object from XML for each CoverflowItem
_obj.image = (coverflowXML.cover[i].#img.toString());
_obj.title = (coverflowXML.cover[i].#title.toString());
_obj.link = (coverflowXML.cover[i].#link.toString());
_data[i] = _obj;
}
loadCover();
}
private function coverflowXMLLoader_IOError(event:IOErrorEvent):void {
trace("Coverflow XML Load Error: "+ event);
}
// load the image cover when xml is loaded
private function loadCover():void {
for (var i:int = 0; i < coverflowItemsTotal; i++) {
var cover:Sprite=createCover(i,_data[i].image);
coverArray[i]=cover;
cover.y=centerY;
cover.z=0;
coverflowItemContainer.addChild(cover);
}
if (startIndexInCenter) {
startIndex=coverArray.length>>1;
gotoCoverflowItem(startIndex);
} else {
gotoCoverflowItem(startIndex);
}
_currentCover=startIndex;
coverSlider=new Scrollbar(coverflowItemsTotal,_stage);
coverSlider.value=startIndex;
coverSlider.x = (_stage.stageWidth/2) - (coverSlider.width/2);
coverSlider.y=_stage.stageHeight-40;
coverSlider.addEventListener("UPDATE", coverSlider_Update);
coverSlider.addEventListener("PREVIOUS", coverSlider_Previous);
coverSlider.addEventListener("NEXT", coverSlider_Next);
addChild(coverSlider);
//coverLabel.x = (sw - coverLabel.width)>>1;
coverLabel.x = (_stage.stageWidth/2) - (coverLabel.width/2);
coverLabel.y=coverLabelPositionY;
addChild(coverLabel);
addChild(coverSlider);
addChild(coverLabel);
}
private function coverSlider_Update(e:Event):void {
var value:Number=(coverSlider.value);
gotoCoverflowItem(value);
e.stopPropagation();
}
private function coverSlider_Previous(e:Event):void {
clickPre();
}
private function coverSlider_Next(e:Event):void {
clickNext();
}
// move to a certain cover via number
private function gotoCoverflowItem(n:int):void {
_currentCover=n;
reOrderCover(n);
if (coverSlider) {
coverSlider.value=n;
}
}
private function cover_Selected(event:CoverflowItemEvent):void {
var currentCover:uint=event.data.id;
if (coverArray[currentCover].rotationY==0) {
try {
// open the link if user click the cover in the middle again
if (_data[currentCover].link!="") {
navigateToURL(new URLRequest(_data[currentCover].link), _target);
}
} catch (e:Error) {
//
}
} else {
gotoCoverflowItem(currentCover);
}
}
// change each cover's position and rotation
private function reOrderCover(currentCover:uint):void {
for (var i:uint = 0, len:uint = coverArray.length; i < len; i++) {
var cover:Sprite=coverArray[i];
if (i<currentCover) {
//Left Side
TweenLite.to(cover, transitionTime, {x:(centerX - (currentCover - i) * coverflowSpacing - coverflowImageWidth/2), z:(coverflowImageWidth/2), rotationY:-65});
} else if (i > currentCover) {
//Right Side
TweenLite.to(cover, transitionTime, {x:(centerX + (i - currentCover) * coverflowSpacing + coverflowImageWidth/2), z:(coverflowImageWidth/2), rotationY:65});
} else {
//Center Coverflow
TweenLite.to(cover, transitionTime, {x:centerX, z:centerCoverflowZPosition, rotationY:0});
//Label Handling
coverLabel._text.text=_data[i].title;
coverLabel.alpha=0;
TweenLite.to(coverLabel, 0.75, {alpha:1,delay:0.2});
}
}
for (i = 0; i < currentCover; i++) {
addChild(coverArray[i]);
}
for (i = coverArray.length - 1; i > currentCover; i--) {
addChild(coverArray[i]);
}
addChild(coverArray[currentCover]);
if (coverSlider) {
addChild(coverSlider);
addChild(coverLabel);
}
}
//Create CoverflowItem and Set Data To It
private function createCover(num:uint, url:String):Sprite {
//Setup Data
var _data:Object = new Object();
_data.id=num;
//Create CoverflowItem
var cover:CoverflowItem=new CoverflowItem(_data);
//Listen for Click
cover.addEventListener(CoverflowItemEvent.COVERFLOWITEM_SELECTED, cover_Selected);
//Set Some Values
cover.name=num.toString();
cover.image=url;
//cover.padding=padding;
cover.imageWidth=coverflowImageWidth;
cover.imageHeight=coverflowImageHeight;
cover.setReflection(reflectionAlpha, reflectionRatio, reflectionDistance, reflectionUpdateTime, reflectionDropoff);
//Put CoverflowItem in Sprite Container
var coverItem:Sprite = new Sprite();
cover.x=- coverflowImageWidth/2-padding;
cover.y=- coverflowImageHeight/2-padding;
coverItem.addChild(cover);
coverItem.name=num.toString();
return coverItem;
}
}
}
Note that there is 6 more script file, named: CoverflowItem.as, CoverflowItemEvent.as, Main.as, Reflect.as, Scrollbar.as, ScrollbarEvent.as.
I hope the solution is in the script file above.
If someone who's familliar with AS can point me in a good direction to go, i will be very pleased !
Sorry if my english isnt very good.
This component loads settings from a file called data.xml. Setting imagePadding="0" in that XML file should get rid of the border.
I have typical situation where big loop is loading lots of images and its done asynchronous which make browser to frees during loading and I want to make it synchronous but having big trouble doing it. I found this class synchronous loader and it work great but you cant add mouse event listener to loader. Here is sample code:
for (var i = 0; i < items.length; i++) {
item = items[i];
if(item.layer>1){
ld:Loader = new Loader();
ld.load(new URLRequest(item.url));
ld.rotation = item.rotation;
ld.x = item.x ;
ld.y = item.y;
ld.addEventListener(Event.COMPLETE, loadComplete);
ld.scaleX = item.scaleX;
ld.scaleY = item.scaleY;
ld.addEventListener(MouseEvent.MOUSE_DOWN, select);
layers_arr[item.layer].addChild(ld);
}
}
Any idea how this can be done?
like arieljake says, here is a little example on how to use it:
package
{
import com.greensock.TweenLite;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.MP3Loader;
import com.greensock.loading.SWFLoader;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.display.ContentDisplay;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Main extends Sprite
{
public var itemUrl:String;
public var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
queue.maxConnections = 1; //Checks how much items that can be loaded at the same time
queue.append( new ImageLoader("http://www.myurl.com/myimage.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0,scaleMode:"proportionalInside"}) );
queue.append( new ImageLoader("http://www.myotherurl.com/awesomeimage.jpg", {name:"photo2", estimatedBytes:2400, container:this, alpha:0, scaleMode:"proportionalInside"}) );
queue.addEventListener(LoaderEvent.CHILD_COMPLETE, childCompleteHandler); //checks when a child has completed to load
queue.addEventListener(LoaderEvent.CHILD_PROGRESS, childProgressHandler); //checks the child progress
//prioritize the loader named "photo1"
LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize();
//start loading
queue.load();
}
protected function childProgressHandler(event:LoaderEvent):void
{
var procent:Number = Math.floor(event.target.progress*100);
var targetName:String = event.target.name;
trace(procent+'% loaded of item: '+targetName);
}
protected function childCompleteHandler(event:LoaderEvent):void
{
var targetName:String = event.target.name;
trace(targetName+' is loaded!');
}
private function completeHandler(event:LoaderEvent):void {
var objects:Array = event.currentTarget.content;
for(var i:uint=0; i < objects.length; i++)
{
var image:ContentDisplay = LoaderMax.getContent(objects[i].name);
TweenLite.to(image, 1, {alpha:1, y:100});
}
trace(event.target + " is complete!");
}
private function errorHandler(event:LoaderEvent):void {
trace("error occured with " + event.target + ": " + event.text);
}
}
}
Check this out: http://www.greensock.com/loadermax/
Let's you specify max number of simultaneous loaders
package com.fladev.background
{
//import all classes
import caurina.transitions.Tweener;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class MainClass extends Sprite
{
//create variables
private var loaderMenu:Loader;
private var loaderNames:Array = new Array ();
private var loaderContents:Array = new Array ();
private var loaderSlide:Loader;
private var swfDisplayObject:DisplayObject;
private var swfComObject:Object;
private var xmlLoader:URLLoader = new URLLoader();
private var xmlSlideLoader:URLLoader = new URLLoader();
public function MainClass()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, stageResize);
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("navigation.xml"));
//xmlSlideLoader.addEventListener(Event.COMPLETE, showSlideXML);
//xmlSlideLoader.load(new URLRequest("slides.xml"));
}
function showXML(e:Event):void
{
XML.ignoreWhitespace = true;
var menuBtns:XML = new XML(e.target.data);
var i:Number = 0;
for ( i = 0; i < menuBtns.navItem.length(); i++ )
{
loaderMenu = new Loader();
loaderMenu.name = menuBtns.navItem[i].name ;
loaderMenu.load(new URLRequest(menuBtns.navItem[i].swfURL));
loaderMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, createSwfObjects);
}
}
private function createSwfObjects(event:Event):void
{
var swfContent = event.currentTarget.content as MovieClip ;
var swfName = event.currentTarget.loader ;
navigationContainer.addChild(event.target.loader);
showImage(swfContent);
if ( swfName.name == 'topNavigation' )
{
swfContent.addEventListener("clickHandle",topNavigationClickHandler);
}
}
private function topNavigationClickHandler():void
{
trace('Back to root');
}
private function showImage(navigationItem):void
{
try
{
navigationItem.alpha = 0;
Tweener.addTween(navigationItem, { alpha:1, time:1, transition:"easeOutSine" } );
navigationItem.smoothing = true;
} catch (e:Error) { trace('Error no tweening'); };
stageResize();
}
private function stageResize(e:Event=null):void
{
var centerImages:Array = new Array ( contentContainer, navigationContainer, backgroundImage ) ;
backgroundImage.x = 0;
backgroundImage.y = 0;
backgroundImage.scaleX = backgroundImage.scaleY = 1;
if ((stage.stageHeight / stage.stageWidth) < backgroundImage.height / backgroundImage.width) {
backgroundImage.width = stage.stageWidth;
backgroundImage.scaleY = backgroundImage.scaleX;
} else {
backgroundImage.height = stage.stageHeight;
backgroundImage.scaleX = backgroundImage.scaleY;
}
for each ( var centered:MovieClip in centerImages )
{
centered.x = stage.stageWidth / 2 - centered.width / 2;
centered.y = stage.stageHeight / 2 - centered.height / 2;
}
}
}
}
This is my code for the main.as.
And here my code for my loaded SWF on the maintimeline.
addEventListener(Event.ADDED_TO_STAGE, init);
function init(event:Event):void
{
trace('try dispatch');
dispatchEvent(new Event("clickHandle",true));
}
Try dispatch works, but it does not get back to the main to fire up "Back to root".
Any idea?
thx!
As long as all movieclips dispatching events are added to the display list, this should work. This makes me think that perhaps the event listener being added is not working. Try adding a trace statement to the block of code as shown below:
if ( swfName.name == 'topNavigation' )
{
trace("adding listener");
swfContent.addEventListener("clickHandle",topNavigationClickHandler);
}
I expect that this if condition is failing, and thus your listener is never being created. Also, you need to add a function parameter to the callback method "topNavigationClickHandler" to accept the event as the callback parameter. You have not done this, and this is an error that would be thrown at runtime when the event was received and dispatched to the callback method. You havn't seen this yet because your listener has never had to invoke the callback. So you're gonna have to fix this code like so:
private function topNavigationClickHandler(e:Event):void
{
trace('Back to root');
}
Also I just want to add that your if condition on setting this listener seems a bit redundant, since you already know you are expecting the navigation swf, because you're explicitly loading it. Also I don't believe the name property would be set like this. Typically the name is only set inside the IDE before compilation, and if it isn't, it gets dynamically generated at runtime. What might be more useful is to check the URL of the loaded SWF to see if it contains "topNavigation" or whatever the swf name is. You can do this like so:
var swfUrl:String = myLoader.contentLoaderInfo.url;
if (swfUrl.search("topNavigation") != -1){
//Match found, add listener for navigation
}