1

im creating a fish tank..so what i want to do is my fish should be infinite scrolling..i'm using action script 3..this is the code i used..but it's not working..

var scrollSpeed:uint = 5;

//This adds two instances of the movie clip onto the stage.



//This positions the second movieclip next to the first one.
f1.x = 0;
f2.x = f1.width;

//Adds an event listener to the stage.
stage.addEventListener(Event.ENTER_FRAME, moveScroll); 

//This function moves both the images to left. If the first and second 
//images goes pass the left stage boundary then it gets moved to 
//the other side of the stage. 
function moveScroll(e:Event):void{
f1.x -= scrollSpeed;  
f2.x -= scrollSpeed;  

if(f1.x < +f1.width){
f1.x = f1.width;
}else if(f2.x < +f2.width){
f2.x = f2.width;
}
}

f1 and f2 is my fish instance name

jlock
  • 69
  • 1
  • 6
  • I'm a little confused, are you looking to move you fish to the opposite side of the screen after they go off one side? – Glitcher Jul 08 '13 at 10:23

2 Answers2

1

Your problem lines are at the bottom.

if (f1.x < f1.width) {
    f1.x = stage.stageWidth - f1.width // f1.width;
} // Removed else

if (f2.x < f2.width) {
    f2.x = f1.width;
}
Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17
0

this assumes your images/mc's are large enough for the screen, and are duplicates (have the same content)

function moveScroll(e:Event):void {
    //position fish 1
    f1.x -= scrollSpeed;
    //when fish 1 is out of bounds (off the screen on the left, set it back to 0
    if (f1.x < -f1.width) {
       f1.x = 0;
    }
    //always position fish2 next to fish 1
    f2.x = f1.x + f1.width;
}

another way would be to put them in a array and switch them around, if they dont have the same content:

//add fishes to array
var images:Array = new Array();
images.push(f1, f2);

function moveScroll(e:Event):void {
    //position fish 1
    images[0].x -= scrollSpeed;
    //when first fish is out of bounds, remove it from the array (unshift) and add it at the end (push)
    if (images[0].x < -images[0].width) {
       images.push(images.unshift())
    }
    //always position fish2 next to fish 1
    images[1].x = images[0].x + images[0].width;
}

these are just basic examples, but you get the idea

Marijn
  • 800
  • 7
  • 14