#19 实现精灵平铺效果

This commit is contained in:
YHH
2020-08-07 08:50:26 +08:00
parent ab3f38c6a8
commit 834ad565e1
9 changed files with 295 additions and 162 deletions
@@ -1,37 +1,38 @@
///<reference path="./TiledSpriteRenderer.ts"/>
module es {
export class ScrollingSpriteRenderer extends TiledSpriteRenderer {
/**
* x自动滚动速度(以像素/s为单位)
*/
public scrollSpeedX = 15;
/**
* 自动滚动的y速度(以像素/s为单位)
*/
public scroolSpeedY = 0;
public get textureScale(): Vector2 {
return this._textureScale;
}
public set textureScale(value: Vector2){
this._textureScale = value;
// 重新计算我们的inverseTextureScale和源矩形大小
this._inverseTexScale = new Vector2(1 / this._textureScale.x, 1 / this._textureScale.y);
}
private _scrollX = 0;
private _scrollY = 0;
constructor(sprite: Sprite) {
super(sprite);
}
public update() {
this._scrollX += this.scrollSpeedX * Time.deltaTime;
this._scrollY += this.scroolSpeedY * Time.deltaTime;
this.sourceRect.x = this._scrollX;
this.sourceRect.y = this._scrollY;
}
public render(camera: Camera) {
if (!this.sprite)
return;
super.render(camera);
let renderTexture = new egret.RenderTexture();
let cacheBitmap = new egret.DisplayObjectContainer();
cacheBitmap.removeChildren();
cacheBitmap.addChild(this.leftTexture);
cacheBitmap.addChild(this.rightTexture);
this.leftTexture.x = this.sourceRect.x;
this.rightTexture.x = this.sourceRect.x - this.sourceRect.width;
this.leftTexture.y = this.sourceRect.y;
this.rightTexture.y = this.sourceRect.y;
cacheBitmap.cacheAsBitmap = true;
renderTexture.drawToTexture(cacheBitmap, new egret.Rectangle(0, 0, this.sourceRect.width, this.sourceRect.height));
this._sourceRect.x = this._scrollX;
this._sourceRect.y = this._scrollY;
}
}
}