The LoopingBackground.as script will help you to make a looping background in a easily and fast way. You will just need to have a tilable background, that can go in any direction, and with that you will be able to do it. Note: You will need to have a public static instance of Mono in the Main.as. and the Update Manager instantiated.
How to use LoopingBackground.as
First of all, you will need to have a public static instance of Mono.as in the Main.as. Also, you will need to have initialized the UpdateManager.as before using this script since it is needed to make the movement of it.
Once you have all the requeriments of the class, you will need to create an instance of LoopingBackground.as. For the constructor you will need to send two sprites of the looping background (they can be different as long as they loop in the desired direction), the sprite where they are going to be attached and the direction you want them to move.
For example:
background = new LoopingBackground(new MCBackground(), new MCBackground(), camera.view, new Point(0, 10)); background.play();
In this case, we have a var called background of type LoopingBackground.as. When we create it, the first two parameters are the sprites we are going to loop. In this case they are the same one and are movieclips of type MCBackground. As third parameter we are sending the container were the sprites are going to be added as childs, in this case we are using the view of the Camera.as, also from Framework Mono. For last, we create a new point that will be the direction and speed where the backgrounds move. In this case, the background will move down at 10 pixels per frame.
Once we created the instance, we just give it play(); and will start playing and looping itself.
Also, if needed, you can stop it called the stop() function and when you want to remove it, just call the remove() function.
For last, you can easily change the direction or speed of the looping background by just calling changeDirection function and sending by parameter the new direction that contains the speed.
background.changeDirection(new Point(0, -5);
In this example we are making now that the background moves upwards at 5 pixels per second.
Public Functions
- play():void
- changeDirection(direction:Point):void
- function stop():void
- remove():void
Private Functions
- update():void
Private Vars
- _firstBackground:Sprite;
- _secondBackground:Sprite;
- _movement:Point;
- _stageWidth:int;
- _stageHeight:int;
- _placingSecond:Boolean;
- _reproducing:Boolean;