var Loader = Laya.Loader;
var loaderHandler = Laya.Handler;
var scronw=640;
var scronh=960;
//初始化展示界面
Laya.init(scronw, scronh)
//设置舞台背景色
Laya.stage.bgColor = '#999999';
//设置适配模式
Laya.stage.scaleMode = "exactfit";
// 无加载失败重试
Laya.loader.retryNum = 0;
//加载的数据信息 https://layaair.ldc.layabox.com/api/?category=Core&class=laya.net.Loader
var urls = [
{url:"res/castlebeyond.jpg",type:Loader.IMAGE},
];
//加载load侦听
Laya.loader.load(urls, loaderHandler.create(this, onAssetLoaded), loaderHandler.create(this, onLoading, null, false));
// 侦听加载失败
Laya.loader.on(Event.ERROR, this, onError);
this.moveover =true;
function onAssetLoaded(texture)
{
console.log("加载结束");
pic="res/castlebeyond.jpg";
this.bgimg = new Laya.Sprite();
this.bgimg.width=scronw;
this.bgimg.height=scronh;
//加载显示图片,坐标位于0,0
var temptexture=Loader.getRes(pic);
if (typeof(temptexture)!="undefined"){
this.bgimg.texture=Loader.getRes(pic)
}else{
this.bgimg.loadImage(pic);
}
this.bgimg.x=0;
this.bgimg.y=0;
this.bgimg.mvtox=-300;
this.bgimg.mvtoy=0;
this.bgimg.speedx=-10;
this.bgimg.speedy=0;
this.bgimg.on(Laya.Event.CLICK,this,readnext);
//添加到舞台
Laya.stage.addChild(this.bgimg);
}
function onLoading(progress)
{
console.log("加载进度: " + Math.floor(progress*100)+"%");
}
function onError(err)
{
console.log("加载失败: " + err);
}
function readnext(){
console.log("readnext");
if (this.moveover){
this.moveover=false;
Laya.timer.loop(100, this, funbg);
}else{
console.log("clicked");
}
}
//移动背景定时器(背景特殊,是基础部分,所以单独拿出来)
function funbg(e){
console.log("funbg");
var canmove=false;
if ( Math.abs(this.bgimg.x - this.bgimg.mvtox) > Math.abs(this.bgimg.speedx)){
this.bgimg.x=this.bgimg.x+this.bgimg.speedx;
canmove=true;
}
if ( Math.abs(this.bgimg.y - this.bgimg.mvtoy) > Math.abs(this.bgimg.speedy)) {
this.bgimg.y=this.bgimg.y+this.bgimg.speedy;
canmove=true;
}
if (!canmove){
console.log("move over");
Laya.timer.clear(this,funbg);
this.moveover=false;
}
}
这个是简单的一段代码
请注意如果初始化不设置
this.bgimg.width=scronw;
this.bgimg.height=scronh;
这个是设置宽高的地方,只有设置了才有反应,似乎是使用
this.bgimg.texture=Loader.getRes(pic) 载入数据的时候,宽度,高度都没有进行定义,需要手动进行设置,而直接用
loadImage(pic)
的时候,则宽高均自动进行了重设