Monsters / Enemies on platforms (e.g. Doodlejump) Cocos2d

I am new here, so hope you can help me.

I follow the tutorial on creating a simple cocos2d game

Ray Wenderlich Tutorial

I implemented it in another game, jumping like a scribble jump.

in this tutorial, monsters / targets move freely to the right of the left side of the screen. when I implement it in my application, monsters are like flying from left to right. What if I want monsters to stand on platforms just like the one on the scrawl jumps? What specific things will I do?

PS: I tried other things on google but nobody works

Here is the monster / target code:

- (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 help ... you are so good.

0
source share
1 answer

This is a rather broad question, unfortunately, which makes the answer difficult in any final terms. If you are hoping to create real platforms to which you can bounce (in the transition mode to scribble), you will need to implement collision detection between monsters and ccNodes platforms. There are many online training applications for cocos2d conflict detection, both simple implementation and more advanced 2d / chipmunk solutions.

If you want to clone doodle jump pretty fast, there is an open source version of the clone available on github here - although I didn't really look at the code.

Finally, if you mean that you just want to limit the movement of monsters in a certain area of ​​the screen (so that they do not run off the edge), you just need to put the target in the area on and change ccAction so that ccMoveTo uses the ccMoveTo part of the "platform" as the most distant point to which he could go, and the right of the last point as the farthest right. (I admit, I did not play Doodle Jump, so I don’t know what the enemies actually do).

If enemies are running back and forth across the platform, you should examine ccRepeatForever in your sequence of movement and have two destination positions in CCSequence : one that moves the monster to the left of the platform, and the other to the right.

Additional Information

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

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

Then at - (void)step:(ccTime)dt it goes through all the platforms and moves them to the right place depending on how far the bird moved:

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

So, the bit you expect:

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 need to have a cleaner design than this, but it should put you on the right track)

in initPlatform add at the end of the function

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

(I just grabbed an image from an existing Atlas. You could replace the “Monster” sprite object described above. Notice, I add 1000 to the currentPlatformTag . It's just for testing, you should have a monsterTag in the end.

So, now there is a “monster” on each platform (again, you only need to target random platforms) so we need to update the positions for the monsters.

B - (void)step:(ccTime)dt immediately after receiving the current platform

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

Now you also need to get the current monster (remembering to use the updated tag value that we created for the "monsters":

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

Then, a few lines below, where we move the platform, we will need to move 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 on every platform there is a monster whose position y moves with the platforms :-) Hope this helps

+2
source

Source: https://habr.com/ru/post/1386640/


All Articles