[Cocos2d-x game engine development notes (15)] tiled map editor (3)

Source: Internet
Author: User
Tags addchild

Original article, reproduced please indicate the source: http://blog.csdn.net/zhy_cheng/article/details/8363028

In the previous article, I added watermelon to the map and asked hero to eat it. This article will complete the game.

1. Add enemies to the object Layer. Note that a key-value pair is added, with the key value n and the value 1. This key-value pair is mainly used to distinguish between enemies and heroes. Draw enemies on the map.
for(int i=0;i<int(objects->getObjects()->count());i++){CCDictionary *enemy=(CCDictionary *)objects->getObjects()->objectAtIndex(i);if(enemy->valueForKey("n")->intValue()==1){CCSprite *s=CCSprite::create("enemy1.png");float x=enemy->valueForKey("x")->floatValue();float y=enemy->valueForKey("y")->floatValue();s->setPosition(ccp(x,y));s->setAnchorPoint(CCPoint(0,0));_tileMap->addChild(s,4);CCActionInterval *move=CCMoveBy::create(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//CCFiniteTimeAction *func=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::goon));s->runAction(CCSequence::create(move,func,NULL));}}

This code Retrieves all objects from the object Layer, determines whether the key of N is 1, and if yes, It is an enemy. It is added to the map and allows the enemy to move.

Call the goon function after the motion:
void HelloWorld::goon(CCNode *pSender){CCSprite *s=(CCSprite *)pSender;CCActionInterval *move=CCMoveBy::actionWithDuration(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//CCFiniteTimeAction *func=CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::goon));s->runAction(CCSequence::actions(move,func,NULL));}

Continue to call yourself in the goon function, so that the enemy keeps moving towards the hero.

2. so that the hero can attack the current game click is to make the hero move to the specified location, so in order to make the hero have an attack, you can add a status, if the status is true, control the hero movement, if the status is false, control the bullets. Add in header file
bool mode;

Add a menu on the screen

Ccmenuitem * oon, * ooff; oon = ccmenuitemimage: Create ("projectile-button-on.png", "projectile-button-on.png"); ooff = ccmenuitemimage: Create ("projectile-button-off.png", "projectile-button-off.png "); ccmenuitemtoggle * toggle = ccmenuitemtoggle: createwithtarget (this, // menu_selector (helloworld: togglegame) of the callback function, // callback function ooff, oon, null // added menu); ccmenu * menu = ccmenu: Create (toggle, null); menu-> setposition (CCP (Oon-> getcontentsize (). width/2, Oon-> getcontentsize (). height/2); addchild (menu );

Add the code for controlling status changes to the togglegame function:

void HelloWorld::toggleGame(CCObject *pSender){mode=mode?false:true;}

When the status is false, hero emits a bullet:

else{CCSprite *s=CCSprite::create("Projectile.png");s->setPosition(_player->getPosition());_tileMap->addChild(s,4);float dx=pp.x-_player->getPosition().x;float dy=pp.y-_player->getPosition().y;if(dx>0){float lx=32*30-_player->getPosition().x;float ly=dy/dx*lx;CCActionInterval *move=CCMoveBy::create(3,ccp(lx+s->getContentSize().width,ly));CCFiniteTimeAction *ff=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::targetFinish));s->runAction(CCSequence::actions(move,ff,NULL));}else{float lx=0-_player->getPosition().x;float ly=dy/dx*lx;CCActionInterval *move=CCMoveBy::actionWithDuration(3,ccp(lx-s->getContentSize().width,ly);CCFiniteTimeAction *ff=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::targetFinish));s->runAction(CCSequence::actions(move,ff,NULL));}

After a bullet is fired, let the bullet move out of the map. Through the hero position and click position, you can know the angle of the bullet to be fired, and then use the principle that the sides of the similar triangle correspond to proportions, the location where x exceeds the map is displayed. In this way, the bullet is removed from the map.

3. After work in the future, there will be something on the map now, and there will be only the problem of determining the outcome and defeat. Specifically, the hero wins after eating all the watermelons and loses when the enemy encounters them. This requires collision detection. In the cocos2d-x to judge the collision is very good to do, I just talk about the principle, the specific implementation of no difficulty. Add all enemies to a ccarray, add all bullets to another ccarray, and add a Schedule to this cclayer, perform Collision Detection on the enemy's ccarray and the bullet's ccarray at a certain time. If a collision occurs, remove the collision enemies and bullets, and detect the collision between the enemy and hero. If a collision occurs, the game ends.
How to detect the collision? We can get the ccrect of the bullet, the ccrect of the enemy, the ccrect of the hero, and use the member function of ccrect.
bool intersectsRect (const CCRect & rect) const

To determine the collision.




Well, it's time to use tiled to make the game. In the end, how can this game be better? Consider the following aspects: 1. add more enemies 2. add more items. 3. map grows and becomes more complex 4. add Level 5. make better game pictures 6. displays the life of a hero.

Finally give the project code download: [Cocos2d-x game engine development notes (15)] tiled map editor (3)





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.