1.如果在程序运行当中需要修改一个图片的资源
//路径一定要放在资源管理器的绝对路径下,不然会一直报错说在resources文件下找不到 var realUrl =cc.url.raw('resources/2.png'); var texture =cc.textureCache.addImage(realUrl); this.getComponent(cc.Sprite).spriteFrame.setTexture(texture); //*************<方法二>**************// cc.loader.loadRes("2",cc.SpriteFrame,function (err,spriteFrame) { self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame; });
loadSpriteFrame(panNode, url) {
console.log("loadSpriteFrame");
Common.loadSpriteFrame(url, function (err, SpriteFrame) {
if (err) {
console.log(err);
return;
}
let sp = panNode.getComponent(cc.Sprite);
sp.spriteFrame = SpriteFrame;
});
}
export function loadSpriteFrame(url, cb) {
cc.loader.loadRes(url, cc.SpriteFrame, cb);
}
2.需要多个精灵执行动画,一个执行完,开始另一个执行(这里需要注意的是,执行动画的是一个线程,还有主线程,这里边一定要用一个var self =this,以免在动画线程中获取不到类的属性)
var self =this; var star1 = this.starlist.getChildByName('start1'); star1.active = true; var action1 = cc.scaleTo(0.1, 0.4); var action2 = cc.scaleTo(0.1, 0.3); star1.runAction(cc.sequence(action1, action2, cc.delayTime(0.5), cc.callFunc(function () { var star2 = self.starlist.getChildByName('start2'); self.loadSpriteFrame(star2,'complete/blackStar.png'); star2.active = true; var action1 = cc.scaleTo(0.1, 0.4); var action2 = cc.scaleTo(0.1, 0.3); star2.runAction(cc.sequence(action1, action2)); }), cc.delayTime(0.5), cc.callFunc(function () { var star3 = self.starlist.getChildByName('start3'); self.loadSpriteFrame(star3,'complete/blackStar.png'); star3.active = true; var action1 = cc.scaleTo(0.1, 0.4); var action2 = cc.scaleTo(0.1, 0.3); star3.runAction(cc.sequence(action1, action2)); }), cc.delayTime(0.5), cc.callFunc(function () { var star4 = self.starlist.getChildByName('start4'); self.loadSpriteFrame(star4,'complete/blackStar.png'); star4.active = true; var action1 = cc.scaleTo(0.1, 0.4); var action2 = cc.scaleTo(0.1, 0.3); star4.runAction(cc.sequence(action1, action2)); }), cc.delayTime(0.5), cc.callFunc(function () { var star5 = self.starlist.getChildByName('start5'); self.loadSpriteFrame(star5,'complete/blackStar.png'); star5.active = true; var action1 = cc.scaleTo(0.1, 0.4); var action2 = cc.scaleTo(0.1, 0.3); star5.runAction(cc.sequence(action1, action2)); }) ));
//js中的结构体
interface IBallInfo{
subject:number;
weight:number;
}
interface Dictionary<T>{
[key:string]:T;
}
export interface IUserInfo {
userId: number;
userName: string;
userRole: number;
}
interface IGameInfo {
gameType: EGameType;
currentLevel: number;
totalLevel: number;
levelInfos?: Dictionary<ILevelInfo>;
}
@property
coacheList:Dirctionary<cc.Node>={}
//添加触摸事件
init(game: cc.Component) {
this.game = game;
this.registerEvent(this.node);
}
registerEvent(ballNode: cc.Node) {
ballNode.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
ballNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
ballNode.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
}
function loadResource(url: string, type: any): Promise<any> { return new Promise((resolved, rejected) => { cc.loader.loadRes(url, type, (err, resource) => { if (err) { rejected(err) } resolved(resource) }) }) } function loadBalls(): Promise<any> { return loadResource('data/balls', cc.JsonAsset) .then((data) => { let list: IBallInfo[] = data.json; let allPromise: Promise<any>[] = []; for (let index = 0; index < list.length; index++) { const element = list[index]; balls[element.id] = element; let promise = loadResource(`game/balls/${element.subject}/${element.icon}`, cc.SpriteFrame) .then((sf) => { element.ballsf = sf; return loadResource(`game/pans/${element.subject}/${element.icon}`, cc.SpriteFrame) .then((panSf) => { element.pansf = panSf; }); }) allPromise.push(promise); } return Promise.all(allPromise) .then(() => { console.log('--balls:--', balls) return list; }) }) }
3.添加音效
首先在脚本下添加一个属性:
@property({ type: cc.AudioClip })
soundWrong = null;
播放音乐:
cc.audioEngine.play(this.soundWrong, false, 1);