我正在尝试一些事情。我已经完成了本教程:https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/ 但是我现在有点卡住了。 我在SpriteBuilder中创建了一个CCNode并添加了3个图像。 carbody.png和2 wheel.png。 我创建了所有对象physicsObjects。
在代码中,我尝试将它们与关节连接起来并移动它们。然而,汽车车身移动但车轮仍然保持原位。
#import "Car.h"
@implementation Skater{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;
}
- (id)init {
self = [super init];
if (self) {
[_wheel1.physicsBody setCollisionGroup: _carBody];
[_wheel2.physicsBody setCollisionGroup: _carBody;
[_carBody.physicsBody setCollisionGroup: _carBody];
_bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
_bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];
}
return self;
}
在Gameplay.m中我只做了这个:
-(void)didLoadFromCCB{
[_car runAction:
[CCActionMoveTo actionWithDuration:10 position:CGPointMake(2000,_car.position.y)]];
}
carBody移动,车轮没有......
我错过了什么?
编辑: 我现在做了一些改动,但我的轮子仍然留在他们的位置......
#import "Car.h"
@implementation Car{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;
}
- (id)init {
self = [super init];
if (self) {
CCLOG(@"Car created");
[_wheel1.physicsBody setCollisionGroup:_carBody];
[_wheel2.physicsBody setCollisionGroup:_carBody];
[_carBody.physicsBody setCollisionGroup:_carBody];
_bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
_bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];
}
return self;
}
-(void)moveCar:(int)distance{
CCLOG(@"Car should move");
CGPoint launchDirection = ccp(1, 0);
CGPoint force = ccpMult(launchDirection, distance);
[self.physicsBody applyForce:force];
}
@end
答案 0 :(得分:1)
你只是错过了移动(和其他)动作绕过物理的观点。一旦一个节点有一个物理体,使用改变位置和旋转的CCAction *类就是禁忌,大多数其他(即比例)也不会应用于物理体,但仍然可以安全使用。
要移动物理对象,请向物理主体施加冲动或力。