1

Cocos2d-v3でSpriteBuilderを使い始めたばかりです SpriteBuilderの演習は、とりあえず以下のWebSiteのチュートリアルで練習しています。

https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/collision-detection/

チュートリアルのゲームの流れとしては、まずステージ上にカタパルトとカタパルトアームを配置し、アームを後ろに指で引っ張って指を離すと、アームに取り付けられた物体(ペンギン)が前方に飛んでいきます。画面から。オブジェクト(ペンギン)が他のオブジェクト(アザラシと呼ばれる)にぶつかると、「何かがアザラシに衝突しました!」というログが出力されます。

チュートリアルの「デリゲートメソッドの実装」で、「何かがシールにぶつかった!」という文字を表示してみました。CCLOGメソッドを使用してConsollで。

Gameplay.m に ccPhysicsCollisionPostSolve:seal:wildcard メソッドを実装し、「何かがアザラシに衝突しました!」という文字を表示するために呼び出されますが、このメソッドはペンギン オブジェクトが他のオブジェクトに衝突したとき (アザラシ) には呼び出されません。

確かに SpriteBuilder で Penguin.ccbi と Seal.ccbi を作ったのですが、その ccbi ファイルが Physics 本体です。

ccPhysicsCollisionPostSolve:seal:wildcard メソッドが呼び出されないのはなぜですか?

次のように実際に実装したのは私のコードです

これは Gameplay.m ファイルです

#import "Gameplay.h"


@implementation Gameplay{

    CCPhysicsNode *_physicsNode;

//To joint betweent catapult and catapultArm
    CCNode *_catapultArm;
    CCNode *_catapult;
    CCPhysicsJoint *_catapultJoint;

//Invisible Physics force
    CCNode *_pullbackNode;
    CCPhysicsJoint *_pullbackJoint;

//to move catapultArm
    CCNode *_mouseJointNode;
    CCPhysicsJoint *_mouseJoint;

//to fly penguin
    CCNode *_currentPenguin;
    CCPhysicsJoint *_penguinCatapultJoint;

//Object
    CCNode *_levelNode;

//To Prevent a 'retry' button from moving with a fly penguin
    CCNode *_contentNode;
}



//is called when CCB file has completed loading
-(void)didLoadFromCCB{


    _physicsNode.collisionDelegate = self;


    //tell this scene to accept touches
    self.userInteractionEnabled = TRUE;

    //loads the Levels/Leve1.ccb we have set up in SpriteBuilder
    CCScene *level = [CCBReader loadAsScene:@"Levels/Level1"];

    [_levelNode addChild:level];

    //visualize physics bodies & joints
    _physicsNode.debugDraw = TRUE;

    //catapultArm and catapult shall not collide
    [_catapultArm.physicsBody setCollisionGroup:_catapult];
    [_catapult.physicsBody setCollisionGroup:_catapult];

    //create a joint to connect the catapult arm with the catapult
    _catapultJoint = [CCPhysicsJoint connectedPivoJointWithBodyA:_catapultArm.physicsBody
                                                           bodyB:_catapult.physicsBody
                                                anchorA:_catapultArm.anchorPointInPoints];



    //nothing shall collide with our invisible nodes
    _pullbackNode.physicsBody.collisionMask = @[];
    //nothing shall collide with our invisible nodes
    _mouseJointNode.physicsBody.collisionMask = @[];


   _pullbackJoint = [CCPhysicsJointconnectedSpringJointWithBodyA:_pullbackNode.physicsBody
                                                           bodyB:_catapultArm.physicsBody
                                                         anchorA:ccp(0,0)
                                                         anchorB:ccp(34,138)
                                                      restLength:60.f
                                                       stiffness:500.f
                                                         damping:40.f
                    ];




}

//called on every touch in this scene (called every touch)
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

   CCLOG(@"touch Began");


   CGPoint touchLcation = [touch locationInNode:_contentNode];


   //start catapult dragging when a touch inside of the catapult arm occurs
  if(CGRectContainsPoint([_catapultArm boundingBox], touchLcation))
  {

   //move the mouseJointNode to the touch position
   _mouseJointNode.position = touchLcation;

   _mouseJoint = [CCPhysicsJoint connectedSpringJointWithBodyA:_mouseJointNode.physicsBody
                                                        bodyB:_catapultArm.physicsBody    
                                                      anchorA:ccp(0,0)
                                                      anchorB:ccp(34,138)
                                                   restLength:0.f
                                                    stiffness:3000.f
                                                      damping:150.f
                 ];



    //create a penguin from the ccbFile
    _currentPenguin = [CCBReader load:@"Penguin"];

    CGPoint penguinPosition = [_catapultArm convertToWorldSpace:ccp(34, 138)];

    _currentPenguin.position = [_physicsNode convertToNodeSpace:penguinPosition];

    //add it to the physics world
    [_physicsNode addChild:_currentPenguin];

    //we don't want the penguin to rotate in the scoop
    _currentPenguin.physicsBody.allowsRotation = FALSE;

    //create a joint to keep the penguin fixed to the scoop until the catapult is released
    _penguinCatapultJoint = 
    [CCPhysicsJoint connectedPivoJointWithBodyA:_currentPenguin.physicsBody
                                          bodyB:_catapultArm.physicsBody
                                        anchorA:_currentPenguin.anchorPointInPoints
    ];


   }


 }


-(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event{

    CCLOG(@"MOVING!!!!!!!!!!");

    //whenever touches move,update the position of the mousejointNode to touch position
    CGPoint touchLocation = [touch locationInNode:_contentNode];

    _mouseJointNode.position = touchLocation;




}



-(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{

//when touches end,meaning the user releases their finger,release the catapult
[self releaseCatapult];

}


-(void)touchCancelled:(UITouch *)touch withEvent:(UIEvent *)event{

[self releaseCatapult];

}


//destroy our joint and let the catapult snap when a touch ends
-(void)releaseCatapult{

 CCLOG(@"Release");


 if(_mouseJoint != nil)
 {



    //releases the joint and lets the catapult snap back
    [_mouseJoint invalidate];
    _mouseJoint = nil;


    //release the joint and lets the penguin fly
    [_penguinCatapultJoint invalidate];
    _penguinCatapultJoint = nil;

    //after snapping rotation is fine
    _currentPenguin.physicsBody.allowsRotation = TRUE;

    //follow the flying penguin
    CCActionFollow *follow = 

 }



}


-(void)retry
{

     [[CCDirector sharedDirector]replaceScene:[CCBReader loadAsScene:@"Gameplay"]];

}


-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair seal:(CCNode *)nodeA  
                                                              wildcard:(CCNode *)nodeB 
{
     CCLOG(@"Something collided with a seal!");
}



@end
4

2 に答える 2

2

Sealほとんどの場合、クラスの衝突タイプを設定していません。あなたが持っていることを再確認してください:

  • SpriteBuilder の Seal のカスタム クラスを次のように設定します。Seal
  • collisionTypeクラス内のdidLoadFromCCBメソッド内Sealを「シール」に設定しました

これで問題が解決するはずです。

于 2014-04-03T15:00:11.657 に答える
1

また、Spritebuilder で、レベル (Level1.ccb) に既にロードされているシールが にCollision type設定されていることを確認しsealます。

于 2016-02-10T20:24:23.053 に答える