cocos2dx游戏--欢欢英雄传说--添加攻击按钮

接下来添加攻击按钮用于执行攻击动作。
同时修复了上一版移动时的bug。
修复后的Player::walkTo()函数:

void Player::walkTo(Vec2 dest)
{if (_seq)this->stopAction(_seq);auto curPos = this->getPosition();if (curPos.x > dest.x)this->setFlippedX(true);elsethis->setFlippedX(false);auto diff = dest - curPos;auto time = diff.getLength() / _speed;auto moveTo = MoveTo::create(time, dest);auto func = [&](){this->stopAllActions();this->playAnimationForever("stay");_seq = nullptr;};auto callback = CallFunc::create(func);this->stopAllActions();this->playAnimationForever("walk");_seq = Sequence::create(moveTo, callback, nullptr);this->runAction(_seq);
}

在MainScene::init()函数中添加了攻击按钮:

    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 objectauto menu = Menu::create(attackItem, NULL);menu->setPosition(Vec2::ZERO);this->addChild(menu, 1);

增加了攻击的回调函数:

void MainScene::attackCallback(Ref* pSender)
{_hero->stopAllActions();_hero->playAnimation("attack");
}

增加了Player::playAnimation()函数,执行了一次动作之后又回返回重复执行"stay"。

void Player::playAnimation(std::string animationName)
{auto str = String::createWithFormat("%s-%s", _name.c_str(), animationName.c_str())->getCString();bool exist = false;for (int i = 0; i < _animationNum; i ++) {if (animationName == _animationNames[i]){exist = true;break;}}if (exist == false)return;auto animation = AnimationCache::getInstance()->getAnimation(str);auto func = [&](){this->stopAllActions();this->playAnimationForever("stay");_seq = nullptr;};auto callback = CallFunc::create(func);auto animate = Sequence::create(Animate::create(animation), callback,NULL);this->runAction(animate);
}

效果:

#ifndef __Player__
#define __Player__
#include "cocos2d.h"USING_NS_CC;class Player : public Sprite
{
public:enum PlayerType{HERO,ENEMY};bool initWithPlayerType(PlayerType type);static Player* create(PlayerType type);void addAnimation();void playAnimationForever(std::string animationName);void playAnimation(std::string animationName);void walkTo(Vec2 dest);
private:PlayerType _type;std::string _name;int _animationNum = 5;float _speed;std::vector<int> _animationFrameNums;std::vector<std::string> _animationNames;Sequence* _seq;
};#endif
Player.h
#include "Player.h"
#include <iostream>bool Player::initWithPlayerType(PlayerType type)
{std::string sfName = "";std::string animationNames[5] = {"attack", "dead", "hit", "stay", "walk"};_animationNames.assign(animationNames,animationNames+5);switch (type){case PlayerType::HERO:{_name = "hero";sfName = "hero-stay0000.png";int animationFrameNums[5] = {10, 12, 15, 30, 24};_animationFrameNums.assign(animationFrameNums, animationFrameNums+5);_speed = 125;break;}case PlayerType::ENEMY:{_name = "enemy";sfName = "enemy-stay0000.png";int animationFrameNums[5] = {21, 21, 24, 30, 24};_animationFrameNums.assign(animationFrameNums, animationFrameNums+5);break;}}this->initWithSpriteFrameName(sfName);this->addAnimation();return true;
}
Player* Player::create(PlayerType type)
{Player* player = new Player();if (player && player->initWithPlayerType(type)){player->autorelease();return player;}else{delete player;player = NULL;return NULL;}
}
void Player::addAnimation()
{auto animation = AnimationCache::getInstance()->getAnimation(String::createWithFormat("%s-%s", _name.c_str(),_animationNames[0].c_str())->getCString());if (animation)return;for (int i = 0; i < _animationNum; i ++){auto animation = Animation::create();animation->setDelayPerUnit(1.0f / 10.0f);for (int j = 0; j < _animationFrameNums[i]; j ++){auto sfName = String::createWithFormat("%s-%s%04d.png", _name.c_str(), _animationNames[i].c_str(), j)->getCString();animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName));if (!animation)log("hello ha ha");}AnimationCache::getInstance()->addAnimation(animation, String::createWithFormat("%s-%s", _name.c_str(),_animationNames[i].c_str())->getCString());}
}
void Player::playAnimationForever(std::string animationName)
{auto str = String::createWithFormat("%s-%s", _name.c_str(), animationName.c_str())->getCString();bool exist = false;for (int i = 0; i < _animationNum; i ++) {if (animationName == _animationNames[i]){exist = true;break;}}if (exist == false)return;auto animation = AnimationCache::getInstance()->getAnimation(str);auto animate = RepeatForever::create(Animate::create(animation));this->runAction(animate);
}void Player::playAnimation(std::string animationName)
{auto str = String::createWithFormat("%s-%s", _name.c_str(), animationName.c_str())->getCString();bool exist = false;for (int i = 0; i < _animationNum; i ++) {if (animationName == _animationNames[i]){exist = true;break;}}if (exist == false)return;auto animation = AnimationCache::getInstance()->getAnimation(str);auto func = [&](){this->stopAllActions();this->playAnimationForever("stay");_seq = nullptr;};auto callback = CallFunc::create(func);auto animate = Sequence::create(Animate::create(animation), callback,NULL);this->runAction(animate);
}void Player::walkTo(Vec2 dest)
{if (_seq)this->stopAction(_seq);auto curPos = this->getPosition();if (curPos.x > dest.x)this->setFlippedX(true);elsethis->setFlippedX(false);auto diff = dest - curPos;auto time = diff.getLength() / _speed;auto moveTo = MoveTo::create(time, dest);auto func = [&](){this->stopAllActions();this->playAnimationForever("stay");_seq = nullptr;};auto callback = CallFunc::create(func);this->stopAllActions();this->playAnimationForever("walk");_seq = Sequence::create(moveTo, callback, nullptr);this->runAction(_seq);
}
Player.cpp
#ifndef __MainScene__
#define __MainScene__#include "cocos2d.h"
#include "Player.h"USING_NS_CC;class MainScene : public cocos2d::Layer
{
public:static cocos2d::Scene* createScene();virtual bool init();void menuCloseCallback(cocos2d::Ref* pSender);CREATE_FUNC(MainScene);bool onTouchBegan(Touch* touch, Event* event);void attackCallback(Ref* pSender);
private:Player* _hero;Player* _enemy;EventListenerTouchOneByOne* _listener_touch;
};#endif
MainScene.h
#include "MainScene.h"
#include "FSM.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");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 objectauto menu = Menu::create(attackItem, NULL);menu->setPosition(Vec2::ZERO);this->addChild(menu, 1);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;
#endifDirector::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");return true;
}void MainScene::attackCallback(Ref* pSender)
{_hero->stopAllActions();_hero->playAnimation("attack");
}
MainScene.cpp

 

转载于:https://www.cnblogs.com/moonlightpoet/p/5560715.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/255148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ceph分布式存储系统-性能测试与优化

测试环境 部署方案&#xff1a;整个Ceph Cluster使用4台ECS&#xff0c;均在同一VPC中&#xff0c;结构如图&#xff1a; 以下是 Ceph 的测试环境&#xff0c;说明如下&#xff1a; Ceph 采用 10.2.10 版本&#xff0c;安装于 CentOS 7.4 版本中&#xff1b;系统为初始安装&…

阅读好书依然是提升自己的高效方法:兼以作者的身份告诉大家如何选择书,以及高效学习的方法...

国内技术网站多如牛毛&#xff0c;质量高的网站也不少&#xff0c;博客园也算一个&#xff0c;各类文章数以百万计&#xff0c;我随便输入一个关键字&#xff0c;比如Spring Cloud&#xff0c;都能看到大量的技术文章和教学视频&#xff0c;我无意贬低技术文章和教学视频的作用…

TCP/IP 协议簇的逐层封装

在使用 TCP 协议的网络程序中&#xff0c;用户数据从产生到从网卡发出去一般要经过如下的逐层封装过程&#xff1a; 从下往上看&#xff1a; 1&#xff09;链路层通过加固定长度的首部、尾部来封装 IP 数据报(Datagram) 产生以太网帧(Frame)。 其中首部存在对封装数据的…

【开源程序(C++)】获取bing图片并自动设置为电脑桌面背景

众所周知&#xff0c;bing搜索网站首页每日会更新一张图片&#xff0c;张张漂亮&#xff08;额&#xff0c;也有一些不合我口味的&#xff09;&#xff0c;特别适合用来做电脑壁纸。 我们想要将bing网站背景图片设置为电脑桌面背景的通常做法是&#xff1a; 上网&#xff0c;搜…

UIProgressView 圆角

里面外面都变成圆角 不用图片 直接改变layer 重点是里面外面都是圆角哦 for (UIImageView * imageview in self.progress.subviews) { imageview.layer.cornerRadius 5; imageview.clipsToBounds YES; } 转载于:https://www.cnblogs.com/huoran1120/p/5563991.html

DataTables warning: Requested unknown parameter '0' from the data source for row '0'

问题&#xff1a;DataTables warning: Requested unknown parameter 0 from the data source for row 0 代码&#xff1a; <script type"text/javascript">var data [{"Name":"UpdateBootProfile","Result":"PASS",&…

我与Linux系统的交集

2019独角兽企业重金招聘Python工程师标准>>> 一、初识Linux 第一次知道Linux还是在我刚进大学的时候&#xff0c;从开始聊QQ、玩斗地主的时候起我就是用的Windows&#xff0c;从Windows2000一直到Windows7&#xff0c;当时我已经完全习惯了使用Windows&#xff0c;而…

IP头、TCP头、UDP头详解以及定义

一、MAC帧头定义 /*数据帧定义&#xff0c;头14个字节&#xff0c;尾4个字节*/ typedef struct _MAC_FRAME_HEADER { char m_cDstMacAddress[6]; //目的mac地址 char m_cSrcMacAddress[6]; //源mac地址 short m_cType;      //上一层协议类型&#xff0c;如…

基本 TCP 套接字编程讲解

基于 TCP 的套接字编程的所有客户端和服务器端都是从调用socket 开始&#xff0c;它返回一个套接字描述符。客户端随后调用connect 函数&#xff0c;服务器端则调用 bind、listen 和accept 函数。 使用套接口客户机服务器的的例子 sever.c #include <stdio.h> #include &…

怎样屏蔽微信朋友圈视频?局域网如何禁止员工看朋友圈视频?

上班时间刷刷朋友圈&#xff0c;一眨眼半小时就过去了。不但会影响工作效率&#xff0c;而且朋友圈的视频会占用大量的带宽。所以对企业管理人员来说&#xff0c;很多时候需要禁止员工在工作时段刷朋友圈。但是行政手段要和技术手段配合&#xff0c;才可以发挥真正的作用。本文…

cf414B(dp)

题目链接&#xff1a;http://codeforces.com/problemset/problem/414/B 题意&#xff1a;定义所有元素是其前一个元素的倍数的数列为good sequence&#xff0c;给出 n, 和 k&#xff0c;求1....n组成的长度为k的good sequence 的数目&#xff1b; 思路&#xff1a;dp 用dp[i][j…

增量值编码器、单圈绝对值编码器、多圈绝对值编码器

主流的伺服电机位置反馈元件包括增量值编码器&#xff0c;单圈绝对值编码器&#xff0c;多圈绝对值编码器&#xff0c;旋转变压器等。下面分别介绍&#xff1a; 增量值编码器增量式编码器是将位移转换成周期性的电信号&#xff0c;再把这个电信号转变成计数脉冲&#xff0c;用…

永磁交流伺服电机的工作原理与更换新编码器后的常规零位校正方法

http://wuhuotun.blog.163.com/blog/static/73085450200910655748516/ 永磁交流伺服电机的编码器相位为何要与转子磁极相位对齐 其唯一目的就是要达成矢量控制的目标&#xff0c;使d轴励磁分量和q轴出力分量解耦&#xff0c;令永磁交流伺服电机定子绕组产生的电磁场始终正交于…

理解Java中字符流与字节流的区别

1. 什么是流 Java中的流是对字节序列的抽象&#xff0c;我们可以想象有一个水管&#xff0c;只不过现在流动在水管中的不再是水&#xff0c;而是字节序列。和水流一样&#xff0c;Java中的流也具有一个“流动的方向”&#xff0c;通常可以从中读入一个字节序列的对象被称为输入…

2018/03/25

2019独角兽企业重金招聘Python工程师标准>>> March 25 2018 Sunday Weather&#xff1a;cloudy 1、需求&#xff1a; a0.5 b3 ca*b 求c的值&#xff1a; [rootDasoncheng sbin]# cat a.sh #!/bin/bash a0.5 b3 cecho $a*$b |bc echo $canswer referred&#xff1a;…

elasticsearch分词聚合查询demo

2019独角兽企业重金招聘Python工程师标准>>> 我们在通过elasticsearch查询text类型的字段时&#xff0c;我们使用aggs进行聚合某个text类型field。这时elasticsearch会自动进行分词将分词后的结果进行聚合。获取每一个分词出现在文档的文档个数。注意&#xff1a;是…

Spring实战第七章

一、SpringMVC配置代替方案 1自定DispatcherServlet 按照AbstractAnnotationConfigDispatcherServletInitializer的定义&#xff0c;它会创建DispatcherServlet和ContextLoaderListener。 AbstractAnnotationConfigDispatcherServletInitializer有三个方法是必须要重载的abstra…

C++多线程(一)

C多线程&#xff08;一&#xff09; WIN 多线程API一 简单实例比较简单的代码&#xff0c;创建10个线程&#xff0c;其中使第4个线程在一创建就挂起&#xff0c;等到其他的线程执行的差不多的时候再使第4个线程恢复执行。#include <stdio.h>#include <stdlib.h>#i…

天梯赛2016-L2

L2-001. 紧急救援 作为一个城市的应急救援队伍的负责人&#xff0c;你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候&#xff0c;你…

伺服系统控制网络的重要性! 现场总线的重要性! SSCNET运动控制系统与发展趋势

引言&#xff1a;在2010年的时候&#xff0c;在北京的一个数控公司工作。产品采用的是通过运动控制卡发脉冲的方式&#xff0c;控制机床的X、Y、Z轴进行加工。 机床在加工产品的时候&#xff0c;一直存在着精度的问题&#xff0c;例如DMG的机床可以达到0.01的加工精度&#x…