Laya实现Banner

随着微信小游戏的突起,一些游戏引擎也越来越火了,目前支持小游戏的游戏引擎就那么几个:Cocos、Egret、Laya 

Laya是最近起来的,我觉得他会火!我加入官方的开发群时已到3群了,3群已快满 ,在群中,常有人问怎么实现广告Banner轮播效果,这里特意写下制作过程。

具体思路:

1、创建一个Panel,用来作Banner的窗口,为什么选Panel而不使用Sprite?如果是全屏的话,Sprite也可用,但如果Banner只是一部分,Sprite就会把该隐藏的图片一起显示出来了,而Panel不会

2、创建两个Image,width和height属性都与Panel一样,一个在左,一个在右,即x1=0,y1=0   ;x2=width,y2=0

3、设置图片path数组

4、开始轮播,采用Laya.timer.loop定时器实现,每隔一定时间,改变两个Image的x值,当一个image的x值变化超过了width的时候,即停止,然后把移动到外面的Image改变x值,相当于把当前的第二个Image改成第一个Image,而第一个Image又变成动画前的第二个Image,如此循环,达到Banner轮播效果

接下来,上代码:

module banner {

    export class BannerPage extends ui.BannerUI {

        public imgs: Array;

        //延迟

        public delay = 2000;

        //停留时间

        private stopTime = 1500;

        //第一个img的图片索引

        private index1 = 0;

        //第二个img的图片索引

        private index2 = 1;

        //速度

        private speed = 1;

        constructor() {

            super();

            this.speed = this.width / (this.delay - this.stopTime);

        }

        public setStopTime(time: number) {

            this.stopTime = time;

            this.speed = this.width / (this.delay - this.stopTime);

        }

        /**

        * 设置数据,设置完成后开妈轮播

        * @param imgs

        */

        public setData(imgs: Array): void {

            this.imgs = imgs;

            if (this.imgs.length <= 1) {

                //只有一张

                this.img1.skin = (this.imgs[0]);

            } else {

                //有多张

                //开始轮播

                this.startPlay();

            }

        }

        public startPlay() {

            //少于两张的就不轮播

            if (this.imgs.length < 2) return;

            this.img1.skin = (this.imgs[0]);

            this.img2.skin = (this.imgs[1]);

            Laya.timer.loop(this.delay, this, this.loopImg);

        }

        /**

        * 图片循环动画

        *

        */

        private loopImg() {

            var thiz = this;

            Laya.timer.loop(10, this, this.onLoop);

        }

        private onLoop() {

            //判断哪个在前面

            var imgLeft = this.img1.x < this.img2.x ? this.img1 : this.img2;

            if (imgLeft.x <= -this.width) {

                //到头了,清除循环

                Laya.timer.clear(this, this.onLoop);

                //把left的img移动到右边

                //同时改变图片

                //如果第一张图片在前面,则index1+=2

                if (this.img1.x < this.img2.x) {

                    this.index1 += 2;

                    this.img1.skin = (this.imgs[this.index1 % this.imgs.length]);

                } else {

                    this.index2 += 2;

                    this.img2.skin = (this.imgs[this.index2 % this.imgs.length]);

                }

                //把第一张图片放到右边去

                imgLeft.x = this.width;

                return;

            }

            this.img1.x -= this.speed * 10;

            this.img2.x -= this.speed * 10;

        }

    }

}



使用:

import WebGL = Laya.WebGL;

// 程序入口

class GameMain {

    constructor() {

        Laya.init(600, 800, WebGL);

        var imgs = [

            'http://www.ytmfdw.com/image/img1.jpg',

            'http://www.ytmfdw.com/image/img2.jpg',

            'http://www.ytmfdw.com/image/img3.jpg',

            'http://www.ytmfdw.com/image/img4.jpg'

        ];

        var bannerPage = new banner.BannerPage();

        Laya.stage.addChild(bannerPage);

        bannerPage.setData(imgs);

    }

}

new GameMain();


界面

ui.BannerUI:

{

    "x":0,

    "type":"View",

    "selectedBox":1,

    "selecteID":1,

    "props":{"width":500,"sceneColor":"#000000","height":1000},

    "nodeParent":-1,

    "label":"View",

    "isOpen":true,

    "isDirectory":true,

    "isAniNode":true,

    "hasChild":true,

    "compId":1,

    "child":[

        {

            "x":15,

            "type":"Panel",

            "props":{"y":0,"x":0,"width":500,"var":"bannerLayout","height":300},

            "nodeParent":1,

            "label":"Panel(bannerLayout)",

            "isOpen":true,

            "isDirectory":true,

            "isAniNode":true,

            "hasChild":true,

            "compId":2,

            "child":[

                {

                    "x":30,

                    "type":"Image",

                    "props":{"y":0,"x":0,"width":500,"var":"img1","height":300},

                    "nodeParent":2,

                    "label":"Image(img1)",

                    "isDirectory":false,

                    "isAniNode":true,

                    "hasChild":false,

                    "compId":4,

                    "child":[

                        ]

                },

                {

                    "x":30,

                    "type":"Image",

                    "props":{"y":0,"x":500,"width":500,"var":"img2","height":300},

                    "nodeParent":2,

                    "label":"Image(img2)",

                    "isDirectory":false,

                    "isAniNode":true,

                    "hasChild":false,

                    "compId":5,

                    "child":[

                        ]

                },

                {

                    "x":30,

                    "type":"Sprite",

                    "props":{"y":270,"x":0,"width":500,"var":"dotLayout","height":30},

                    "nodeParent":2,

                    "label":"Sprite(dotLayout)",

                    "isDirectory":false,

                    "isAniNode":true,

                    "hasChild":false,

                    "compId":3,

                    "child":[

                        ]

                }]

        }],

    "animations":[

        {

            "nodes":[

                ],

            "name":"ani1",

            "id":1,

            "frameRate":24,

            "action":0

        }]

}


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容