So I'm looking for some help in migrating and refactoring some code. I'm
more than happy to post up the code and may write a tutorial with some of
the things I've learned when I'm done. So here goes: I've written a hacked
to image scroller in as2. I am working on converting it to as3 and got
everything down but the movement. One problem is I'm not really sure how to
replicate the duplicate functionality in as3. Second in the as2 version
sometimes the spacing between images got messed up and if anyone had any
ideas I'd be appreciative.
AS2 Movement and Duplication code:
PHP Code:
function mover():Void{
for(var i:String in ads){
ads[i]._x += vX;
if(vX > 0){
if(ads[i]._x > totalWidth){
removeMovieClip(ads[i])
ads = ads.slice(ads[i]);
}
if(ads[i]._x + ads[i]._width > totalWidth){
var newAd:MovieClip = duplicateClip(ads[i]);
if(newAd){
newAd._y = offsetH;
newAd._x = -ads[i]._width-spacing;
if(!checkArray(newAd,ads))
ads.push(newAd)
}
}
}else{
if(ads[i]._x + ads[i]._width < 0){
removeMovieClip(ads[i])
ads = ads.slice(ads[i]);
}
if(ads[i]._x < 0){
var newAd:MovieClip = duplicateClip(ads[i]);
if(newAd){
newAd._x = totalWidth + spacing;
newAd._y = offsetH;
if(!checkArray(newAd,ads))
ads.push(newAd)
}
}
}
}
}
function duplicateClip(m:MovieClip):MovieClip{
var copy:String = m._name+"_c";
var orig:String = m._name.substring(0,m._name.length -2);
if(!_root.banner[copy] and !_root.banner[orig]){
var temp:MovieClip = m._parent.createEmptyMovieClip(m._name+"_c"
,m._parent.getNextHighestDepth())
var id:Number = Number(m._name.split("_c")[0])
var mcListener:Object = new Object();
mcListener.onLoadInit = function(target:MovieClip){
target._xscale = target._yscale = (stageHeight/target.
_height)*100;
}
var imgLoader:MovieClipLoader = new MovieClipLoader();
imgLoader.addListener(mcListener);
imgLoader.loadClip(reader.getProperty(id, "media"), temp);
//duplicateMovieClipImage(m,temp)
return temp;
}
}
function duplicateMovieClipImage(from, target):Void{
var visuals = new flash.display.BitmapData(from._width, from._height);
var m:Matrix = new Matrix();
m.scale(from._xscale/100,from._xscale/100)
visuals.draw(from,m);
target.attachBitmap(visuals, 1);
}
AS3 Class thus far:
PHP Code:
package com.infinity{
import flash.net.*;
import flash.xml.XML;
import flash.events.*
import flash.display.*;
import flash.geom.Matrix;
public class MediaTickerTape extends MovieClip{
private var __images:Array;
private var __loadCount:Number = 0;
private var __totalWidth:Number = 0;
private var __width:Number;
private var __height:Number;
public var speed:Number = 5;
public var spacing:Number = 5;
public function MediaTicketTape(){
}
/************************************************************************************
* Private
************************************************************************************/
private function loaded(evt:Event):void {
evt.target.content.scaleX = evt.target.content.scaleY =
__height/evt.target.content.height;
evt.target.content.x = __totalWidth;
__totalWidth +=evt.target.content.width + spacing;
evt.target.removeEventListener(Event.COMPLETE,loaded)
__images[__loadCount] = evt.target.content;
__loadCount++;
if(__loadCount == __images.length){
//__totalWidth = Math.max(__totalWidth,stageWidth);
addEventListener(Event.ENTER_FRAME,scroller)
}else{
loadImg(__images[__loadCount])
}
}
private function scroller(evt:Event){
for each(var img:Object in __images){
img.x += speed;
if(img.x > (__totalWidth)){
}
if(img.x > (__totalWidth+img.width)){
//img.x = -img.width-spacing;
}
}
}
private function loadImg(url:String):void{
var pictLdr:Loader = new Loader();
var pictURLReq:URLRequest = new URLRequest(url);
pictLdr.load(pictURLReq);
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE,
loaded);
addChild(pictLdr)
}
/************************************************************************************
* Public
************************************************************************************/
public function size(w:Number, h:Number):void{
__width = w;
__height = h;
}
public function set feed(mediaData:XML):void{
__images = new Array();
for each (var media:XML in mediaData..media){
__images.push(ISMFunctions.formatURL(media.toString(),null))
}
loadImg(__images[__loadCount])
}
}
}
Any ideas how I could duplicate it but make sure its limited so there is
only one copy of the image on the stage at any given time?
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com