怪物/平台上的敌人(像Doodlejump)Cocos2d [英] Monsters/Enemies on Platforms(like on Doodlejump)Cocos2d

查看:122
本文介绍了怪物/平台上的敌人(像Doodlejump)Cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,所以我希望你能帮助我。



我正在学习制作简单的cocos2d游戏教程



Ray Wenderlich的教程



我在另一个游戏上实现了它,像跳跃的跳跃一样。



所述教程的怪物/目标正在自由地从屏幕的右侧移动到左侧。当我在我的应用程序实现它的怪物就像从左到右飞行。如果我想让怪物站在平台上,就像在涂鸦跳上一样?我会做什么特别的事情?



PS:我在google上尝试了其他一些操作,但都无效



是怪物/目标的代码:

   - (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);打破;
case 1:rect = CGRectMake(608,128,90,32);打破;
}

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];


}

你很好。

解决方案

这是一个相当广泛的问题,不幸的是,这使得很难回答任何确定的术语。如果你希望创建可以反弹的实际平台(以涂鸦跳跃的方式),你将需要实现Monsters和Platform ccNodes之间的冲突检测。有许多教程在线cocos2d碰撞检测,简单的实现和更高级的盒子2d /花栗鼠的解决方案。



如果你正在寻找克隆涂鸦跳相当接近, github上有一个开源版本的克隆这里 - 虽然我没有实际看过代码。



最后,如果你只是想限制怪物移动到屏幕的特定区域(所以他们不会跑掉边缘),您只需要将目标定位到屏幕上的某个区域,并更改 ccAction ,以便 ccMoveTo 使用平台的最左点作为最左点,它可以移动到最右点,最右点作为最右边。



如果敌人在平台上来回跑动,你应该使用 ccRepeatForever 查看您的移动序列,并有两个 CCSequence 中的目标位置:将怪物移动到平台左侧,另一个将其移动到右侧。



其他信息



好的,我看到你在做什么。这将让您开始: 平台是在 initPlatforms 中创建的。这会调用 initPlatform 多次。这将从平台的 AtlasSprite 中获取一个映像,为每个平台创建一个 ccSprite 并为其分配唯一的标记。



然后,在 - (void)步骤:(ccTime)dt 循环遍历所有平台, (t; t

  {
AtlasSprite * platform =(AtlasSprite *)[spriteManager getChildByTag:t];
//等...

p>

如果你想在这些平台上添加一个怪物,你必须遵循类似的模式。要开始尝试这样(你会想要一个比这更干净的设计,但它应该把你放在正确的轨道)



initPlatform 将以下内容添加到函数的结尾

  //添加一个怪物精灵
AtlasSprite * monster = [AtlasSprite spriteWithRect:CGRectMake(608,128,64,64)spriteManager:spriteManager];
[spriteManager addChild:monster z:3 tag:currentPlatformTag + 1000];

(我刚刚从现有的Atlas获取了一张图片,注意我添加1000到 currentPlatformTag ,这只是为了测试;你应该有一个 monsterTag



现在每个平台都有一个'monster'(同样,你只需要定位随机平台)
,所以我们需要更新位置。



- (void)步骤:(ccTime)dt 平台

  AtlasSprite * platform =(AtlasSprite *)[spriteManager getChildByTag:t]; 

您现在还需要获取当前的怪物(记住使用我们为怪物创建的更新的标签值:

  AtlasSprite * monster =(AtlasSprite *)[spriteManager getChildByTag:t + 1000]; 

然后,我们重新定位平台下面几行,我们将需要重新定位怪物。

  platform.position = pos; 
//我们更新怪物并将其设置为平台上方32像素:
monster.position = ccp(pos.x,pos.y + 32);

现在每个平台都有一个怪物,它的y位置随着平台移动:-)
希望这有助于


I'm new here so i hope you can help me.

I am following a tutorial on making simple cocos2d game

Ray Wenderlich's Tutorial

I implemented it on another game a jumping one like doodle jump.

in the said tutorial the monsters/targets are moving freely coming from the right to the left side of the screen. when i implement it on my app the monsters are like flying from left to right. What if i want the monsters to stand on the platforms just like the one on doodle jump? what particular things will i do?

PS:i tried some other things on google but none works

Here is the code of the monsters/targets:

- (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];


}

Thanks to those who will help... you are so nice.

解决方案

This is a pretty broad question unfortunately, which makes it difficult to answer in any definitive terms. If you are hoping to create actual platforms that can be bounced on (in a doodle jump manner) you are going to need to implement collision detection between the Monsters and the Platform ccNodes. There are numerous tutorials online for cocos2d collision detection, both simple implementation and the more advanced box 2d/chipmunk based solutions.

If you are looking to clone doodle jump fairly closely, there is an open source version of a clone available on github here - though I've not actually looked at the code.

Finally, if you mean that you simply want to restrict the movement of the monsters to a particular area of the screen (so they don't keep running off the edge) you just need to position the target to an area on the screen and alter theccAction so that the ccMoveTo uses the left most point of the 'platform' as the furthest point left it can move to and the right most point as the furthest right. (I'll confess I've not played Doodle Jump so have no idea what the enemies actually do).

If the enemies run back and forth across the platform you should look into using ccRepeatForever on your movement sequence and have two destination positions in the CCSequence : one that moves the monster to the left of the platform, the other to move it to the right.

Additional Info

Ok, I see what you are trying to do. This should get you started:

Platforms are created in initPlatforms. This calls initPlatform a number of times. This grabs an image from the AtlasSprite for the platform, creates a ccSprite for each platform and assigns it a unique tag.

Then, in - (void)step:(ccTime)dt it loops through all the platforms and moves them to their correct location based on how far the bird has moved:

for(t; t < kPlatformsStartTag + kNumPlatforms; t++) {
        AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];
//etc...

So, the bit you are waiting for:

If you want to add a monster to these platforms, you will have to follow a similar pattern. To get started try something like this (You will want to have a cleaner design than this though but it should put you on the right track)

in initPlatform add the following to the end of the function

// add a monster sprite
AtlasSprite *monster = [AtlasSprite spriteWithRect:CGRectMake(608,128,64,64) spriteManager:spriteManager];
[spriteManager addChild:monster z:3 tag:currentPlatformTag + 1000];

(I've just grabbed an image from the existing Atlas. You could replace the above with your actual 'Monster' sprite object. Notice I add 1000 to thecurrentPlatformTag. This is just for testing; you should have a monsterTag implementation eventually.

So now every platform has a 'monster' (Again, you will only want to target random platforms) so we need to update the positions for the monsters.

In - (void)step:(ccTime)dt directly after you get the current platform

AtlasSprite *platform = (AtlasSprite*)[spriteManager getChildByTag:t];

You now also need to get the current monster (remembering to use the updated tag value we created for 'monsters':

AtlasSprite *monster = (AtlasSprite*)[spriteManager getChildByTag:t + 1000];

Then, a few lines below where we reposition the platform we will need to reposition the monster

platform.position = pos;
// We update the monster and set it a 32 pixels above the platform:
monster.position = ccp(pos.x, pos.y + 32);

So now each platform has a monster on it whose y position moves with the the platforms :-) Hope this helps

这篇关于怪物/平台上的敌人(像Doodlejump)Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆