使用cocos2d-iphone实现割绳子游戏

jopen 13年前
     <p>参考资料:<a href="/misc/goto?guid=4959500828727273092">http://www.cocos2d-iphone.org/forum/topic/10411</a></p>    <p>这是我改写的一则绳子,绳子以body小节相连接</p>    <p>不过我还是觉得 verlet rope那种原型方式更加漂亮!</p>    <p>原型的缺点就是绳子一旦被切断,就要即刻清除所有相关内容,</p>    <p>一根绳子被切断后就这么凭空消失了,这总让人觉得有点儿说不过去吧?</p>    <p>于是我便想以body的形式改写一则更有质感的绳子,</p>    <p>这样的话,即使在被切断了之后,两段“绳子尸体”依然还能飘来荡去~</p>    <p> </p>    <p>学到了不少新的东西,例如CCSpriteBatchNode的正确使用方法~</p>    <p>以前我也用 CCSpriteBatchNode,但是用的方法不正确</p>    <p> </p>    <p>我现在是这么认识的,CCSpriteBatchNode加载一份图片文件,</p>    <p>其他的CCSprite都可以来使用这份图片,直接从内存里面取的,迅速快捷~</p>    <p>如果一份图片反复要在游戏里面用到,但是每次都通过 [CCSprite spriteWithFile:@"123.png"]的方式来取的话</p>    <p>就会存在每次都要从磁盘介质里面拿取这份图片,而读磁盘是很耗时间的,不管怎么说,一定比从内存读取慢!</p>    <p> </p>    <p>其实很多时候都要权衡利弊,从内存里面读取资源文件虽快,但是ios设备内存吃紧</p>    <p>将整个游戏几十兆的资源文件一次性全部加载到内存,程序很有可能会被强干掉</p>    <p>上次我查了一番加密资源文件的手法,有一种方式是将资源文件“打碎成”字节数据放在头文件里面</p>    <p>后来我一想,游戏一运行,那么装载头文件里面的资源文件肯定在第一时间就会被全部加载到内存</p>    <p>快是够快了,内存够不够用却又成问题了~</p>    <p>后来我留言给作者,作者的回复也是如同我所想,此种加密方式仅限于一些必须要保密的资源,</p>    <p>如防止别人篡改公司logo,就可能将logo图片置于头文件中~</p>    <p> </p>    <p>last,绳子其实还很有待挖掘,今后有时间的话,一定会力争写出更真实,更高效的绳子来~</p>    <p> </p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  BYRope.h</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  GoldMine0.6</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  Created by Bruce Yang on 12-1-13.</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  Copyright (c) 2012</span><span style="font:12px 'Heiti SC Light';">年</span><span style="font-size:12px;"> __MyCompanyName__. All rights reserved.</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#cb341a;"><span style="color:#754b2b;font-size:12px;">#import</span><span style="font-size:12px;"><Foundation/Foundation.h></span></p>    <p style="margin:0px;font:11px Menlo;color:#cb341a;"><span style="color:#754b2b;font-size:12px;">#import</span><span style="font-size:12px;">"cocos2d.h"</span></p>    <p style="margin:0px;font:11px Menlo;color:#cb341a;"><span style="color:#754b2b;font-size:12px;">#import</span><span style="font-size:12px;">"Box2D.h"</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#import</span><span style="color:#cb341a;font-size:12px;"><vector></span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define PTM_RATIO</span><span style="color:#4300d7;font-size:12px;">32</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;">// </span><span style="font-size:12px;">绳子小段图片的高度和宽度</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define ROPE_ITEM_WIDTH</span><span style="color:#4300d7;font-size:12px;">16.0f</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define ROPE_ITEM_HEIGHT</span><span style="color:#4300d7;font-size:12px;">4.0f</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;">// </span><span style="font-size:12px;">绳子</span><span style="font:12px Menlo;"> bar </span><span style="font-size:12px;">图片的高度和宽度</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define ROPE_BAR_DIAMETER</span><span style="color:#4300d7;font-size:12px;">8.0f</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;">// 1.</span><span style="font-size:12px;">是否在绳子的两个连接点之间启用</span><span style="font:12px Menlo;"> </span><span style="font-size:12px;">距离限定关节</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define B2_ROPE_JOINT_ENABLED NO</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">// 2.bar body</span><span style="font:12px 'Heiti SC Light';">到底是连接在绳子最外侧小节的</span><span style="font-size:12px;"> body </span><span style="font:12px 'Heiti SC Light';">上,还是连接在</span><span style="font-size:12px;"> comp </span><span style="font:12px 'Heiti SC Light';">元素的</span><span style="font-size:12px;"> body </span><span style="font:12px 'Heiti SC Light';">上</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define CONNECTED_WITH_COMP_BODY</span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">  </span><span style="color:#00870c;font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';color:#00870c;">紧随</span><span style="color:#00870c;font-size:12px;">comp</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define CONNECTED_WITH_ROPE_BODY</span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;">  </span><span style="color:#00870c;font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';color:#00870c;">紧随绳子最外侧小节</span><span style="color:#00870c;font-size:12px;">(</span><span style="font:12px 'Heiti SC Light';color:#00870c;">因为会随着绳子抖动,所以效果感觉不怎么样</span><span style="color:#00870c;font-size:12px;">)~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="font-size:12px;">#define ROPE_BAR_CONNECT_MODE</span><span style="color:#4300d7;font-size:12px;">0</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">/**</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> * added by BruceYang on 2012.01.15.06.22</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> * </span><span style="font:12px 'Heiti SC Light';">对</span><span style="font-size:12px;"> Box2D b2Math.h </span><span style="font:12px 'Heiti SC Light';">做功能扩充</span><span style="font-size:12px;">(c</span><span style="font:12px 'Heiti SC Light';">语言</span><span style="font-size:12px;"> </span><span style="font:12px 'Heiti SC Light';">运算符重载的用法</span><span style="font-size:12px;">)~</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;"> * </span><span style="font-size:12px;">之所以放在该类里面而不直接去</span><span style="font:12px Menlo;"> box2d</span><span style="font-size:12px;">源码里面修改,主要还是为了升级</span><span style="font:12px Menlo;"> box2d </span><span style="font-size:12px;">库的时候不出现方法丢失的问题</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;"> * </span><span style="font-size:12px;">可想而知,如果这些代码是插进</span><span style="font:12px Menlo;"> box2d</span><span style="font-size:12px;">源码里面的话,升级的时候一个替换,这些我自己新添加的东西肯定就要丢失了</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> */</span></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">inline</span><span style="color:#000000;font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="color:#000000;font-size:12px;"> </span><span style="font-size:12px;">operator</span><span style="color:#000000;font-size:12px;"> * (</span><span style="font-size:12px;">const</span><span style="color:#000000;font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="color:#000000;font-size:12px;">& a, </span><span style="color:#548087;font-size:12px;">float32</span><span style="color:#000000;font-size:12px;"> s) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">return</span><span style="font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">(s * a.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, s * a.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">inline</span><span style="color:#000000;font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="color:#000000;font-size:12px;"> </span><span style="font-size:12px;">operator</span><span style="color:#000000;font-size:12px;"> / (</span><span style="font-size:12px;">const</span><span style="color:#000000;font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="color:#000000;font-size:12px;">& a, </span><span style="color:#548087;font-size:12px;">float32</span><span style="color:#000000;font-size:12px;"> s) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">return</span><span style="font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">(a.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;"> / s, a.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;"> / s);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">inline</span><span style="font-size:12px;"> </span><span style="color:#548087;font-size:12px;">float32</span><span style="font-size:12px;"> b2Vec2ToAngle(</span><span style="color:#b81aa1;font-size:12px;">const</span><span style="font-size:12px;"> </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">& v) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">return</span><span style="font-size:12px;"> </span><span style="color:#430280;font-size:12px;">atan2f</span><span style="font-size:12px;">(v.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">, v.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">@interface</span><span style="font-size:12px;"> BYRope :</span><span style="color:#742ea8;font-size:12px;">NSObject</span><span style="font-size:12px;"> {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    </span><span style="color:#00870c;font-size:12px;">// ~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">/**</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> * p1</span><span style="font:12px 'Heiti SC Light';">为在</span><span style="font-size:12px;"> body1 fxitures </span><span style="font:12px 'Heiti SC Light';">内部的一个世界坐标点</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> * p2</span><span style="font:12px 'Heiti SC Light';">为在</span><span style="font-size:12px;"> body2 fxitures </span><span style="font:12px 'Heiti SC Light';">内部的一个世界坐标点</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> * itemBatch</span><span style="font:12px 'Heiti SC Light';">装载绳子小节图片</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;"> * barBatch </span><span style="font-size:12px;">装载绳子端点圆形小图片</span><span style="font:12px Menlo;">(</span><span style="font-size:12px;">若传入参数</span><span style="font:12px Menlo;">nil</span><span style="font-size:12px;">,则绳子两端不包含圆形小图片</span><span style="font:12px Menlo;">)~</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;"> */</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">-(</span><span style="color:#b81aa1;font-size:12px;">id</span><span style="font-size:12px;">) init:(</span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;">*)body1 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    point1:(</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">)p1 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">     body2:(</span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;">*)body2 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    point2:(</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">)p2 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;"> itemBatch:(</span><span style="color:#548087;font-size:12px;">CCSpriteBatchNode</span><span style="font-size:12px;">*)itemBatch </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">  barBatch:(</span><span style="color:#548087;font-size:12px;">CCSpriteBatchNode</span><span style="font-size:12px;">*)barBatch;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">-(</span><span style="color:#b81aa1;font-size:12px;">void</span><span style="font-size:12px;">) dealloc;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">@end</span></p>    <div>     <br />    </div>    <br />    <p><span style="font-size:12px;"> </span></p>    <p><span style="font-size:12px;"> </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  BYRope.mm</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  GoldMine0.6</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  Created by Bruce Yang on 12-1-13.</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//  Copyright (c) 2012</span><span style="font:12px 'Heiti SC Light';">年</span><span style="font-size:12px;"> __MyCompanyName__. All rights reserved.</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">//</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#cb341a;"><span style="color:#754b2b;font-size:12px;">#import</span><span style="font-size:12px;">"BYRope.h"</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">@implementation</span><span style="color:#000000;font-size:12px;"> BYRope</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">-(</span><span style="color:#b81aa1;font-size:12px;">id</span><span style="font-size:12px;">) init:(</span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;">*)body1 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    point1:(</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">)p1 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">     body2:(</span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;">*)body2 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    point2:(</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">)p2 </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;"> itemBatch:(</span><span style="color:#548087;font-size:12px;">CCSpriteBatchNode</span><span style="font-size:12px;">*)itemBatch </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">  barBatch:(</span><span style="color:#548087;font-size:12px;">CCSpriteBatchNode</span><span style="font-size:12px;">*)barBatch </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">{</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#b81aa1;font-size:12px;">if</span><span style="font-size:12px;">((</span><span style="color:#b81aa1;font-size:12px;">self</span><span style="font-size:12px;"> = [</span><span style="color:#b81aa1;font-size:12px;">super</span><span style="font-size:12px;"> </span><span style="color:#430280;font-size:12px;">init</span><span style="font-size:12px;">])) {</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;color:#000000;">        </span><span style="font:12px Menlo;">// world</span><span style="font-size:12px;">完全没必要用一个参数传进来,直接从</span><span style="font:12px Menlo;"> body </span><span style="font-size:12px;">里面取出来就是了</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2World</span><span style="font-size:12px;"> *world = body1-></span><span style="color:#35585d;font-size:12px;">GetWorld</span><span style="font-size:12px;">();</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#742ea8;font-size:12px;">vector</span><span style="font-size:12px;"><</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">> vPoints;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">       </span><span style="font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';">计算两点之间的距离</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">float</span><span style="font-size:12px;"> distance = </span><span style="color:#35585d;font-size:12px;">b2Distance</span><span style="font-size:12px;">(p1, p2);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;color:#000000;">        </span><span style="font:12px Menlo;">//</span><span style="font-size:12px;">定义绳子的</span><span style="font:12px Menlo;"> “</span><span style="font-size:12px;">每段平均长度</span><span style="font:12px Menlo;">”</span><span style="font-size:12px;">:长度越短,段数越多;长度越长,段数越少</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">float</span><span style="font-size:12px;"> segmentLength = </span><span style="color:#754b2b;font-size:12px;">ROPE_ITEM_WIDTH</span><span style="font-size:12px;"> /</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">int</span><span style="font-size:12px;"> pointsCount = distance / segmentLength + </span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">       </span><span style="font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';">两点间的矢量差</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> diffVector = p2 - p1;</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">       </span><span style="font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';">求出单位向量</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        diffVector.</span><span style="color:#35585d;font-size:12px;">Normalize</span><span style="font-size:12px;">();</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">float</span><span style="font-size:12px;"> multiplier = distance / (pointsCount - </span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">for</span><span style="font-size:12px;">(</span><span style="color:#b81aa1;font-size:12px;">int</span><span style="font-size:12px;"> i = </span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">; i < pointsCount; ++ i) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> vector = p1 + multiplier * i * diffVector;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            vPoints.</span><span style="color:#430280;font-size:12px;">push_back</span><span style="font-size:12px;">(vector);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;"> *prevBody = body1;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2BodyDef</span><span style="font-size:12px;"> bodyDef;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;">* body;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2Fixture</span><span style="font-size:12px;">* fixture;</span></p>    <p style="margin:0px;font:11px Menlo;color:#548087;"><span style="color:#000000;font-size:12px;">       </span><span style="font-size:12px;">b2RevoluteJointDef</span><span style="color:#000000;font-size:12px;"> jd;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;"> *firstBd, *lastBd;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">for</span><span style="font-size:12px;">(</span><span style="color:#b81aa1;font-size:12px;">int</span><span style="font-size:12px;"> i = </span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">; i < pointsCount-</span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;">; ++ i) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> p1_ = vPoints.</span><span style="color:#430280;font-size:12px;">at</span><span style="font-size:12px;">(i);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> p2_ = vPoints.</span><span style="color:#430280;font-size:12px;">at</span><span style="font-size:12px;">(i+</span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> stickVector = p1_ - p2_;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#b81aa1;font-size:12px;">float</span><span style="font-size:12px;"> stickAngle = </span><span style="color:#35585d;font-size:12px;">b2Vec2ToAngle</span><span style="font-size:12px;">(stickVector);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> midPoint = (p1_ + p2_) / </span><span style="color:#4300d7;font-size:12px;">2.0f</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            bodyDef.</span><span style="color:#548087;font-size:12px;">type</span><span style="font-size:12px;"> =</span><span style="color:#35585d;font-size:12px;">b2_dynamicBody</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            body = world-></span><span style="color:#35585d;font-size:12px;">CreateBody</span><span style="font-size:12px;">(&bodyDef);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;color:#000000;">            </span><span style="font:12px Menlo;">//</span><span style="font-size:12px;">如果是绳子的第一个或最后一个关节,则不可以被切割</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="font-size:12px;">(i == </span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                firstBd = body;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                body-></span><span style="color:#548087;font-size:12px;">m_isCuttable</span><span style="font-size:12px;"> =</span><span style="color:#b81aa1;font-size:12px;">false</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2PolygonShape</span><span style="font-size:12px;"> boxShape;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            boxShape.</span><span style="color:#35585d;font-size:12px;">SetAsBox</span><span style="font-size:12px;">(multiplier/</span><span style="color:#4300d7;font-size:12px;">2.0f</span><span style="font-size:12px;">,</span><span style="color:#754b2b;font-size:12px;">ROPE_ITEM_HEIGHT</span><span style="font-size:12px;">/</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">/</span><span style="color:#4300d7;font-size:12px;">2.0f</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            fixture = body-></span><span style="color:#35585d;font-size:12px;">CreateFixture</span><span style="font-size:12px;">(&boxShape,</span><span style="color:#4300d7;font-size:12px;">1.0f</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">           </span><span style="font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';">设置绳子单元不响应碰撞</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            fixture-></span><span style="color:#35585d;font-size:12px;">SetSensor</span><span style="font-size:12px;">(</span><span style="color:#b81aa1;font-size:12px;">true</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            body-></span><span style="color:#35585d;font-size:12px;">SetTransform</span><span style="font-size:12px;">(</span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;">(midPoint.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, midPoint.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">), stickAngle);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#00870c;font-size:12px;">/**</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;">             * </span><span style="font-size:12px;">如果将下面这块代码放到上面那块代码的前面,会出现很奇葩的现象:</span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;">             * </span><span style="font-size:12px;">所预期的绳子关节不会建立起来,绳子小段会到处乱飞,</span><span style="font:12px Menlo;">sprite</span><span style="font-size:12px;">图片也会一个个从场景中消失不见</span><span style="font:12px Menlo;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">             *</span><span style="font:12px 'Heiti SC Light';">猜测原因可能是因为</span><span style="font-size:12px;"> body </span><span style="font:12px 'Heiti SC Light';">创建出来的时候</span><span style="font-size:12px;"> position </span><span style="font:12px 'Heiti SC Light';">还没有确定下来</span><span style="font-size:12px;">!!</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="font-size:12px;">             */</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            jd.</span><span style="color:#548087;font-size:12px;">collideConnected</span><span style="font-size:12px;"> =</span><span style="color:#b81aa1;font-size:12px;">false</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> anchor(p1_.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, p1_.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(prevBody, body, anchor);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&jd);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            prevBody = body;</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="font-size:12px;">(i == pointsCount-</span><span style="color:#4300d7;font-size:12px;">2</span><span style="font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                lastBd = body;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                body-></span><span style="color:#548087;font-size:12px;">m_isCuttable</span><span style="font-size:12px;"> =</span><span style="color:#b81aa1;font-size:12px;">false</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#548087;font-size:12px;">collideConnected</span><span style="font-size:12px;"> =</span><span style="color:#b81aa1;font-size:12px;">false</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                </span><span style="color:#548087;font-size:12px;">b2Vec2</span><span style="font-size:12px;"> lastAnchor(p2_.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, p2_.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(body, body2, lastAnchor);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&jd);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#742ea8;font-size:12px;">CGRect</span><span style="font-size:12px;"> spriteRect = </span><span style="color:#430280;font-size:12px;">CGRectMake</span><span style="font-size:12px;">(</span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">,</span><span style="color:#4300d7;font-size:12px;">0</span><span style="font-size:12px;">, multiplier*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">,</span><span style="color:#754b2b;font-size:12px;">ROPE_ITEM_HEIGHT</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> *itemSprite = [</span><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> </span><span style="color:#35585d;font-size:12px;">spriteWithBatchNode</span><span style="font-size:12px;">:itemBatch </span><span style="color:#430280;font-size:12px;">rect</span><span style="font-size:12px;">:spriteRect];</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="color:#548087;font-size:12px;">ccTexParams</span><span style="color:#000000;font-size:12px;"> params = {</span><span style="font-size:12px;">GL_LINEAR</span><span style="color:#000000;font-size:12px;">, </span><span style="font-size:12px;">GL_LINEAR</span><span style="color:#000000;font-size:12px;">,</span><span style="font-size:12px;">GL_REPEAT</span><span style="color:#000000;font-size:12px;">, </span><span style="font-size:12px;">GL_REPEAT</span><span style="color:#000000;font-size:12px;"> };</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">[itemSprite.</span><span style="color:#742ea8;font-size:12px;">texture</span><span style="font-size:12px;"> </span><span style="color:#35585d;font-size:12px;">setTexParameters</span><span style="font-size:12px;">:&params];</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">[itemSprite</span><span style="color:#430280;font-size:12px;">setPosition</span><span style="font-size:12px;">:</span><span style="color:#754b2b;font-size:12px;">ccp</span><span style="font-size:12px;">(midPoint.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">, midPoint.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">)];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">[itemSprite</span><span style="color:#35585d;font-size:12px;">setRotation</span><span style="font-size:12px;">:-</span><span style="color:#4300d7;font-size:12px;">1</span><span style="font-size:12px;"> *</span><span style="color:#754b2b;font-size:12px;">CC_RADIANS_TO_DEGREES</span><span style="font-size:12px;">(stickAngle)];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">[itemBatch</span><span style="color:#35585d;font-size:12px;">addChild</span><span style="font-size:12px;">:itemSprite];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            body-></span><span style="color:#35585d;font-size:12px;">SetUserData</span><span style="font-size:12px;">(itemSprite);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px 'Heiti SC Light';color:#00870c;"><span style="font:12px Menlo;color:#000000;">        </span><span style="font:12px Menlo;">//</span><span style="font-size:12px;">创建绳子关节,限定两物体之间的最大距离</span><span style="font:12px Menlo;">(</span><span style="font-size:12px;">解决了绳子与被连接物体碰撞的问题,这个绳子关节便没必要创建了</span><span style="font:12px Menlo;">)~</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="color:#000000;font-size:12px;">       </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="color:#000000;font-size:12px;">(</span><span style="font-size:12px;">B2_ROPE_JOINT_ENABLED</span><span style="color:#000000;font-size:12px;"> ==</span><span style="color:#b81aa1;font-size:12px;">YES</span><span style="color:#000000;font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2RopeJointDef</span><span style="font-size:12px;"> rjd;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            rjd.</span><span style="color:#548087;font-size:12px;">bodyA</span><span style="font-size:12px;"> = body1;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            rjd.</span><span style="color:#548087;font-size:12px;">bodyB</span><span style="font-size:12px;"> = body2;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            rjd.</span><span style="color:#548087;font-size:12px;">localAnchorA</span><span style="font-size:12px;"> = p1-body1-></span><span style="color:#35585d;font-size:12px;">GetPosition</span><span style="font-size:12px;">();</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            rjd.</span><span style="color:#548087;font-size:12px;">localAnchorB</span><span style="font-size:12px;"> = p2-body2-></span><span style="color:#35585d;font-size:12px;">GetPosition</span><span style="font-size:12px;">();</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            rjd.</span><span style="color:#548087;font-size:12px;">maxLength</span><span style="font-size:12px;"> = (p2 - p1).</span><span style="color:#35585d;font-size:12px;">Length</span><span style="font-size:12px;">();</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&rjd);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">        </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">       </span><span style="font-size:12px;">// </span><span style="font:12px 'Heiti SC Light';">添加绳子端点的图片</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="font-size:12px;">(barBatch != </span><span style="color:#b81aa1;font-size:12px;">nil</span><span style="font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">           </span><span style="font-size:12px;">// 1.</span><span style="font:12px 'Heiti SC Light';">创建绳子端点上的</span><span style="font-size:12px;">body(</span><span style="font:12px 'Heiti SC Light';">给绳子</span><span style="font-size:12px;"> bar</span><span style="font:12px 'Heiti SC Light';">依附用</span><span style="font-size:12px;">)~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2BodyDef</span><span style="font-size:12px;"> bd;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            bd.</span><span style="color:#548087;font-size:12px;">type</span><span style="font-size:12px;"> =</span><span style="color:#35585d;font-size:12px;">b2_dynamicBody</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">bd.</span><span style="color:#548087;font-size:12px;">position</span><span style="font-size:12px;">.</span><span style="color:#35585d;font-size:12px;">Set</span><span style="font-size:12px;">(p1.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, p1.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;"> *b1 = world-></span><span style="color:#35585d;font-size:12px;">CreateBody</span><span style="font-size:12px;">(&bd); </span><span style="color:#00870c;font-size:12px;">// p1 </span><span style="font:12px 'Heiti SC Light';color:#00870c;">点的</span><span style="color:#00870c;font-size:12px;"> body~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">bd.</span><span style="color:#548087;font-size:12px;">position</span><span style="font-size:12px;">.</span><span style="color:#35585d;font-size:12px;">Set</span><span style="font-size:12px;">(p2.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">, p2.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">b2Body</span><span style="font-size:12px;"> *b2 = world-></span><span style="color:#35585d;font-size:12px;">CreateBody</span><span style="font-size:12px;">(&bd); </span><span style="color:#00870c;font-size:12px;">// p2 </span><span style="font:12px 'Heiti SC Light';color:#00870c;">点的</span><span style="color:#00870c;font-size:12px;"> body~</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">           </span><span style="font-size:12px;">// 2.</span><span style="font:12px 'Heiti SC Light';">将端点</span><span style="font-size:12px;"> body </span><span style="font:12px 'Heiti SC Light';">连接到绳子最外侧的两个绳子小节</span><span style="font-size:12px;">~</span></p>    <p style="margin:0px;font:11px Menlo;color:#548087;"><span style="color:#000000;font-size:12px;">           </span><span style="font-size:12px;">b2RevoluteJointDef</span><span style="color:#000000;font-size:12px;"> jd;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            jd.</span><span style="color:#548087;font-size:12px;">collideConnected</span><span style="font-size:12px;"> =</span><span style="color:#b81aa1;font-size:12px;">false</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="color:#000000;font-size:12px;">           </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="color:#000000;font-size:12px;">(</span><span style="font-size:12px;">ROPE_BAR_CONNECT_MODE</span><span style="color:#000000;font-size:12px;"> ==</span><span style="font-size:12px;">CONNECTED_WITH_COMP_BODY</span><span style="color:#000000;font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(b1, body1, p1);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&jd);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(b2, body2, p2);</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="color:#000000;font-size:12px;">            }</span><span style="color:#b81aa1;font-size:12px;">else</span><span style="color:#000000;font-size:12px;"> </span><span style="color:#b81aa1;font-size:12px;">if</span><span style="color:#000000;font-size:12px;">(</span><span style="font-size:12px;">ROPE_BAR_CONNECT_MODE</span><span style="color:#000000;font-size:12px;"> ==</span><span style="font-size:12px;">CONNECTED_WITH_ROPE_BODY</span><span style="color:#000000;font-size:12px;">) {</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(b1, firstBd, p1);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&jd);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">                jd.</span><span style="color:#35585d;font-size:12px;">Initialize</span><span style="font-size:12px;">(b2, lastBd, p2);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            }</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            world-></span><span style="color:#35585d;font-size:12px;">CreateJoint</span><span style="font-size:12px;">(&jd);</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"><span style="font-size:12px;">            </span></p>    <p style="margin:0px;font:11px Menlo;color:#00870c;"><span style="color:#000000;font-size:12px;">           </span><span style="font-size:12px;">// 3.</span><span style="font:12px 'Heiti SC Light';">创建绳子</span><span style="font-size:12px;"> bar sprite~</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">CCTexture2D</span><span style="font-size:12px;"> *tex2d = barBatch.</span><span style="color:#548087;font-size:12px;">textureAtlas</span><span style="font-size:12px;">.</span><span style="color:#548087;font-size:12px;">texture</span><span style="font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;color:#754b2b;"><span style="color:#000000;font-size:12px;">           </span><span style="color:#742ea8;font-size:12px;">CGRect</span><span style="color:#000000;font-size:12px;"> barRect =</span><span style="color:#430280;font-size:12px;">CGRectMake</span><span style="color:#000000;font-size:12px;">(</span><span style="color:#4300d7;font-size:12px;">0</span><span style="color:#000000;font-size:12px;">,</span><span style="color:#4300d7;font-size:12px;">0</span><span style="color:#000000;font-size:12px;">, </span><span style="font-size:12px;">ROPE_BAR_DIAMETER</span><span style="color:#000000;font-size:12px;">,</span><span style="font-size:12px;">ROPE_BAR_DIAMETER</span><span style="color:#000000;font-size:12px;">);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> *spriteA = [</span><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> </span><span style="color:#35585d;font-size:12px;">spriteWithTexture</span><span style="font-size:12px;">:tex2d </span><span style="color:#430280;font-size:12px;">rect</span><span style="font-size:12px;">:barRect];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            [spriteA</span><span style="color:#430280;font-size:12px;">setPosition</span><span style="font-size:12px;">:</span><span style="color:#754b2b;font-size:12px;">ccp</span><span style="font-size:12px;">(p1.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">, p1.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">)];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            [barBatch</span><span style="color:#35585d;font-size:12px;">addChild</span><span style="font-size:12px;">:spriteA];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            b1-></span><span style="color:#35585d;font-size:12px;">SetUserData</span><span style="font-size:12px;">(spriteA);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            </span><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> *spriteB = [</span><span style="color:#548087;font-size:12px;">CCSprite</span><span style="font-size:12px;"> </span><span style="color:#35585d;font-size:12px;">spriteWithTexture</span><span style="font-size:12px;">:tex2d </span><span style="color:#430280;font-size:12px;">rect</span><span style="font-size:12px;">:barRect];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            [spriteB</span><span style="color:#430280;font-size:12px;">setPosition</span><span style="font-size:12px;">:</span><span style="color:#754b2b;font-size:12px;">ccp</span><span style="font-size:12px;">(p2.</span><span style="color:#548087;font-size:12px;">x</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">, p2.</span><span style="color:#548087;font-size:12px;">y</span><span style="font-size:12px;">*</span><span style="color:#754b2b;font-size:12px;">PTM_RATIO</span><span style="font-size:12px;">)];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            [barBatch</span><span style="color:#35585d;font-size:12px;">addChild</span><span style="font-size:12px;">:spriteB];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">            b2-></span><span style="color:#35585d;font-size:12px;">SetUserData</span><span style="font-size:12px;">(spriteB);</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">        }</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">return</span><span style="color:#000000;font-size:12px;"> </span><span style="font-size:12px;">self</span><span style="color:#000000;font-size:12px;">;</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">-(</span><span style="color:#b81aa1;font-size:12px;">void</span><span style="font-size:12px;">)dealloc{</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">    [</span><span style="color:#b81aa1;font-size:12px;">super</span><span style="font-size:12px;"> </span><span style="color:#430280;font-size:12px;">dealloc</span><span style="font-size:12px;">];</span></p>    <p style="margin:0px;font:11px Menlo;"><span style="font-size:12px;">}</span></p>    <p style="margin:0px;min-height:13px;font:11px Menlo;"></p>    <p style="margin:0px;font:11px Menlo;color:#b81aa1;"><span style="font-size:12px;">@end</span></p>    <div>     <br />    </div> 转自:    <a href="/misc/goto?guid=4959500828811917395" target="_blank">http://blog.csdn.net/yang3wei/article/details/7202267</a>