cocos2dx游戏--欢欢英雄传说--为敌人添加移动和攻击动作
2016-06-05 17:36
447 查看
这里主要为敌人添加了一个移动动作和攻击动作。
移动动作是很简略的我动他也动的方式。
攻击动作是很简单的我打他也打的方式。
效果:
MainScene.cpp
------------华丽的分割线------------
最近工作上出了一点事情。可能暂时就写到这里了。希望以后有机会能够再补上接下来的内容。
参考教程:http://www.cocos.com/doc/tutorial/lists?id=85
项目github地址:https://github.com/moonlightpoet/HuanHero
移动动作是很简略的我动他也动的方式。
攻击动作是很简单的我打他也打的方式。
效果:
#include "MainScene.h" Scene* MainScene::createScene() { auto scene = Scene::create(); auto layer = MainScene::create(); scene->addChild(layer); return scene; } bool MainScene::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("images/role.plist","images/role.png"); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("images/ui.plist","images/ui.pvr.ccz"); Sprite* background = Sprite::create("images/background.png"); background->setPosition(origin + visibleSize/2); this->addChild(background); //add player _hero = Player::create(Player::PlayerType::HERO); _hero->setPosition(origin.x + _hero->getContentSize().width/2, origin.y + visibleSize.height/2); this->addChild(_hero); //add enemy1 _enemy = Player::create(Player::PlayerType::ENEMY); _enemy->setPosition(origin.x + visibleSize.width - _enemy->getContentSize().width/2, origin.y + visibleSize.height/2); this->addChild(_enemy); _hero->playAnimationForever("stay"); _enemy->playAnimationForever("stay"); _listener_touch = EventListenerTouchOneByOne::create(); _listener_touch->onTouchBegan = CC_CALLBACK_2(MainScene::onTouchBegan,this); _eventDispatcher->addEventListenerWithSceneGraphPriority(_listener_touch, this); auto attackItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MainScene::attackCallback, this)); attackItem->setPosition(Vec2(origin.x + visibleSize.width - attackItem->getContentSize().width/2 , origin.y + attackItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(attackItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); _progress = Progress::create("player-progress-bg.png","player-progress-fill.png"); _progress->setPosition(_progress->getContentSize().width/2, this->getContentSize().height - _progress->getContentSize().height/2); this->addChild(_progress); return true; } void MainScene::menuCloseCallback(cocos2d::Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } bool MainScene::onTouchBegan(Touch* touch, Event* event) { Vec2 pos = this->convertToNodeSpace(touch->getLocation()); _hero->walkTo(pos); log("MainScene::onTouchBegan"); _enemy->autoDoAction(_hero); return true; } void MainScene::attackCallback(Ref* pSender) { _hero->stopAllActions(); _hero->playAnimation("attack"); Vec2 del = _hero->getPosition() - _enemy->getPosition(); float distance = del.length(); log(String::createWithFormat("distance == %f", distance)->getCString()); if (distance <= 100.0) { _enemy->getHit(); } _enemy->autoAttack(_hero); _progress->setProgress(_hero->getProgress()->getProgress()); }
MainScene.cpp
------------华丽的分割线------------
最近工作上出了一点事情。可能暂时就写到这里了。希望以后有机会能够再补上接下来的内容。
参考教程:http://www.cocos.com/doc/tutorial/lists?id=85
项目github地址:https://github.com/moonlightpoet/HuanHero
相关文章推荐
- 【Cocos Creator 实战教程(4)】——黄金矿工(上)(节点动作、碰撞体相关)
- Cocos2d-x 面试题解
- cocos2dx游戏--欢欢英雄传说--添加血条
- cocos2dx 历史版本下载问题
- cocos2dx游戏--欢欢英雄传说--添加攻击按钮
- 为什么在项目中使用Using namespace CocosDenshion;会提示出错expected namespace namespace
- 2016 CocosPods安装教程
- cocos2dx学习源代码之接口iOS事件分发(2)
- cocos2dx游戏--欢欢英雄传说--添加触摸响应
- 如何在window下搭建 cocos2d-x-开发环境保罗各项配置
- iOS cocos2d的安装和卸载
- Cocos2dx-Lua与C++混合使用
- cocos2dx解决苹果正版ipv6的问题
- 在 cocos2d-x 中使用多组shader实现多重滤镜
- Cocos2D与SpriteBuilder的问题在哪提问
- Cocos2D与SpriteBuilder的问题在哪提问
- Cocos2D与SpriteBuilder的问题在哪提问
- cocos2dx游戏--欢欢英雄传说--添加动作
- cocos2dx[3.x](3)——大巨变3.x
- COCOS2D学习日记之 moveto 与 moveby