A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Hello and quick question about my script...

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    72

    Hello and quick question about my script...

    I'm working on a photo slideshow and I have 4 photos. I have them displaying randomly with TweenLite, however I have 2 more things I'd like to do with it but I really don't know how to do them. I'd like for the photos to display randomly forever, as if the script would never end. Also, I'm curious as to why only 3 of the 4 photos are displayed when I run the swf.

    Here is my script thus far:
    Code:
    import gs.TweenLite;
    import gs.easing.*;
    
    var photoNums:Array = new Array(photo_1,photo_2,photo_3,photo_4);
    
    var randPhotos:Array;
    
    var currentPhoto:uint = 0;
    
    function shuffle(a:Array):Array 
    {
    	var len:Number = a.length-1;
    	for (var ivar:Number = len; ivar>=0; ivar--) 
    	{
    		var p:Number = Math.floor(Math.random()*(ivar+1));
    		var t = a[ivar];
    		a[ivar] = a[p];
    		a[p] = t;
    	}
    	return a;
    }
    
    randPhotos = shuffle(photoNums);
    
    var timer:Timer =  new Timer(5000);
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    
    function onTimer(evt:TimerEvent):void 
    {
    	if (currentPhoto > 0) 
    	{
    		TweenLite.to(this.randPhotos[currentPhoto], .75, {x:140, y:0, alpha:0, ease:Back.easeInOut});
    	}
    	TweenLite.to(this.randPhotos[currentPhoto+1], .75, {x:600, y:0, alpha:1, ease:Back.easeInOut});
    
    	currentPhoto += 1;
    }
    trace(photoNums);
    Any help would be extremely appreciated.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The photo at index 0 is never manipulated with the onTimer code. Why do you have the if there?

    To make it loop, you will have to set currentPhoto back to 0 when it gets to randPhotos.length.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You do know that your shuffle is actually manipulating the original array, right? It's just odd that you are returning the array and putting it in a new variable if you knew that. If you did not intend for it to shuffle the parameter array, you should clone it then manipulate the new clone.

  4. #4
    Member
    Join Date
    Jul 2009
    Posts
    72
    Wow, I'm sorry but most of what you said went over my head. I'm fairly new with AS in case you couldn't tell. How would you do it if you were trying to accomplish what I'm trying to accomplish? If my way is not the most efficient, please, by all means, let me know a better way to go about it.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I happen to have built a slideshow framework a few weeks ago. I haven't got around to posting the code and explanation yet, but I'd like to do that tonight. The stuff I'm going to post is far more general and configurable than yours, so it might be overkill for you, but you could make a looping slideshow like you want in a few minutes.

    Anyway, a small modification to your existing code:
    Code:
    import gs.TweenLite;
    import gs.easing.*;
    
    var photoNums:Array = new Array(photo_1,photo_2,photo_3,photo_4);
    
    var randPhotos:Array;
    
    var currentPhoto:uint = 0;
    
    function shuffle(arr:Array):Array 
    {
            var a:Array = arr.clone(); //do not alter input array.
    	var len:Number = a.length-1;
    	for (var ivar:Number = len; ivar>=0; ivar--) 
    	{
    		var p:Number = Math.floor(Math.random()*(ivar+1));
    		var t = a[ivar];
    		a[ivar] = a[p];
    		a[p] = t;
    	}
    	return a;
    }
    
    randPhotos = shuffle(photoNums);
    
    var timer:Timer =  new Timer(5000);
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    
    function onTimer(evt:TimerEvent):void 
    {
    	TweenLite.to(this.randPhotos[currentPhoto], .75, {x:140, y:0, alpha:0, ease:Back.easeInOut});
            currentPhoto++;
            if (currentPhoto >= randPhotos.length){
              currentPhoto = 0;
            }
    	TweenLite.to(this.randPhotos[currentPhoto], .75, {x:600, y:0, alpha:1, ease:Back.easeInOut});
    }
    trace(photoNums);

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I've now posted my slideshow code, and an object oriented mini-tutorial. http://cosmodro.me/blog/2009/jul/3/b...eshow-framewo/

  7. #7
    Member
    Join Date
    Jul 2009
    Posts
    72
    hello again. I'm having some trouble with your revision. If I'm correct you were trying to make it loop indefinitely. However what were you trying to do with "clone"? Clone is not a function, therefore I get a compiler error. Any suggestions?

  8. #8
    flash chick
    Join Date
    Oct 2007
    Location
    Cali
    Posts
    37
    I don't want to derail the thread, but what is tweenLite?

  9. #9
    Member
    Join Date
    Jul 2009
    Posts
    72
    "TweenLite is an extremely lightweight, FAST, and flexible tweening engine that serves as the core of the GreenSock tweening platform. There are plenty of other tweening engines out there to choose from, so here's why you might want to consider TweenLite:"...

    Thats from http://blog.greensock.com/tweenliteas3/

    check it out...it's pretty sweet.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sorry about the clone thing. I forgot that it doesn't exist on arrays. The point was to make a copy so as not to manipulate the array passed in. Replace 'clone' with 'concat', I think.

  11. #11
    Member
    Join Date
    Jul 2009
    Posts
    72
    you sir, are awesome. It works perfectly. I have one more thing I would like to do though. But only if it's fairly harmless to implement it. I would like the swf to start with one of the random photos already up, and then after the timer fires it goes off the stage and then the rest of the photos begin showing

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your photos are already on stage, so just place randphotos[0] in the right spot right after setting randphotos to the shuffled array.

  13. #13
    Member
    Join Date
    Jul 2009
    Posts
    72
    randPhotos[0]...not sure how that makes sense but I'll try it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center