1.添加监听事件
public open(): void {
Laya.timer.frameLoop(1, this, this.update);
this.ui.stage.on(Laya.Event.CLICK, this, this.stageClick);
this.ui.stage.on(Laya.Event.MOUSE_DOWN, this, this.MOUSE_DOWN);
}
2.一个循环的tween动画
Laya.timer.loop(1000, this.ui.imgGuide, () => {
Laya.Tween.to(this.ui.imgGuide, { x: this.ui.imgGuide.x + 400 }, 300, null, Laya.Handler.create(this, (evt) => {
Laya.Tween.to(this.ui.imgGuide, { x: this.ui.imgGuide.x - 400 }, 600, null, Laya.Handler.create(this, (evt) => {
this.ui.imgGuide.x = 147;
Laya.Tween.clearAll(evt);
}))
}));
})
3.弧度转角度
//滑动角度
let hudu: number = Math.acos((this.curPoint.x - this.lastPoint.x) / this.fishLineLength);
let jiaodu: number = 90 - MathUtil.radianToAngle(hudu);
4.画个圈圈吧
for (var i = 0; i < 200; i++) {
let radianUnit: number = Math.PI / 100;
var img: Laya.Sprite = new Laya.Sprite();
let x: number = Math.cos(radianUnit * i) * 150;
let y: number = Math.sin(radianUnit * i) * 150;
img.graphics.drawCircle(x, y, 10, "#000000");
this.ui.imgGuide.addChild(img);
}
5.自己常用的排序遍历
for (var i = 0; i < this.vGuns.length - 1; i++) {
for (var j = 0; j < this.vGuns.length - 1 - i; j++) {
if (this.vGuns[j].id == this.vGuns[j + 1].id) {
this.isDouble = true;
console.log("第一段引导");
this.playAnimationOne(this.vGuns[j], this.vGuns[j + 1]);
this.isGEnent = true;
return;
}
}
}
6.一个接一个
/**一波又一波的敌人 */
public playAnimaByTimes(times: number, delay: number = 1000, fun: Function) {
fun();
Laya.Tween.to(this, {}, delay, null, Laya.Handler.create(this, function () {
times--;
if (times <= 0) {
return;
}
this.playAnimaByTimes(times, delay, fun);
}.bind(this)))
}
7.一个金币飞出的特效
/**金币飞出的特效 */
public popCoin(xRange: number, yRange: number, endpoint: Laya.Point, coinCount: number, uiAdd: any, func: Function = null): void {
var vgolds: Laya.Image[] = [];
for (var i = 0; i < coinCount; i++) {
var coin: Laya.Image = new Laya.Image();
coin.skin = "game/gold.png";
coin.x = MathUtil.randomRange(xRange - 100, xRange + 100);
coin.y = MathUtil.randomRange(yRange - 100, yRange + 100);
uiAdd.addChild(coin);
Laya.Tween.to(coin,{scaleX:1.2,scaleY:1.2},200);
vgolds.push(coin);
}
Laya.timer.once(1000, this, () => {
if (vgolds.length) {
for (var j = 0; j < vgolds.length; j++) {
Laya.Tween.to(vgolds[j], { x: endpoint.x, y: endpoint.y, scaleX: 0.2, scaleY: 0.2 }, 100, null, Laya.Handler.create(vgolds[j], function () {
func();
Laya.Tween.clearAll(vgolds[j]);
}.bind(this)))
}
}
SoundManager.ins.playSound(CustomBase64.sound_money);
})
}
8.一个炫酷的图片数字标签
/**
* 数字标签以及文字显示
* @param num 传进去的数值
* @param x
* @param y
* @param path 路径
*/
public showWordImg(num: number, x: number, y: number, path: string = "game/UI_Number_"): void {
var numL: number = num.toString().length;
var imgs: Laya.Image[] = [];
for (var i = 0; i < numL; i++) {
var img: Laya.Image = new Laya.Image();
var nNum: number = Math.pow(10, i);
img.skin = path + Math.floor((num / nNum % 10)) + ".png";
img.x = -(i * 40) + x;
img.y = y;
UIManager.ins.addTop(img);
imgs.push(img);
Laya.Tween.to(img, { y: img.y - 150 }, 300);
}
Laya.timer.once(800, this, function () {
for (var i = 0; i < imgs.length; i++) {
var sImg: Laya.Image = imgs[i];
sImg.destroy();
}
}.bind(this));
}
9.弧度转角度
public GetCov(x: number, y: number): number {
if (Math.abs(x) + Math.abs(y) <= 0.01) {
return 0;
}
var roi: number = Math.atan2(x, y) * 180 / Math.PI;
return roi;
}
10.朝向某一点移动
/**寻路循环 */
public onLoop(): void {
var point: Laya.Point = this.getFrameTransition(this.targetPlayer.x - this.x, this.targetPlayer.y - this.y, this.moveSpeed);
this.x += point.x;
this.y += point.y;
this.dirAngle = 90 - this.GetCov(this.targetPlayer.x - this.x, this.targetPlayer.y - this.y);
}
/**
* 返回每一帧的偏差值
* @param deltaX 目标点与本身x的偏差
* @param deltaY 目标点与本身y的偏差
* @param speed 移动速度
* @return 下一帧本身应该在的point
*/
public getFrameTransition(deltaX: number, deltaY: number, speed: number): Laya.Point {
var x: number;
var y: number;
var num: number = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
x = (speed / num) * deltaX;
y = (speed / num) * deltaY;
return new Laya.Point(x, y);
}