[cocos2d-js]chipmunk例子(二)
- var TAG_SPRITE_BALL = ;
- var TAG_SPRITE_USER = ;
- var TAG_SPRITE_NPC = ;
- var PTM_RATIO = ;
- var GRABABLE_MASK_BIT = <<;
- var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT;
- var MainLayer = cc.Layer.extend({
- _ball:null,
- _man:null,
- _rpc:null,
- _leftGoal:null,
- _rightGoal:null,
- _leftPower:null,
- _rightPower:null,
- _time:,
- space:null,
- ctor:function () {
- this._super();
- var size = cc.director.getWinSize();
- this._ball = new Ball();
- this._ball.x = size.width / ;
- this._ball.y = size.height / ;
- this.addChild(this._ball);
- // 左方向
- var btnLeft = cc.MenuItemImage.create(
- res.btn_left_up,
- res.btn_left_down,
- function () {
- this._man.runLeft();
- }, this);
- btnLeft.x = ;
- // 右方向
- var btnRight = cc.MenuItemImage.create(
- res.btn_right_up,
- res.btn_right_down,
- function () {
- this._man.runRight();
- }, this);
- btnRight.x = ;
- // power
- var btnPower = cc.MenuItemImage.create(
- res.btn_power_up,
- res.btn_power_down,
- function () {
- this._man.power();
- }, this);
- btnPower.x = ;
- // jump
- var btnJump = cc.MenuItemImage.create(
- res.btn_jump_up,
- res.btn_jump_down,
- function () {
- this._man.jump();
- }, this);
- btnJump.x = ;
- // kick
- var btnKick = cc.MenuItemImage.create(
- res.btn_kick_up,
- res.btn_kick_down,
- function () {
- this._man.kick();
- }, this);
- btnKick.x = ;
- // 暂停
- var btnPause = cc.MenuItemImage.create(
- "res/pause.png",
- "res/pause2.png",
- function () {
- this.onStop();
- }, this);
- btnPause.x = size.width - ;
- btnPause.y = size.height - ;
- var menu = cc.Menu.create(btnLeft, btnRight, btnPower, btnJump, btnKick, btnPause);
- menu.x = ;
- menu.y = ;
- this.addChild(menu, );
- // 主角
- this._man = new Footballer_cn();
- this._man.flippedX = true;
- this._man.x = ;
- this._man.y = ;
- this._man.anchorX = 0.5;
- this._man.anchorY = ;
- this.addChild(this._man,,TAG_SPRITE_USER);
- this._npc = new Footballer_br();
- this._npc.x = size.width - ;
- this._npc.y = ;
- this._npc.anchorX = 0.5;
- this._npc.anchorY = ;
- this.addChild(this._npc,,TAG_SPRITE_NPC);
- // 球门
- this._leftGoal = new Goalpost(true);
- this._leftGoal.x = ;
- this._leftGoal.y = ;
- this._leftGoal.anchorX = ;
- this._leftGoal.anchorY = ;
- this.addChild(this._leftGoal);
- this._rightGoal = new Goalpost();
- this._rightGoal.x = size.width;
- this._rightGoal.y = ;
- this._rightGoal.anchorX = ;
- this._rightGoal.anchorY = ;
- this.addChild(this._rightGoal);
- // power
- this._leftPower = new PowerProgress(size.width/-,size.height-,,0.5,this);
- this.addChild(this._leftPower, );
- this._rightPower = new PowerProgress(size.width/+,size.height-,,0.5,this);
- this.addChild(this._rightPower, );
- // 系统计划任务,即每帧调用update函数
- this.scheduleUpdate();
- // 自定义计划任务
- this.schedule(this.uiSchedule, );
- cc.sys.dumpRoot();
- cc.sys.garbageCollect();
- this.initChipmunk();
- return true;
- },
- onStop:function () {
- this._bStop = !this._bStop;
- if (this._bStop == true) {
- cc.director.pause();
- }
- else {
- cc.director.resume();
- }
- },
- update:function (dt) {
- this.space.step(dt);
- },
- uiSchedule:function () {
- this._time++;
- this._leftPower.showPower();
- this._rightPower.showPower();
- },
- initChipmunk:function() {
- this.space = new cp.Space();
- var sprite = this.createPhysicsSprite( cc.p(cc.director.getWinSize().width/ , cc.director.getWinSize().height-) );
- this.addChild( sprite, );
- this.addWalls();
- this.space.gravity = cp.v(, -);
- },
- initPhysics:function() {
- var space = this.space ;
- var staticBody = space.staticBody;
- var winSize = cc.director.getWinSize();
- // Walls
- var walls = [ new cp.SegmentShape( staticBody, cp.v(,), cp.v(winSize.width,), ), // bottom
- new cp.SegmentShape( staticBody, cp.v(,winSize.height), cp.v(winSize.width,winSize.height), ), // top
- new cp.SegmentShape( staticBody, cp.v(,), cp.v(,winSize.height), ), // left
- new cp.SegmentShape( staticBody, cp.v(winSize.width,), cp.v(winSize.width,winSize.height), ) // right
- ];
- for( var i=; i < walls.length; i++ ) {
- var shape = walls[i];
- shape.setElasticity();
- shape.setFriction();
- space.addStaticShape( shape );
- }
- // Gravity
- space.gravity = cp.v(, -);
- },
- addWalls:function() {
- // Walls
- var winSize = cc.director.getWinSize();
- var walls = [ new cp.SegmentShape( this.space.staticBody, cp.v(,), cp.v(winSize.width,), ), // bottom
- new cp.SegmentShape( this.space.staticBody, cp.v(,winSize.height), cp.v(winSize.width,winSize.height), ), // top
- new cp.SegmentShape( this.space.staticBody, cp.v(,), cp.v(,winSize.height), ), // left
- new cp.SegmentShape( this.space.staticBody, cp.v(winSize.width,), cp.v(winSize.width,winSize.height), ) // right
- ];
- for( var i=; i < walls.length; i++ ) {
- var shape = walls[i];
- shape.setElasticity(0.8);
- shape.setFriction(0.1);
- this.space.addStaticShape( shape );
- }
- },
- createPhysicsSprite:function( pos ) {
- var radius = ;
- var mass = ;
- var body = new cp.Body(mass, cp.momentForCircle(mass, , radius,cp.v(, )));
- body.setPos( pos );
- this.space.addBody( body );
- var shape = new cp.CircleShape(body, radius,cp.v(, )); //new cp.BoxShape( body, 48, 108);
- shape.setElasticity( );
- shape.setFriction( 0.1 );
- this.space.addShape( shape );
- var sprite = cc.PhysicsSprite.create(res.b_ball_01);
- sprite.setBody( body );
- return sprite;
- },
- setupDebugNode:function()
- {
- // debug only
- this._debugNode = cc.PhysicsDebugNode.create( this.space );
- this._debugNode.visible = false ;
- this.addChild( this._debugNode );
- }
- });
- var MainScene = cc.Scene.extend({
- onEnter:function () {
- this._super();
- this.addChild(new GameBackgroundLayer());
- this.addChild(new MainLayer());
- }
- });
[cocos2d-js]chipmunk例子(二)的更多相关文章
- 【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
原文:[高德地图API]从零开始学高德JS API(二)地图控件与插件——测距.圆形编辑器.鼠标工具.地图类型切换.鹰眼鱼骨 摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装 ...
- qrcode.js 动态生成二维码
用qrcode.js动态生成二维码图片非常简单,只需要引入qrcode.js即可使用,而且可以自定义图片大小.背景色等信息. 1.jsp代码---页面头部引入qrcode.js,jquery文件可选 ...
- node.js 初学(二)—— 搭建注册/登录服务器
node.js 初学(二)—— 搭建注册/登录服务器 理论上来说,代码实现在理论和实际上是一样的.但实际上来说,他们不是 做一个最简单的用户注册登录功能 1.接口定义: 注册:/user?act=re ...
- js生成简单二维码
js文件下载地址:https://download.csdn.net/download/weixin_38296752/10554485 一.引入qrcode.js文件 <script type ...
- 进击Node.js基础(二)
一.一个牛逼闪闪的知识点Promise npm install bluebird 二.Promise实例 ball.html <!doctype> <!DOCTYPE html> ...
- js生成中文二维码
http://www.cnblogs.com/xcsn/archive/2013/08/14/3258035.html http://www.jb51.net/article/64928.htm 使用 ...
- cocos2d js jsb XMLHttpRequest 中文乱码
1.首先讲下怎样使用XMLHttpRequest 下面所说的是在cocos2d-x 2.2.2 或者 2.3 版本号中. 首先要明确cocos2d js事实上分两个版本号,一个是html5的版本号,另 ...
- cocos2d js的一些tip
cocos2d-js-v3.2-rc0 cc.director.end();//退出app cc.Application.getInstance().openURL("http://www. ...
- cocos2d js ClippingNode 制作标题闪亮特效
1.效果图: 之前在<Android 高仿 IOS7 IPhone 解锁 Slide To Unlock>中制作了文字上闪亮移动的效果,这次我们来看下怎样在cocos2d js 中做出类似 ...
- Cordova app 检查更新 ----JS进行调用(二)
原文:Cordova app 检查更新 ----JS进行调用(二) 1.获取版本号 需要添加 插件 cordova plugin add https://github.com/whiteoctober ...
随机推荐
- asp.net页面过滤所有换行符和多余空格
不知道大家注意到了没有,Google和Baidu网页的HTML源代码是混合在一起的.HTML代码混合在一起,出发点是为了减小网页体积,从而加快网页加载速度. 写个函数把网页HTML源代码的换行符和空格 ...
- 标准模板库(STL)学习探究之stack
标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string
- ACM - ICPC World Finals 2013 H Матрёшка
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...
- java连接mysql的一个小例子
想要用java 连接数据库,需要在classpath中加上jdbc的jar包路径 在eclipse中,Project的properties里面的java build path里面添加引用 连接成功的一 ...
- SecureCRT介绍、安装、使用
简介 SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. SecureCRT支持SSH,同时支持Telnet和 ...
- HDU 1851 (巴什博奕 SG定理) A Simple Game
这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...
- 漫游Kafka实战篇之客户端API
Kafka Producer APIs 旧版的Procuder API有两种:kafka.producer.SyncProducer和kafka.producer.async.AsyncProduce ...
- ASP.NET 4的Demo实践:URL路由改进支持
从.NET框架3.5 SP1开始,微软推出了ASP.NET路由支持,从而实现了特定资源的URL与其对应的Web服务器上的物理文件之间的彻底解耦.借助于ASP.NET路由支持,开发人员可以定义一组路由规 ...
- 指针数组vs数组指针 指针函数vs函数指针
在分辨这些重要的概念时,我们先回顾一下前面所讲的C之三值合一,由于三个值所求出的地址是相同的,所以经常有传言说他们都是首元素的地址.这种说法是不正确的.为什么说它是不正确的呢? 首先定义一个指针,将三 ...
- 【英语】Bingo口语笔记(45) - Pass系列