【问题标题】:Cocos2d Chipmunk: Touch Shapes?Cocos2d 花栗鼠:触摸形状?
【发布时间】:2011-02-24 21:40:59
【问题描述】:

今天我有一个cocos2d问题!

我使用节点在我的 .m 文件中设置了一些 shapesspace-manager

- (CCNode*) createBlockAt:(cpVect)pt

                    width:(int)w
                   height:(int)h
                     mass:(int)mass
{
    cpShape *shape = [smgr addRectAt:pt mass:mass width:w height:h rotation:0];

    cpShapeNode *node = [cpShapeNode nodeWithShape:shape];
    node.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);

    [self addChild:node];

    return node;
}

- (CCNode*) createCircleAt:(cpVect)pt
                     mass:(int)mass
                     radius:(int)radius
{
    cpShape *shape = [smgr addCircleAt:pt mass:mass radius:radius];
    cpShapeNode *node1 = [cpShapeNode nodeWithShape:shape];
    CCSprite *sprt = [CCSprite spriteWithFile:@"fire.png"];
    node1.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);

    [self addChild:node1];

    return node1;
}

然后我实际制作形状,设置背景,分配并在我的 init 方法中启动空间管理器:

- (id) init
{
    [super init];

    CCSprite *background = [CCSprite spriteWithFile:@"BGP.png"];
    background.position = ccp(240,160);
    [self addChild:background]; 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

    //allocate our space manager
    smgr = [[SpaceManagerCocos2d alloc] init];
    smgr.constantDt = 1/55.0; 

    [smgr addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpvzero];

    [self createBlockAt:cpv(160,50) width:50 height:100 mass:100];
    [self createBlockAt:cpv(320,50) width:50 height:100 mass:100];
    [self createBlockAt:cpv(240,110) width:210 height:20 mass:100];
    [self createCircleAt:cpv(240,140) mass:25 radius:20];

    [smgr start];

    return self;
}

现在,我希望某个形状在被触摸时被移除。最好的方法是什么??? 我希望有人可以帮助我:)

编辑: 请某人,开始赏金!或者我从来没有得到这个答案:(

-DD

【问题讨论】:

    标签: iphone cocos2d-iphone touch shape chipmunk


    【解决方案1】:

    这样怎么样?

    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint point = [self convertTouchToNodeSpace:touch];
        cpShape *shape = [smgr getShapeAt:point];
        if (shape) {
            [self removeChild:shape->data cleanup:YES];
            [smgr removeAndFreeShape:shape];
        }
        return YES;
    }
    

    【讨论】:

    • 您好!我尝试了这个并得到了导致我的游戏崩溃的警告:第一个:“控制达到非无效功能”(我通过添加 return 0; 在最后删除它)。第二个:'游戏'可能没有响应'-remove child'我应该如何处理第二个警告?谢谢你的帮助!!
    • 抱歉,我修复了警告,使用 removeChild:cleanup:.
    • 天哪,它工作!!!! :D :D 谢谢你帮我解决了我的问题!!!但我不会让你那么容易 XD 我还有一个问题:我在屏幕上有 2 个矩形,它们形成一个拱形。在这两个之上,我有第三个矩形,在它之上,我有一个球。如果我按下下面两个矩形之一,它们就会消失并且 smgr 继续工作。但是如果我按下上面的矩形,它就会消失,但由于某种原因,球不会掉下来。你能帮我解决这个问题吗??
    • removeAndFreeShape 有效吗? (固定 s/removeShape/removeAndFreeShape/)
    • 嗯。 [smgr rehashStaticShapes] 怎么样? Chipmunk document
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多