对于不规则的精灵我们可以借助PhysicsEditor来制作shape ,

对于地图可以使用Tiled软件制作瓷砖地图。

今天主要记录一下如何把CCSprite与不规则刚体进行绑定,然后一起移动

//初始化玩家

1.加载shape文件,在init方法中添加:

//加载shape文件
[[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"physicShape.plist"];

.plist文件内容大体如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!-- created with http://www.physicseditor.de --> <plist version="1.0">
<dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>1</integer>
<key>ptm_ratio</key>
<real>32</real>
</dict>
<key>bodies</key>
<dict> <key>Player1</key>
<dict>
<key>anchorpoint</key>
<string>{ 0.0000,0.0000 }</string>
<key>fixtures</key>
<array> <dict>
<key>density</key> <real>2</real>
<key>friction</key> <real>0</real>
<key>restitution</key> <real>0</real>
<key>filter_categoryBits</key> <integer>1</integer>
<key>filter_groupIndex</key> <integer>0</integer>
<key>filter_maskBits</key> <integer>65535</integer>
<key>isSensor</key> <false/>
<key>id</key> <string></string>
<key>fixture_type</key> <string>POLYGON</string> <key>polygons</key>
<array>
<array>
<string>{ 1.000,0.000 }</string>
<string>{ 32.000,0.000 }</string>
<string>{ 27.000,32.000 }</string>
<string>{ 7.000,32.000 }</string>
</array>
</array>
</dict>
</array>
</dict>
<key>RetroCoin</key>
<dict>
<key>anchorpoint</key>
<string>{ 0.0000,0.0000 }</string>
<key>fixtures</key>
<array> <dict>
<key>density</key> <real>2</real>
<key>friction</key> <real>0</real>
<key>restitution</key> <real>0</real>
<key>filter_categoryBits</key> <integer>1</integer>
<key>filter_groupIndex</key> <integer>0</integer>
<key>filter_maskBits</key> <integer>65535</integer>
<key>isSensor</key> <false/>
<key>id</key> <string></string>
<key>fixture_type</key> <string>POLYGON</string> <key>polygons</key>
<array>
<array>
<string>{ 4.000,3.000 }</string>
<string>{ 17.000,0.000 }</string>
<string>{ 28.000,3.000 }</string>
<string>{ 28.000,28.000 }</string>
<string>{ 16.000,32.000 }</string>
<string>{ 4.000,28.000 }</string>
</array>
</array>
</dict>
</array>
</dict>
</dict>
</dict>
</plist>

2.初始化精灵和刚体及夹具

#pragma mark 初始化玩家
-(void)initPlayer{
//创建对象层 获得对象的产生点
CCTMXObjectGroup *objects=[_gameMap objectGroupNamed:@"objects"];
NSMutableDictionary *spawnPoint=[objects objectNamed:@"StartPoint"];
//获得出生点坐标
float x=[[spawnPoint valueForKey:@"x"] floatValue];
float y=[[spawnPoint valueForKey:@"y"] floatValue];
_player=[CCSprite spriteWithFile:@"Player1.png"];
_player.position=ccp(x,y);
_player.anchorPoint=CGPointZero; //设置描点为0,0 否则与刚体不能重合
b2BodyDef playerBodyDef; //定义刚体结构体的定义
[self addChild:_player]; playerBodyDef.type=b2_dynamicBody;
playerBodyDef.fixedRotation=true; //不会旋转
playerBodyDef.position.Set(x/PTM_RATIO, y/PTM_RATIO); _playBody=world->CreateBody(&playerBodyDef);
_playBody->SetUserData(_player);
[[GB2ShapeCache sharedShapeCache] addFixturesToBody:_playBody forShapeName:@"Player1"]; //为刚体设置夹具 }

3.在update函数中添加更新位置方法

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {

        if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData(); //获得精灵对象
if (sprite != nil) {
b2Vec2 bodyPos = b->GetPosition(); //获得刚体的位置
CGPoint pos = CGPointMake(bodyPos.x * PTM_RATIO, bodyPos.y * PTM_RATIO); //变换为坐标
float32 rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
sprite.position = pos;
sprite.rotation = rotation;
}
}
}

效果图:

转载请注明:版权所有http://1.wildcat.sinaapp.com/

cocos2d(CCSprite绑定不规则刚体与精灵一起移动)的更多相关文章

  1. 如何在Cocos2D 1.0 中掩饰一个精灵(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...

  2. 如何在Cocos2D 1.0 中掩饰一个精灵(四)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了完成需要的效果,我们将使用如下策略: 我们将首先绘制掩饰精灵 ...

  3. 如何在Cocos2D 1.0 中掩饰一个精灵(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原帖来自Ray Wunderlich写的精彩的文章 How To ...

  4. cocos2d(CCSprite 用贝塞尔做抛物线,足球精灵并且同时做旋转放大效果)

    今天刚学到Cocos2d中的动作哪一张,自己做了一个用贝塞尔曲线足球精灵实现同时放大旋转和抛物线动作. 使用 [CCSpawn actions:,,]链接这几个动作,同时做.与CCSequence(一 ...

  5. 如何在Cocos2D 1.0 中掩饰一个精灵(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰和CCRenderTexture CCRenderTextu ...

  6. 如何在Cocos2D 1.0 中掩饰一个精灵(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 让我们开始吧 打开Xcode,从New Project中选择co ...

  7. Cocos2d Box2D之静态刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_staticBody 在模拟环境下静态物体是不会移动的,就好像有无限大的质量.在Box2D的内部会将质量至反,存储为零.静态物体也可 ...

  8. 关于CCSprite改变box2d刚体位置以及角度。

    同事今天在讨论一个事情,box2d中,body不可以直接设置位置,这样是不合理的,因为在物理的世界,你去左右它的物理检测.它就没有存在的必要了.但是,有人就想直接用box2d的碰撞.不用物理模拟.怎么 ...

  9. Cocos2d Box2D之浮动刚体

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. b2_kinematicBody 运动学物体在模拟环境中根据自身的速度进行移动.运动学物体自身不受力的作用.虽然用户可以手动移动它,但是通 ...

随机推荐

  1. PHP连接Access数据库代码

    使用php的odbc函数,不创建数据源. $connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath(" ...

  2. 【BZOJ3450】【Tyvj1952】Easy 可能DP

    联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  3. ios结构体httpPost头结构

    ios结构体httpPost头结构 by 吴雪莹 NSString* urlStr = @"; NSURL* url = [NSURL URLWithString:urlStr]; NSMu ...

  4. Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串

    原文:Winform: use the WebBrowser to display XML with xslt, xml, xslt 转 html 字符串 声明xml字符串: string xml = ...

  5. The Swift Programming Language-官方教程精译Swift(1)小试牛刀

    通常来说,编程语言教程中的第一个程序应该在屏幕上打印“Hello, world”.在 Swift 中,可以用一行代码实现:  println("hello, world") 如果你 ...

  6. NGUI使用教程(2) 使用NGUI创建2D场景而且加入标签和button

    1.创建2D场景 要使用NGUI创建2D场景,首先咱们必须新建一个项目,而且导入NGUI作为这个项目的插件,相信假设看过上一篇教程都知道怎么导入NGUI了,这里就不赘述,假设有疑问的能够去看上一篇教程 ...

  7. SQL语句分享[不定期更新]

    查询临时表 if object_id('')>0 查询表中的数据 select 'insert into ta1(col1,col2,col3) values('''+ltrim(列1)+''' ...

  8. JSP+Java+properties+FileInputStream文件读写,JSP页面读取properties文件

    String realPath = request.getRealPath("WEB-INF/classes/com/properties/devicetype.properties&quo ...

  9. 表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法

    原文:表单验证的3个函数ISSET().empty().is_numeric()的使用方法 本文就简单讲一下php中表单验证的三个函数,应该比较常用吧,最后给一些示例,请看下文. ISSET();—— ...

  10. jQuery中wrap、wrapAll和wrapInner用法以及区别

    原文: <ul>   <li title='苹果'>苹果</li>   <li title='橘子'>橘子</li>   <li ti ...