egret之红包满屏随意飘动
在做这个需求之前,我们假设屏幕上同时飘动的红包数最大为10
/**红包生成 */
if (this.redBagNum < 9) { let redBag = GameUtil.createBitmapByName("Red_bag_png"); redBag.x = this.gameScene.gamePanel.redBag.x; redBag.y = this.gameScene.gamePanel.redBag.y; redBag.width = 80; redBag.height = 150; redBag.touchEnabled = true; this.redBagNum++; redBag.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickRedBag, this); this.gameScene.gamePanel.redBagGroup.addChild(redBag); this.redBagAnim(redBag, redBag.x, 1500); }else{ //红包数达到上限 } /**红包飘动动画 */ private redBagAnim(obj: egret.Bitmap, x: number, y: number) { egret.Tween.get(obj).to({ x: x, y: y }, 3000).call(() => { egret.Tween.removeTweens(obj); let randomX = Math.floor(Math.random() * 1000) - 450; let randomY = Math.floor(Math.random() * 1600); this.redBagAnim(obj, randomX, randomY); }); } /**点击红包事件 */ private clickRedBag(evt: egret.TouchEvent) { // evt.currentTarget.visible=false; this.gameScene.gamePanel.redBagGroup.removeChild(evt.currentTarget); this.redBagNum--; }