基于cocos2dx的飞机大战学习[三]-为英雄添加飞行帧动作并控制飞机移动

2024-08-21 07:58

本文主要是介绍基于cocos2dx的飞机大战学习[三]-为英雄添加飞行帧动作并控制飞机移动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



第三节:为英雄添加飞行帧动作并控制飞机移动


一、为飞机添加飞行动画

为飞机添加飞行动画十分简单,只需要在FlyPlane::init()函数中创建一个动画对象,在里面添加两张英雄图并相互切换就可以了。

添加代码如下:

//为英雄添加飞行动作,动作由动画组成,所以得到动作对象前,需要先得到动画对象//一、创建动画对象//1.1通过create得到动画对象auto animation = cocos2d::Animation::create();	//1.2添加这个动画所要用的精灵帧animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero1.png"));animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero2.png"));//1.3设置切换时间animation->setDelayPerUnit(0.2f);//1.4循环次数,默认为1,设为-1使其无限循环animation->setLoops(-1);	//二、根据动画对象创建动作对象auto animate = cocos2d::Animate::create(animation);//三、让hero执行这个动作hero->runAction(animate);


修改后init()函数如下:
bool FlyPlane::init() {//一定要先调用父类初始函数if( !cocos2d::Layer::init() ) {return false;}//使用精灵集需要两步//1、将美工做好的plist文件读取到缓存中//2、通过帧名字创建精灵并显示cocos2d::CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile("shoot_background.plist");auto bg1 = cocos2d::Sprite::createWithSpriteFrameName("background.png");//把精灵bg1加到FlyPlane层中,第二个参数ZOrder表示距离用户的距离,第三个参数tag设为1this->addChild(bg1, -1, 1);//默认锚点为(0.5,0.5),只会显示一半的图,必须设置锚点为(0,0)bg1->setAnchorPoint(cocos2d::Point(0,0));//texture:纹理,通过精灵找到对应的纹理,并开启抗锯齿bg1->getTexture()->setAliasTexParameters();auto bg2 = cocos2d::Sprite::createWithSpriteFrameName("background.png");this->addChild(bg2, -1, 2);bg2->setAnchorPoint(cocos2d::Point(0,0));bg2->getTexture()->setAliasTexParameters();//添加英雄cocos2d::CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile("shoot.plist");auto hero = cocos2d::Sprite::createWithSpriteFrameName("hero1.png");	hero->setPosition(VISIBLE_SIZE.width / 2, 100);this->addChild(hero, 3, 3);//为英雄添加飞行动作,动作由动画组成,所以得到动作对象前,需要先得到动画对象//一、创建动画对象//1.1通过create得到动画对象auto animation = cocos2d::Animation::create();	//1.2添加这个动画所要用的精灵帧animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero1.png"));animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero2.png"));//1.3设置切换时间animation->setDelayPerUnit(0.2f);//1.4循环次数,默认为1,设为-1使其无限循环animation->setLoops(-1);	//二、根据动画对象创建动作对象auto animate = cocos2d::Animate::create(animation);//三、让hero执行这个动作hero->runAction(animate);//定时器。scheduleUpdate每帧调用一次update函数scheduleUpdate();return true;
}
运行效果图(就是飞机屁股能喷火- -):




二、控制英雄移动

控制英雄需要监听事件,有这么几个问题需要考虑。

1.hero可以跟随触摸点移动
2.触摸点必须在hero的绘制区域内才会跟随移动
3.hero随触摸点移动时保持向量差
4.hero不能移出规定区域

现在进行编码,为了解决第三个问题首先我们需要在FlyPlane.h文件中添加一个属性m_vec,表示由touch指向英雄锚点坐标的向量。

private:cocos2d::Point m_vec;


在FlyPlane.cpp中的init()函数添加如下代码

	//用单个处理事件添加鼠标监听事件auto listener = cocos2d::EventListenerTouchOneByOne::create();//用lambda表达式处理分解事件//Lambda表达式://	{} 类似于普通函数的函数体//	() 类似于普通函数的参数列表//	[] 默认Lambda表达式不能访问外部的变量,如果想访问外部变量,就通过中括号传进来listener->onTouchBegan = [=](cocos2d::Touch* touch, cocos2d::Event*) {auto touchPos = touch->getLocation();bool isContain = hero->getBoundingBox().containsPoint(touchPos);if(isContain) {m_vec = hero->getPosition() - touchPos;//my_vector,记录touchPos指向hero的向量}return isContain;};const float leftMinX = hero->getContentSize().width / 2;	//英雄的X最小值(左边界线)const float rightMaxX = VISIBLE_SIZE.width - hero->getContentSize().width / 2;//英雄X最大值(右边界线)const float downMinY = hero->getContentSize().height / 2;		//英雄Y最小值(下边界线)const float upMaxY = VISIBLE_SIZE.height - hero->getContentSize().height / 2; //英雄Y最大值(上边界线)listener->onTouchMoved = [=](cocos2d::Touch* touch, cocos2d::Event*) {		auto touchPos = touch->getLocation() + m_vec;//让英雄跟着手指动并且不超出边界hero->setPosition(cocos2d::Point(MAX(leftMinX, MIN(rightMaxX, touchPos.x)), MAX(downMinY, MIN(upMaxY, touchPos.y)))); };//将监听器添加到事件分配器上this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, hero);

添加后FlyPlane::init() 函数如下

bool FlyPlane::init() {//一定要先调用父类初始函数if( !cocos2d::Layer::init() ) {return false;}//使用精灵集需要两步//1、将美工做好的plist文件读取到缓存中//2、通过帧名字创建精灵并显示cocos2d::CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile("shoot_background.plist");auto bg1 = cocos2d::Sprite::createWithSpriteFrameName("background.png");//把精灵bg1加到FlyPlane层中,第二个参数ZOrder表示距离用户的距离,第三个参数tag设为1this->addChild(bg1, -1, 1);//默认锚点为(0.5,0.5),只会显示一半的图,必须设置锚点为(0,0)bg1->setAnchorPoint(cocos2d::Point(0,0));//texture:纹理,通过精灵找到对应的纹理,并开启抗锯齿bg1->getTexture()->setAliasTexParameters();auto bg2 = cocos2d::Sprite::createWithSpriteFrameName("background.png");this->addChild(bg2, -1, 2);bg2->setAnchorPoint(cocos2d::Point(0,0));bg2->getTexture()->setAliasTexParameters();//添加英雄cocos2d::CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile("shoot.plist");auto hero = cocos2d::Sprite::createWithSpriteFrameName("hero1.png");	hero->setPosition(VISIBLE_SIZE.width / 2, 100);this->addChild(hero, 0, 3);//为英雄添加飞行动作,动作由动画组成,所以得到动作对象前,需要先得到动画对象//一、创建动画对象//1.1通过create得到动画对象auto animation = cocos2d::Animation::create();	//1.2添加这个动画所要用的精灵帧animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero1.png"));animation->addSpriteFrame(cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("hero2.png"));//1.3设置切换时间animation->setDelayPerUnit(0.2f);//1.4循环次数,默认为1,设为-1使其无限循环animation->setLoops(-1);	//二、根据动画对象创建动作对象auto animate = cocos2d::Animate::create(animation);//三、让hero执行这个动作hero->runAction(animate);//用单个处理事件添加鼠标监听事件auto listener = cocos2d::EventListenerTouchOneByOne::create();//用lambda表达式处理分解事件//Lambda表达式://	{} 类似于普通函数的函数体//	() 类似于普通函数的参数列表//	[] 默认Lambda表达式不能访问外部的变量,如果想访问外部变量,就通过中括号传进来listener->onTouchBegan = [=](cocos2d::Touch* touch, cocos2d::Event*) {auto touchPos = touch->getLocation();bool isContain = hero->getBoundingBox().containsPoint(touchPos);if(isContain) {m_vec = hero->getPosition() - touchPos;//my_vector,记录touchPos指向hero的向量}return isContain;};const float leftMinX = hero->getContentSize().width / 2;	//英雄的X最小值(左边界线)const float rightMaxX = VISIBLE_SIZE.width - hero->getContentSize().width / 2;//英雄X最大值(右边界线)const float downMinY = hero->getContentSize().height / 2;		//英雄Y最小值(下边界线)const float upMaxY = VISIBLE_SIZE.height - hero->getContentSize().height / 2; //英雄Y最大值(上边界线)listener->onTouchMoved = [=](cocos2d::Touch* touch, cocos2d::Event*) {		auto touchPos = touch->getLocation() + m_vec;//让英雄跟着手指动并且不超出边界hero->setPosition(cocos2d::Point(MAX(leftMinX, MIN(rightMaxX, touchPos.x)), MAX(downMinY, MIN(upMaxY, touchPos.y)))); };//将监听器添加到事件分配器上this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, hero);//定时器。scheduleUpdate每帧调用一次update函数scheduleUpdate();return true;
}

运行操作英雄,发现必须要点中英雄才可以自由移动并且不会超过边界,BUG解决。



本节效果完成,下节讲添加子弹。

这篇关于基于cocos2dx的飞机大战学习[三]-为英雄添加飞行帧动作并控制飞机移动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092578

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Python远程控制MySQL的完整指南

《Python远程控制MySQL的完整指南》MySQL是最流行的关系型数据库之一,Python通过多种方式可以与MySQL进行交互,下面小编就为大家详细介绍一下Python操作MySQL的常用方法和最... 目录1. 准备工作2. 连接mysql数据库使用mysql-connector使用PyMySQL3.

如何搭建并配置HTTPD文件服务及访问权限控制

《如何搭建并配置HTTPD文件服务及访问权限控制》:本文主要介绍如何搭建并配置HTTPD文件服务及访问权限控制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、安装HTTPD服务二、HTTPD服务目录结构三、配置修改四、服务启动五、基于用户访问权限控制六、

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程

《双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程》:本文主要介绍如何在Windows11系统中使用VMware17创建虚拟机,并在虚拟机中安装Ubuntu22.04桌面版或Ubunt... 目录一、首先win11中安装vmware17二、磁盘分区三、保存四、使用虚拟机进行系统安装五、遇见的错误和解决

使用FileChannel实现文件的复制和移动方式

《使用FileChannel实现文件的复制和移动方式》:本文主要介绍使用FileChannel实现文件的复制和移动方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录使用 FileChannel 实现文件复制代码解释使用 FileChannel 实现文件移动代码解释

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen