滚动精灵
This commit is contained in:
37
source/src/ECS/Components/ScrollingSpriteRenderer.ts
Normal file
37
source/src/ECS/Components/ScrollingSpriteRenderer.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
///<reference path="./TiledSpriteRenderer.ts"/>
|
||||
class ScrollingSpriteRenderer extends TiledSpriteRenderer {
|
||||
public scrollSpeedX = 15;
|
||||
public scroolSpeedY = 0;
|
||||
private _scrollX = 0;
|
||||
private _scrollY = 0;
|
||||
|
||||
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.bitmap.texture = renderTexture;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,57 @@
|
||||
///<reference path="./SpriteRenderer.ts" />
|
||||
/**
|
||||
* 滚动由两张图片组合而成
|
||||
*/
|
||||
class TiledSpriteRenderer extends SpriteRenderer {
|
||||
protected sourceRect: Rectangle;
|
||||
protected leftTexture: egret.Bitmap;
|
||||
protected rightTexture: egret.Bitmap;
|
||||
|
||||
public get scrollX(){
|
||||
public get scrollX() {
|
||||
return this.sourceRect.x;
|
||||
}
|
||||
public set scrollX(value: number){
|
||||
public set scrollX(value: number) {
|
||||
this.sourceRect.x = value;
|
||||
}
|
||||
public get scrollY(){
|
||||
public get scrollY() {
|
||||
return this.sourceRect.y;
|
||||
}
|
||||
public set scrollY(value: number){
|
||||
public set scrollY(value: number) {
|
||||
this.sourceRect.y = value;
|
||||
}
|
||||
|
||||
constructor(sprite: Sprite){
|
||||
constructor(sprite: Sprite) {
|
||||
super();
|
||||
|
||||
this.leftTexture = new egret.Bitmap();
|
||||
this.rightTexture = new egret.Bitmap();
|
||||
this.leftTexture.texture = sprite.texture2D;
|
||||
this.rightTexture.texture = sprite.texture2D;
|
||||
|
||||
this.setSprite(sprite);
|
||||
this.sourceRect = sprite.sourceRect;
|
||||
}
|
||||
|
||||
public render(camera: Camera){
|
||||
public render(camera: Camera) {
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
super.render(camera);
|
||||
|
||||
let renderTexture = new egret.RenderTexture();
|
||||
let targetTexture = new egret.Bitmap(this.sprite.texture2D);
|
||||
let clipBounds = new egret.Rectangle(this.sourceRect.x, this.sourceRect.y, this.sourceRect.width, this.sourceRect.height);
|
||||
renderTexture.drawToTexture(targetTexture, clipBounds);
|
||||
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.bitmap.texture = renderTexture;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user