Cocos2d iphone 运动学体的Cocos2d/Box2d奇怪行为
我正在开发一个非常简单的游戏。这是我的敌人类机器的行为代码:Cocos2d iphone 运动学体的Cocos2d/Box2d奇怪行为,cocos2d-iphone,box2d-iphone,Cocos2d Iphone,Box2d Iphone,我正在开发一个非常简单的游戏。这是我的敌人类机器的行为代码: #import "Machine.h" @implementation Machine +(id)machineWithWorld:(b2World*)world position:(CGPoint)pos { return [[[self alloc] initWithWorld:world position:pos] autorelease]; } -(id)initWithWorld:(b2World*)world
#import "Machine.h"
@implementation Machine
+(id)machineWithWorld:(b2World*)world position:(CGPoint)pos
{
return [[[self alloc] initWithWorld:world position:pos] autorelease];
}
-(id)initWithWorld:(b2World*)world position:(CGPoint)pos
{
if(self = [super initWithShape:[AppDelegate renameFrameForIpad:@"machine"] inWorld:world])
{
size = [CCDirector sharedDirector].winSize;
self.body->SetTransform([Helper toMeters:pos], 0.0);
self.body->SetType(b2_staticBody);
safetyCounter = 5;
[self schedule:@selector(machineSafetyCounter)];
movementWidthInMeters = (size.width-self.contentSize.width)/PTM_RATIO;
linearSpeed = 0.5;
[self schedule:@selector(startMoving) interval:1.5];
}
return self;
}
#pragma mark<Machine Behavior>
-(void)startMoving
{
[self unschedule:_cmd];
float distanceFromCenterInMeters = (size.width/2 - self.position.x)/PTM_RATIO;
float interval = ABS(distanceFromCenterInMeters/linearSpeed);
if(interval < 0.01f)
interval = 0.02f;
b2Vec2 motionDirection = (distanceFromCenterInMeters > 0.0f) ? b2Vec2(1.0, 0.0) : b2Vec2(-1.0, 0.0);
self.body->SetType(b2_kinematicBody);
self.body->SetLinearVelocity(linearSpeed*motionDirection);
[self schedule:@selector(startMotionFromBeginning) interval:interval-0.01];
CCLOG(@"startMoving distance-->%f, interval-->%f", distanceFromCenterInMeters, interval);
}
-(void)startMotionFromBeginning
{
[self unschedule:_cmd];
float interval = (movementWidthInMeters/2)/linearSpeed;
self.body->SetLinearVelocity(0.5*b2Vec2(1.0, 0.0));
[self schedule:@selector(moveRTL) interval:interval-0.01];
[self schedule:@selector(checkIfHelmetIsBelowMachine) interval:0.1];
CCLOG(@"startMotionFromBeginning interval-->%f", interval);
}
-(void)moveRTL
{
[self unschedule:_cmd];
float interval = movementWidthInMeters/linearSpeed;
self.body->SetLinearVelocity(0.5*b2Vec2(-1.0, 0.0));
[self schedule:@selector(moveLTR) interval:interval-0.01];
CCLOG(@"moveRTL interval-->%f", interval);
}
-(void)moveLTR
{
[self unschedule:_cmd];
float interval = movementWidthInMeters/linearSpeed;
self.body->SetLinearVelocity(0.5*b2Vec2(1.0, 0.0));
[self schedule:@selector(moveRTL) interval:interval-0.01];
CCLOG(@"moveLTR interval-->%f", interval);
}
-(void)checkIfHelmetIsBelowMachine
{
[self unschedule:_cmd];
Helmet* helmet = (Helmet*)[[[[[CCDirector sharedDirector] runningScene] children] objectAtIndex:0] getChildByTag:kTagHelmet];
float helmetPosX = helmet.position.x;
if((self.position.x > helmetPosX) && (self.position.x < helmetPosX+helmet.contentSize.width))
{
[self unscheduleAllSelectors];
[self schedule:@selector(machineSafetyCounter) interval:0.1];
[self schedule:@selector(startMovingDownwards) interval:0.0];
return;
}
[self schedule:_cmd interval:0.1];
}
-(void)startMovingDownwards
{
[self unschedule:_cmd];
self.body->SetLinearVelocity(0.25*b2Vec2(0.0, -1.0));
[self schedule:@selector(stopMovingDownwards) interval:1.0];
CCLOG(@"startMovingDownwards");
}
-(void)stopMovingDownwards
{
[self unschedule:_cmd];
self.body->SetLinearVelocity(b2Vec2(0.0, 0.0));
[self schedule:@selector(startMoving) interval:0.2];
CCLOG(@"stopMovingDownwards");
}
#导入“Machine.h”
@执行机器
+(id)机器世界:(b2World*)世界位置:(CGPoint)位置
{
return[[self alloc]initWithWorld:world position:pos]autorelease];
}
-(id)initWithWorld:(b2World*)世界位置:(CGPoint)位置
{
if(self=[super initWithShape:[AppDelegate重命名iPad的名称:@“machine”]inWorld:world])
{
大小=[CCDirector sharedDirector].winSize;
self.body->SetTransform([Helper-toMeters:pos],0.0);
self.body->SetType(b2_staticBody);
安全计数=5;
[自调度:@选择器(机器安全计数器)];
movementWidthInMeters=(size.width self.contentSize.width)/PTM_比率;
线性速度=0.5;
[自调度:@选择器(开始移动)间隔:1.5];
}
回归自我;
}
#布拉格标记
-(无效)开始移动
{
[自我计划外:_cmd];
浮点数距中心的距离=(size.width/2-self.position.x)/PTM_比率;
浮动间隔=绝对值(以米/线速度为单位与中心的距离);
如果(间隔<0.01f)
间隔=0.02f;
b2Vec2运动方向=(与中心的距离以米计>0.0f)?b2Vec2(1.0,0.0):b2Vec2(-1.0,0.0);
self.body->SetType(b2_运动学体);
self.body->SetLinearVelocity(linearSpeed*运动方向);
[自调度:@selector(startMotionFromStart)间隔:间隔-0.01];
CCLOG(@“开始移动距离-->%f,间隔-->%f”,距离中心米,间隔);
}
-(无效)从一开始就开始移动
{
[自我计划外:_cmd];
浮动间隔=(移动宽度米/2)/线性速度;
self.body->SetLinearVelocity(0.5*b2Vec2(1.0,0.0));
[自调度:@selector(moveRTL)间隔:间隔-0.01];
[自调度:@selector(checkIfHelmetIsBelowMachine)间隔:0.1];
CCLOG(@“StartMotionFromStart间隔-->%f”,间隔);
}
-(void)moveRTL
{
[自我计划外:_cmd];
浮动间隔=移动宽度(米/线速度);
self.body->SetLinearVelocity(0.5*b2Vec2(-1.0,0.0));
[自调度:@selector(moveLTR)间隔:间隔-0.01];
CCLOG(@“移动RTL间隔-->%f”,间隔);
}
-(无效)移动
{
[自我计划外:_cmd];
浮动间隔=移动宽度(米/线速度);
self.body->SetLinearVelocity(0.5*b2Vec2(1.0,0.0));
[自调度:@selector(moveRTL)间隔:间隔-0.01];
CCLOG(@“移动LTR间隔-->%f”,间隔);
}
-(无效)检查Helmetisbelowmachine
{
[自我计划外:_cmd];
头盔*头盔=(头盔*)[[CCDirector sharedDirector]runningScene]儿童]对象索引:0]getChildByTag:KTAGHelm];
浮动头盔POSX=头盔位置x;
if((self.position.x>helmetPosX)和&(self.position.xSetLinearVelocity(0.25*b2Vec2(0.0,-1.0));
[自调度:@选择器(停止向下移动)间隔:1.0];
CCLOG(“开始向下移动”);
}
-(无效)停止向下移动
{
[自我计划外:_cmd];
self.body->SetLinearVelocity(b2Vec2(0.0,0.0));
[自调度:@选择器(开始移动)间隔:0.2];
CCLOG(“停止向下移动”);
}
我所做的一切如下:
1) 车身最初是静态的,定位在ccp(尺寸、宽度*0.5,尺寸、高度*0.75)
2) 1.5秒后,它变为运动学,并以0.5 m/s的线性速度开始移动
3) 它检查它的当前距离(从屏幕宽度中心保持高度不变),计算到达该点所需的时间,然后开始沿该方向水平移动
4) 到达该点后,它开始它的标志性运动,它开始从左向右移动,如果头盔(另一个游戏对象)在任何时候从它下面经过,它开始向下移动,并在1.0秒后停止,然后整个循环重复
5) 它移动LTR和RTL,直到发现头盔在其下方时开始向下移动
现在的问题是,有时行为与预期完全相同。
很多时候,它开始向上移动,我从来没有为正方向的运动向量设置y位。初始化身体后,你不应该更改它的
身体类型。
当你第一次制作身体时,将它设置为运动学,这样你以后在尝试更改它时就不会遇到问题。主体是否连接到CCSprite
?如果是这样,您应该将该类设置为CCSprite
的子类,这样您就可以使用[super initWithFile:(NSString*)world:(b2World*)]
或您选择的某种方法来促进您的工作 在SetLinearVelocity中设置断点(或添加日志语句),查看是否接收到y坐标为正的调用。