我对游戏开发非常陌生,我需要开发像NinjaJump这样的游戏。
我创建了一个 CCParallaxNode 来设置滚动背景,并添加了 CCPhysicsNode 来设置物理世界。我创建了如下所示的播放器对象。
// Add a sprite
_sprite = [CCSprite spriteWithImageNamed:@"Icon.png"];
_sprite.position = ccp(self.contentSize.width/2,100);
_sprite.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _sprite.contentSize} cornerRadius:0.0];
_sprite.physicsBody.friction = 0.0f;
_sprite.physicsBody.collisionGroup = @"player";
_sprite.physicsBody.collisionType = @"Player";
//_sprite.physicsBody.collisionMask = 0;
//[self addChild:_sprite];
[foreground addChild:_sprite];
foreground
只是添加到 CCScene 中的一个节点,用于轻松管理焦点玩家。
// code for physics world
_physicsWorld = [CCPhysicsNode node];
_physicsWorld.gravity = ccp(0,-100);
_physicsWorld.debugDraw = YES;
//_physicsWorld.collisionDelegate = self;
[self addChild:_physicsWorld];
_foreground = [CCNode node];
//[self addChild: _foreground];
[_physicsWorld addChild: _foreground];
为了让玩家始终可见,我们将update
方法实现为
- (void) update:(CFTimeInterval)currentTime {
// Calculate player y offset
if (_player.position.y > 200.0f) {
//_midgroundNode.position = CGPointMake(0.0f, -((_player.position.y - 200.0f)/4));
_foreground.position = CGPointMake(0.0f, -(_player.position.y - 200.0f));
}
}
我无法理解,但播放器无论如何都会滚出屏幕。代码使用 Cocos2d v3 编写。
我还设置了一个演示项目来展示我实现的内容:https ://www.dropbox.com/s/5s55d00kk80wun4/HumptyJump-Example.zip?dl=0
任何形式的帮助表示赞赏。提前致谢。