This snipet creates a scrolling background.

The image must be 256×256, 512×512 or something like that.

package screens.scrollingStarField
{
	import flash.geom.Point;
 
	import starling.display.BlendMode;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.EnterFrameEvent;
	import starling.textures.Texture;
 
	public class ScrollingStarField extends Sprite
	{
		[Embed(source="data/starfield.png")] private const StarField1:Class;
		[Embed(source="data/starfield.jpg")] private const StarField2:Class;	
		private var backGround:Image;
		private var backGround2:Image;
		public function ScrollingStarField()
		{
			super();
			var texture:Texture = Texture.fromBitmap(new StarField2, true, false, 1, "bgra", true));
			backGround = new Image(texture);
			backGround.x = 512;
			backGround.y = 384;
			tile(backGround, 3, 3);
			addChild(backGround);
 
			texture = Texture.fromBitmap(new StarField1, true, false, 1, "bgra", true));
			backGround2 = new Image(texture);
			backGround2.x = 512;
			backGround2.y = 384;
			backGround2.blendMode = BlendMode.SCREEN;
			tile(backGround2, 3, 3);
			addChild(backGround2);
 
			addEventListener(EnterFrameEvent.ENTER_FRAME, onEnterFrame);
		}
 
		protected function onEnterFrame(evt:EnterFrameEvent):void
		{
			var speed:Number = 0.01;
			var p:Point;
			p = backGround.getTexCoords(0);
			p.x += speed;
			backGround.setTexCoords(0, p);
 
			p = backGround.getTexCoords(1);
			p.x += speed;
			backGround.setTexCoords(1, p);
 
			p = backGround.getTexCoords(2, p);
			p.x += speed;
			backGround.setTexCoords(2, p);
 
			p = backGround.getTexCoords(3, p);
			p.x += speed;
			backGround.setTexCoords(3, p);
 
 
			speed = 0.02;
 
			p = backGround2.getTexCoords(0);
			p.x += speed;
			backGround2.setTexCoords(0, p);
 
			p = backGround2.getTexCoords(1);
			p.x += speed;
			backGround2.setTexCoords(1, p);
 
			p = backGround2.getTexCoords(2, p);
			p.x += speed;
			backGround2.setTexCoords(2, p);
 
			p = backGround2.getTexCoords(3, p);
			p.x += speed;
			backGround2.setTexCoords(3, p);
 
 
		}
 
		private function tile(pImage:Image, pVertically:Number, pHorizontally:Number):void
		{
			pImage.setTexCoords(1,new Point(pHorizontally,0));
			pImage.setTexCoords(2,new Point(0,pVertically));
			pImage.setTexCoords(3,new Point(pHorizontally, pVertically));
			pImage.pivotX = pImage.width / 2; 
			pImage.pivotY = pImage.height / 2; 
			pImage.width *= pHorizontally;
			pImage.height *= pVertically;
		}
	}
}
  tutorials/scrolling_image_tiling.txt · Last modified: 2014/02/20 20:53 by 71.189.190.186
 
Powered by DokuWiki