Hi @pandamicro i’m beginner at cocos2d-js html5. How to use requestanimationframe function in scene.
i used requestanimationframe function then error is displayed. HELP ME
Uncaught TypeError: Failed to execute ‘requestAnimationFrame’ on ‘Window’: The callback provided as parameter 1 is not a function.GameLayer.js:91 cc.Layer.extend.loop
my code is
var options = {
name : “Маамуу Нааш Ир”,
singer : “Маамуу Нааш Ир”,
genre : “Хүүхдийн дуу”,
level : 0,
data : null,
src : res.music
};
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
return window.setTimeout(callback, 1000 / 60);
};
})();
window.cancelRequestAnimFrame = ( function() {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
} )();
var animeLoop;
var music;
var GameLayer = cc.Layer.extend({
ctor:function(){
this._super();
this.init();
},
init:function () {
var sp = new cc.Sprite(res.bgStart);
sp.anchorX = 0;
sp.anchorY = 0;
sp.scale = 1.5;
this.addChild(sp, 0, 1);
var winsize = cc.director.getWinSize();
var title = new cc.LabelTTF("POINT : 200", "Arial", 21, cc.size(MW.WIDTH * 0.85, 0), cc.TEXT_ALIGNMENT_CENTER );
title.attr({
x: 80,
y: winsize.height - 50
});
title.setColor(cc.color(MW.FONTCOLOR));
this.addChild(title);
music = new Music(options);
music.play();
var button = new ccui.Button();
button.setTouchEnabled(true);
button.setPosition(cc.p(winsize.width - 50, winsize.height - 50));
button.loadTextures(res.pause_n, res.pause_c, "");
button.addTouchEventListener(this.onPause ,this);
button.attr({ scale : 0.5 })
this.addChild(button);
var label = new cc.LabelTTF("Буцах", "Arial", 21);
label.setColor(cc.color(MW.FONTCOLOR));
var back = new cc.MenuItemLabel(label, this.onBackCallback);
var menu = new cc.Menu(back);
menu.x = winsize.width / 2;
menu.y = 60;
this.addChild(menu);
this.loop();
return true;
},
onBackCallback:function () {
var scene = new cc.Scene();
scene.addChild(new SongSelectLayer());
cc.director.runScene(new cc.TransitionFade(1.2, scene));
},
onPause:function() {
music.pause();
cancelAnimationFrame(animeLoop);
},
loop:function () {
animeLoop = requestAnimFrame(this.loop);
cc.log("-> loop <-");
}
});