Files
esengine/source/src/ECS/Components/ScrollingSpriteRenderer.ts
T

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-07-07 21:40:57 +08:00
///<reference path="./TiledSpriteRenderer.ts"/>
2020-07-23 09:10:27 +08:00
module es {
2020-08-07 11:02:04 +08:00
import Bitmap = egret.Bitmap;
2020-07-23 09:10:27 +08:00
export class ScrollingSpriteRenderer extends TiledSpriteRenderer {
2020-08-07 08:50:26 +08:00
/**
* x自动滚动速度(以像素/s为单位)
*/
2020-07-23 09:10:27 +08:00
public scrollSpeedX = 15;
2020-08-07 08:50:26 +08:00
/**
* 自动滚动的y速度(以像素/s为单位)
*/
2020-07-23 09:10:27 +08:00
public scroolSpeedY = 0;
2020-07-07 21:40:57 +08:00
2020-08-07 08:50:26 +08:00
public get textureScale(): Vector2 {
return this._textureScale;
2020-07-23 09:10:27 +08:00
}
2020-07-07 21:40:57 +08:00
2020-08-07 08:50:26 +08:00
public set textureScale(value: Vector2){
this._textureScale = value;
2020-07-07 21:40:57 +08:00
2020-08-07 08:50:26 +08:00
// 重新计算我们的inverseTextureScale和源矩形大小
this._inverseTexScale = new Vector2(1 / this._textureScale.x, 1 / this._textureScale.y);
}
2020-07-07 21:40:57 +08:00
2020-08-07 11:02:04 +08:00
public set scrollWidth(value: number){
this._scrollWidth = value;
}
public get scrollWidth(){
return this._scrollWidth;
}
public set scrollHeight(value: number){
this._scrollHeight = value;
}
public get scrollHeight(){
return this._scrollHeight;
}
2020-08-07 08:50:26 +08:00
private _scrollX = 0;
private _scrollY = 0;
2020-08-07 11:02:04 +08:00
private _scrollWidth = 0;
private _scrollHeight = 0;
2020-07-07 21:40:57 +08:00
2020-08-07 08:50:26 +08:00
constructor(sprite: Sprite) {
super(sprite);
2020-08-07 11:02:04 +08:00
this._scrollWidth = this.width;
this._scrollHeight = this.height;
2020-08-07 08:50:26 +08:00
}
2020-07-07 21:40:57 +08:00
2020-08-07 08:50:26 +08:00
public update() {
2020-08-07 09:21:55 +08:00
if (!this.sprite)
return;
2020-08-07 08:50:26 +08:00
this._scrollX += this.scrollSpeedX * Time.deltaTime;
this._scrollY += this.scroolSpeedY * Time.deltaTime;
2020-08-07 11:02:04 +08:00
this._sourceRect.x = this._scrollX;
this._sourceRect.y = this._scrollY;
this._sourceRect.width = this._scrollWidth + this._scrollX;
this._sourceRect.height = this._scrollHeight + this._scrollY;
2020-07-23 09:10:27 +08:00
}
2020-07-07 21:40:57 +08:00
}
2020-07-23 09:10:27 +08:00
}