我刚开始使用SpriteBuilder和Cocos2d-v3 为了练习SpriteBuilder,我在以下WebSite的教程中练习 暂时。
https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/collision-detection/
作为教程游戏的流程,起初弹射器和弹射器位于舞台上,当我用手指将手臂拉向后方时,附在手臂上的物体(企鹅)向前飞。将手指与屏幕分开。 当对象(Penguin)击中其他物体(称为Seal)时,输出日志," Something与封印相撞!"
在"实施委托方法"在教程中,我打算尝试展示一封信," Something与封印相撞!"在Consoll中使用CCLOG方法。
在Gameplay.m中,我实现了ccPhysicsCollisionPostSolve:封面:通配符方法被称为显示字母" Something与封印相撞!"但是这种方法在企鹅时不被调用物体击中其他物体(密封)。
我当然在SpriteBuilder中制作了Penguin.ccbi和Seal.ccbi,而他们的ccbi文件是物理体。
为什么ccPhysicsCollisionPostSolve:封印:不会调用通配符方法?
我的代码是我真正实现的如下
这是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
答案 0 :(得分:2)
您很可能没有为Seal
类设置碰撞类型。
仔细检查你有:
Seal
collisionType
类didLoadFromCCB
内的Seal
设置为“封印”那应该解决你的问题。
答案 1 :(得分:1)
此外,在Spritebuilder中,确保已在级别(Level1.ccb)中加载的封条的Collision type
设置为seal
。