CoocsCreator学习,更改精灵背景图。
// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {
// //1 设置背景图完整平铺整个页面。
// var childChangeB = this.node.getChildByName("ChangeButton");//获得悲剧子节点
// var bj = cc.view.getVisibleSize();//获得整个场景大小
// var bjSprite = this.node.getChildByName("BjSprite");
// bjSprite.setContentSize(bj.width, bj.height);
// var texture = cc.textureCache.addImage(cc.url.raw('/resources/bj.jpg'));
// this.node.children[0].getComponent(cc.Sprite).spriteFrame.setTexture(texture);
// var self = this;
// //2 点击按钮,更改背景
// childChangeB.on('click', function () { //给按钮节点添加点击事件
// });
// },
onLoad () {
//1 设置背景图完整平铺整个页面。
var childChangeB = this.node.getChildByName("ChangeButton");//获得悲剧子节点
var bj = cc.view.getVisibleSize();//获得整个场景大小
var bjSprite = this.node.getChildByName("BjSprite");
bjSprite.setContentSize(bj.width, bj.height);
//这样图片不现实的原因可能是resources都是动态加载的,在onload的时候还没加载完图片,所以没有图片。
// var texture = cc.textureCache.addImage(cc.url.raw('resources/bg.jpg'));
// this.node.children[0].getComponent(cc.Sprite).spriteFrame.setTexture(texture);
var self = this;
//2 点击按钮,更改背景
childChangeB.on('click', function () { //给按钮节点添加点击事件
// var path = cc.url.raw("resources/bg.jpg");
// this.node.children[0].getComponent(cc.Sprite).spriteFrame.setTexture(path);
var texture = cc.textureCache.addImage(cc.url.raw('resources/bg.jpg'));
//this.node.children[0].getComponent(cc.Sprite) 这句的意思应该是,获取他为Sprite对象,
//如果直接获取this.node.children[0]他还是不会是Sprite对象,至少spriteFrame没有这个属性
this.node.children[0].getComponent(cc.Sprite).spriteFrame.setTexture(texture);
}.bind(self));
},
start () {
},
// update (dt) {},
});
这个是ChangBJ文件中的代码。也是主场景绑定的JS组件。
下面是文档的接口图,为了能找到图片。


浙公网安备 33010602011771号