我正在开发一个非常简单的游戏。下面是我的敌人类Machine的行为代码:
#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");
}
我所做的一切如下:
1) body最初是静态的,定位于ccp(size.width*0.5, size.height*0.75)。
2) 1.5秒后,进入运动状态,开始以0.5 m/s的线速度运动
3)它检查它当前的距离(从屏幕宽度中心保持高度相同),计算到达该点所需的时间,然后开始在该方向水平移动。
4)到达该点后,它开始它的标志性动作,它开始从左向右移动,如果在任何时候头盔(另一个游戏对象)经过它下面,它开始向下移动,并在1.0秒后停止,然后整个循环重复。
5)移动LTR &RTL,直到它开始向下移动,当它发现下面的头盔。
现在的问题是,有时行为与预期完全相同。很多次,它开始向上移动我从来没有把y位设置为正方向
初始化主体后,不应更改其bodyType.
。当您第一次制作主体时,将其设置为运动学,这样您以后尝试更改它时就不会遇到问题。尸体是否附在CCSprite
上?如果是这样,您应该使该类成为CCSprite
的子类,这样您就可以使用
[super initWithFile:(NSString *) world:(b2World *)]
或您选择的一些方法来促进您的工作。