Skip to content

Instantly share code, notes, and snippets.

@Seasons7
Created November 8, 2012 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seasons7/4041507 to your computer and use it in GitHub Desktop.
Save Seasons7/4041507 to your computer and use it in GitHub Desktop.
CardFlip for cocos2d jsb
//------------------------------------------------------------------
//
// JavaScript sample
//
//------------------------------------------------------------------
//
// For a more complete sample, see "JS Watermelon With Me" bundled with cocos2d-iphone
//
// Loads cocos2d, chipmunk constants and helper functions
require("jsb_constants.js");
director = cc.Director.getInstance();
winSize = director.getWinSize();
centerPos = cc.p( winSize.width/2, winSize.height/2 );
//------------------------------------------------------------------
//
// Main Layer
//
//------------------------------------------------------------------
var MainLayer = cc.LayerGradient.extend({
// Constructor
ctor:function () {
// This is needed when subclassing a native class from JS
cc.associateWithNative( this, cc.LayerGradient );
// Initialize the Grandient Layer
this.init(cc.c4b(0, 0, 0, 255), cc.c4b(0, 128, 255, 255));
// After initializing, you can add nodes to the GradientLayer
var hello = cc.LabelTTF.create("Hello World (JavaScript)", "Marker Felt", 36);
this.addChild( hello );
hello.setPosition( centerPos );
// Simple Menu
var item1 = cc.MenuItemFont.create("Play Game", this, this.onItem1);
var item2 = cc.MenuItemFont.create("Options", this, this.onItem2);
// Change size and color of items
item1.setFontSize( 20 );
item1.setColor( cc.c3b(192,192,192));
item2.setFontSize( 20 );
item2.setColor( cc.c3b(192,192,192));
// create menu with items
var menu = cc.Menu.create( item1, item2);
menu.setPosition( cc.p( winSize.width/2, winSize.height/3) );
menu.alignItemsHorizontally();
this.addChild(menu);
},
//
// callbacks
//
onEnter:function () {
// Do something if needed
},
onExit:function () {
// Do something if needed
},
onItem1:function(sender) {
// Item 1 callback
var scene = cc.Scene.create();
var layer = new GameLayer();
scene.addChild( layer );
director.replaceScene( cc.TransitionFade.create(0.5, scene ) );
},
onItem2:function(sender) {
// Item 2 callback
sender.stopAllActions();
var rot = cc.RotateBy.create( 2, 360 );
sender.runAction( rot );
}
});
//------------------------------------------------------------------
//
// Game Layer
//
//------------------------------------------------------------------
CARD_FACE_TAG = 1;
CARD_BACK_TAG = 2;
var GameLayer = cc.Layer.extend({
_cards:[],
_action:null,
// Constructor
ctor:function () {
// This is needed when subclassing a native class from JS
cc.associateWithNative( this, cc.Layer );
// Initialize the Layer
this.init();
// schedule update
this.scheduleUpdate();
// Misc label
var label = cc.LabelTTF.create("GameLayer node", "Arial", 24);
this.addChild( label );
label.setPosition( centerPos );
this.setTouchEnabled( true );
// load card
var cardF,cardB;
cardF = this._cards[0];
cardB = this._cards[1];
cardF = cc.Sprite.create("cardface.png");
this.addChild( cardF );
cardF.setPosition(cc.p(200,200));
cardF.setTag( CARD_FACE_TAG );
cardF.setVisible( false );
cardB = cc.Sprite.create("cardback.png");
this.addChild( cardB );
cardB.setPosition(cc.p(200,200));
cardB.setTag( CARD_BACK_TAG );
var hide = cc.Hide.create();
var func = cc.CallFunc.create( this, this.flipAction);
var orbitCamera = cc.OrbitCamera.create( 0.2, 1,0,0, 90,0,0 );
this._action = cc.Sequence.create(orbitCamera,hide,func);
this._action.retain();
},
onTouchesBegan:function( touches,event ) {
cc.log( "touch" );
if( this._action == null ) return;
var card = this.getChildByTag( CARD_BACK_TAG );
card.runAction(this._action);
this._action.release();
this._action = null;
},
flipAction:function(){
cc.log("flip!!");
var card = this.getChildByTag( CARD_FACE_TAG );
var show = cc.Show.create();
var orbitCamera = cc.OrbitCamera.create( 0.2, 1,0,270, 90,0,0 );
var action = cc.Sequence.create( show,orbitCamera );
card.runAction( action );
},
//
// callbacks
//
onEnter:function () {
// Do something if needed
this._super();
},
onExit:function () {
// Do something if needed
this._super();
},
update:function(dt) {
// Add the game logic here
// This function will be called each frame
//cc.log( 'Delta Time is: ' + dt );
}
});
//------------------------------------------------------------------
//
// Main entry point
//
//------------------------------------------------------------------
function run()
{
var scene = cc.Scene.create();
var layer = new MainLayer();
scene.addChild( layer );
director.runWithScene( scene );
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment