前端之家收集整理的这篇文章主要介绍了
cocos2d-js 翻牌,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
pg.TurnCardLayer = cc.Layer.extend({
okBtn: null
});
pg.TurnCardLayer.create = function () {
var res = new pg.TurnCardLayer();
return (res && res.init()) ? res : null;
};
pg.TurnCardLayer.prototype.ctor = function () {
cc.Layer.prototype.ctor.call(this);
// 初始化
console.log("TurnCardLayer:init");
// 屏幕大小
var winSize = cc.winSize;
// 设定纹理格式
pg.setDefaultTextureFormat(cc.Texture2D.PIXEL_FORMAT_RGB565);
// 创建背景
var bgSprite = new cc.Sprite(res.StartBG_568_png);
bgSprite.setPosition(winSize.width / 2,winSize.height / 2);
this.addChild(bgSprite);
// 重置纹理格式
pg.setDefaultTextureFormat(cc.Texture2D.PIXEL_FORMAT_RGBA4444);
var okBtnItem = cc.MenuItemImage.create(res.Start_png);
okBtnItem.setPosition(winSize.width / 2,winSize.height / 2);
var menu = new cc.Menu(okBtnItem);
menu.setPosition(0,0);
this.addChild(menu,10);
// 输出纹理内存占用
pg.dumpTextureInfo();
};
pg.TurnCardLayer.prototype.init = function () {
return true;
};
pg.TurnCardLayer.prototype.onClear = function () {
pg.Layer.prototype.onClear();
console.log("TurnCardLayer:clear");
pg.removeTextureForKey(res.StartBG_568_png);
};
pg.TurnCardScene = cc.Scene.extend({
actionIsDone: false
});
pg.TurnCardScene.create = function (num) {
var res = new pg.TurnCardScene();
if (res && res.init(num)) {
// var layer = pg.TurnCardLayer.create();
// res.addChild(layer);
return res;
}
return null;
};
pg.TurnCardScene.prototype.init = function (num) {
//加一背景
// var background = cc.LayerColor.create(cc.color(255,180,255,255),cc.size.width,cc.size.height);
// this.addChild(background);
this.actionIsDone = true;// 标示动作是否完成
this.createPoker(); // 创建扑克
this.createListener(); // 注册监听
return true;
};
// 创建扑克
pg.TurnCardScene.prototype.createPoker = function () {
// 扑克牌正面
// 创建背景
// var pokerFront = new cc.Sprite(res.Card_Clubs_2_png);
// pokerFront.setVisible(true);
// pokerFront.setPosition(cc.p(cc.winSize.width / 2,cc.winSize.height / 2));
// this.addChild(pokerFront,1,123);
//
// 扑克牌反面
var pokerBack = new cc.Sprite(res.Card_BackGround_Normal_png);
pokerBack.setPosition(cc.p(cc.winSize.width / 2,cc.winSize.height / 2));
this.addChild(pokerBack,1,321);
};
// 翻牌动作
pg.TurnCardScene.prototype.startOrbitAction = function () {
// // 扑克牌正面
// var pokerFront = this.getChildByTag(123);
// // 扑克牌反面
var pokerBack = this.getChildByTag(321);
var actionBy = cc.rotateBy(0.2,0,-90);
pokerBack.runAction(cc.sequence(actionBy, cc.callFunc( function() {
pokerBack.setTexture(res.Card_Clubs_2_png);
pokerBack.setFlippedX(true);
}), actionBy
));
};
pg.TurnCardScene.prototype.actionIsDownFunc = function () {
this.actionIsDone = true;
};
pg.TurnCardScene.prototype.createListener = function() {
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ALL_AT_ONCE, swallowTouches: true, onTouchesBegan: function (touches,event) {
var target = event.getCurrentTarget();
target.onTouchBegan(touches);
return true;
}, onTouchesMoved: function (touches,event) {
var target = event.getCurrentTarget();
target.onTouchMoved(touches);
}, onTouchesEnded: function (touches,event) {
var target = event.getCurrentTarget();
target.onTouchEnded(touches);
}
});
cc.eventManager.addListener(listener,this);
};
pg.TurnCardScene.prototype.onTouchBegan = function (touches) {
var touch = touches[0];
var convertedLocation = touch.getLocation();
this.startOrbitAction();
return true;
};
pg.TurnCardScene.prototype.onTouchMoved = function (touches) {
var touch = touches[0];
var convertedLocation = touch.getLocation();
};
pg.TurnCardScene.prototype.onTouchEnded = function (touches) {
};
pg.TurnCardScene.prototype.onEnter = function () {
cc.Layer.prototype.onEnter.call(this);
};
pg.TurnCardScene.prototype.onExit = function (){
cc.Layer.prototype.onExit.call(this);
};
原文链接:https://www.f2er.com/cocos2dx/339300.html