我是新来的,所以我希望你能帮助我。
我正在学习制作简单的 cocos2d 游戏的教程
我在另一个游戏中实现了它,一个像涂鸦跳跃这样的跳跃游戏。
在上述教程中,怪物/目标从屏幕右侧自由移动到左侧。当我在我的应用程序上实现它时,怪物就像从左飞到右。如果我想让怪物像涂鸦跳跃一样站在平台上怎么办?我会做什么特别的事情?
PS:我在谷歌上尝试了一些其他的东西,但都没有用
这是怪物/目标的代码:
- (void)initPlatforms {
// NSLog(@"initPlatforms");
currentPlatformTag = kPlatformsStartTag;
while(currentPlatformTag < kPlatformsStartTag + kNumPlatforms) {
[self initPlatform];
currentPlatformTag++;
}
[self resetPlatforms];
}
- (void)initPlatform {
CGRect rect;
switch(random()%2) {
case 0: rect = CGRectMake(608,64,102,36); break;
case 1: rect = CGRectMake(608,128,90,32); break;
}
AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
AtlasSprite *platform = [AtlasSprite spriteWithRect:rect spriteManager:spriteManager];
[spriteManager addChild:platform z:3 tag:currentPlatformTag];
}
-(void)addTarget {
Sprite *target = [Sprite spriteWithFile:@"komodo.png"];
target.position = ccp(300,200);
[self addChild:target];
CGSize winSize = [[Director sharedDirector]winSize];
int minX = target.contentSize.height/2;
int maxX = winSize.height -target.contentSize.height/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) +minX;
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
id actionMove = [MoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width,actualX)];
id actionMoveDone = [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[Sequence actions:actionMove, actionMoveDone,nil]];
target.tag = 1;
[_targets addObject:target];
}
感谢那些愿意提供帮助的人……你们真好。
最佳答案
不幸的是,这是一个相当宽泛的问题,因此很难用任何明确的术语来回答。如果您希望创建可以在其上弹跳(以涂鸦跳跃方式)的实际平台,您将需要在怪物和平台 ccNode 之间实现碰撞检测。网上有很多关于 cocos2d 碰撞检测的教程,包括简单的实现和更高级的基于 box 2d/chipmunk 的解决方案。
如果您想非常接近地克隆 doodle jump,可以在 github 上找到一个开源版本的克隆 here - 虽然我实际上并没有看过代码。
最后,如果您的意思是您只想将怪物的移动限制在屏幕的特定区域(这样它们就不会一直跑出边缘),您只需将目标定位到屏幕上的一个区域筛选并更改 ccAction
,以便 ccMoveTo
使用“平台”的最左侧点作为它可以移动到的最左侧点,最右侧点作为最远点正确的。 (我承认我没有玩过 Doodle Jump,所以不知道敌人实际上做了什么)。
如果敌人在平台上来回奔跑,你应该考虑使用 ccRepeatForever在您的移动序列上,并在 CCSequence
中有两个目标位置:一个将怪物移动到平台左侧,另一个将其移动到右侧。
附加信息
好的,我知道你想做什么了。这应该让你开始:
Platforms 在 initPlatforms
中创建。这会多次调用 initPlatform
。这从平台的 AtlasSprite
抓取图像,为每个平台创建一个 ccSprite
并为其分配一个唯一的标签。
然后,在 - (void)step:(ccTime)dt
中循环遍历所有平台,并根据鸟移动的距离将它们移动到正确的位置:
for(t; t < kPlatformsStartTag + kNumPlatforms; t++) {
AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];
//etc...
那么,您正在等待的是:
如果你想在这些平台上添加一个怪物,你将不得不遵循类似的模式。要开始尝试这样的事情(虽然你会想要一个比这更简洁的设计,但它应该让你走上正确的轨道)
在initPlatform
函数的末尾添加以下内容
// add a monster sprite
AtlasSprite *monster = [AtlasSprite spriteWithRect:CGRectMake(608,128,64,64) spriteManager:spriteManager];
[spriteManager addChild:monster z:3 tag:currentPlatformTag + 1000];
(我刚刚从现有的 Atlas 中抓取了一张图像。您可以将上面的图像替换为实际的“怪物” Sprite 对象。请注意,我将 1000 添加到 currentPlatformTag
。这仅用于测试; 你最终应该有一个 monsterTag
实现。
所以现在每个平台都有一个“怪物”(同样,您只想定位随机平台) 所以我们需要更新怪物的位置。
在- (void)step:(ccTime)dt
中直接获取当前平台
AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];
您现在还需要获取当前的怪物(记得使用我们为“怪物”创建的更新标签值:
AtlasSprite *monster = (AtlasSprite*)[spriteManager getChildByTag:t + 1000];
然后,在我们重新定位平台的下面几行,我们需要重新定位怪物
platform.position = pos;
// We update the monster and set it a 32 pixels above the platform:
monster.position = ccp(pos.x, pos.y + 32);
所以现在每个平台上都有一个怪物,它的 y 位置随平台移动:-) 希望这有帮助
关于iphone - 平台上的怪物/敌人(如 Doodlejump)Cocos2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797777/