加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x游戏项目总结

发布时间:2020-12-14 16:38:02 所属栏目:百科 来源:网络整理
导读:(1)异步加载图片资源 当游戏中需要使用的纹理图过多时,如果在进入界面加载,会导致界面长时间处于等待状态,影响体验。所以最好的情况就是设置一个加载界面,异步加载图片资源,主要API如下所示。 span style="font-size:18px;"auto texture=Director::ge

(1)异步加载图片资源

当游戏中需要使用的纹理图过多时,如果在进入界面加载,会导致界面长时间处于等待状态,影响体验。所以最好的情况就是设置一个加载界面,异步加载图片资源,主要API如下所示。

<span style="font-size:18px;">auto texture=Director::getInstance()->getTextureCache();
texture->addImageAsync(const std::string &filepath,const std::function<void(Texture2D*)>& callback);</span>

(2)异步加载3D模型

3D模型如obj等的加载也是需要大量的时间的,所以此处也是采用相同的策略,异步加载。主要API如下所示。

<span style="font-size:18px;">Sprite3D::createAsync(const std::string &modelPath,const std::function<void(Sprite3D*,void*)>& callback,void* callbackparam);
Sprite3D::createAsync(path,CC_CALLBACK_2(LoadingLayer::printf,this),(void*)i++);<span style="white-space:pre">	</span>//项目实际使用方法</span>
(3)获得触摸对象

在游戏中,我们经常需要获得点击点处的节点对象,然后对其进行相应的操作,主要使用的是Event类和Touch类。主要API如下所示。

<span style="font-size:18px;">auto target=static_cast<Sprite*>(event->getCurrentTarget());	<span style="white-space:pre">			</span>//获得触摸对象
Point location=target->convertToNodeSpace(target->getLocation());	<span style="white-space:pre">		</span>//获得精灵位置</span>

注:Touch类常用的方法有:
<span style="font-size:18px;">/** Returns the current touch location in OpenGL coordinates.
     *
     * @return The current touch location in OpenGL coordinates.
     */
    Vec2 getLocation() const;</span>
<span style="font-size:18px;">/** Returns the delta of 2 current touches locations in screen coordinates.
     *
     * @return The delta of 2 current touches locations in screen coordinates.
     */
    Vec2 getDelta() const;</span>
 
(4)角度与弧度的相互转化 
 

在进行sin、tan等的函数运算中,参数常常为弧度,所以常常需要进行弧度与角度的转化,所幸的Cocos2d-x引擎提供了相关的API,具体API如下所示。

<span style="font-size:18px;">、/** @def CC_DEGREES_TO_RADIANS
 converts degrees to radians
 */
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180

/** @def CC_RADIANS_TO_DEGREES
 converts radians to degrees
 */
#define CC_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180</span>

(5)BillBoard类的使用

BillBoard类主要用于实现3D场景的公告板功能,所谓的公告板,即始终朝向相机所在点,或始终朝向相机局部坐标系的XoY平面的2D精灵。具体API如下所示。

<span style="font-size:18px;">BillBoard *board=BillBoard::create();
board->addChild("精灵即可");
board->setCameraMask((unsigned short)CameraFlag::USER1);</span>


(6)环境光的使用

在3D场景中,一般都需要添加环境光,具体API如下所示。

<span style="font-size:18px;">/**
     * Creates a ambient light.
     * @param color The light's color.
     *
     * @return The new ambient light.
     */
    static AmbientLight* create(const Color3B &color);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">void setLightFlag(LightFlag flag) { _lightFlag = flag;</span>

 
 

(7)定位光的使用

定位光在3D场景中起到至关重要的作用,下面是定位光中的主要方法。具体API如下所示。

<span style="font-size:18px;"><span style="font-size:18px;">/**
     * Creates a direction light.
     * @param direction The light's direction
     * @param color The light's color.
     *
     * @return The new direction light.
     */
    static DirectionLight* create(const Vec3 &direction,const Color3B &color);</span></span>
<span style="font-size:18px;"></pre><pre name="code" class="cpp"><span style="font-size:18px;"><pre name="code" class="cpp">/**
     * Sets the Direction in parent.
     *
     * @param dir The Direction in parent.
     */
    void setDirection(const Vec3 &dir);</span></span>

 
(8)ControlPotentiometer类(圆盘拖拉条)的使用 
 

这个控件有很多用处,可以让用户更改某些参数,在项目中,我们是当做当前英雄的血量。具体API如下所示。

<span style="font-size:18px;"><span style="font-size:18px;">/**
     * Creates potentiometer with a track filename and a progress filename.//背景图、周边图、中间旋转按钮图
     */
    static ControlPotentiometer* create(const char* backgroundFile,const char* progressFile,const char* thumbFile);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;"><pre name="code" class="cpp">m_potentiometer->setMinimumValue(float minimumValue);
m_potentiometer->setMaximumValue(float maximumValue);
m_potentiometer->addTargetWithActionForControlEvents(Ref* target,Handler action,EventType controlEvents);   //添加监听</span></span>


 
(9)MotionStreak拖尾类的使用 
 

Cocos2d-x引擎自带的拖尾功能很是方便,如果要是用户自己实现拖尾效果,将是很大的代码量。这个类主要用于实现滑动屏幕时,设定一条拖尾条带。具体API如下所示。

<span style="font-size:18px;"><span style="font-size:18px;">/** Creates and initializes a motion streak with fade in seconds,minimum segments,stroke's width,color,texture filename.
     *
     * @param fade The fade time,in seconds.<span style="white-space:pre">			</span>//1-消隐动画时长 2-拖尾条带相邻节点的最小距离
     * @param minSeg The minimum segments.<span style="white-space:pre">			</span>//3-拖尾条带的宽度 4-颜色 5-纹理路径
     * @param stroke The width of stroke.
     * @param color The color of stroke.
     * @param path The texture file name of stoke.
     * @return An autoreleased MotionStreak object.
     */
    static MotionStreak* create(float fade,float minSeg,float stroke,const Color3B& color,const std::string& path);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">m_streak->setBlendFunc(BlendFunc::ADDITIVE);<span style="white-space:pre">			</span>//设置混合
</span></span>

(10)RemoveSelf类的使用

这个类继承于CCActionInstant类,同级别的类还有CCShow、CCHide、CCFlipX等,所以是没有中间过程直接调用的动作。此动作主要用于在一连串动作执行完毕后,销毁自身。具体API如下所示。

<span style="font-size:18px;"><span style="font-size:18px;">/** Create the action.
     *
     * @param isNeedCleanUp Is need to clean up,the default value is true.
     * @return An autoreleased RemoveSelf object.
     */
    static RemoveSelf * create(bool isNeedCleanUp = true);</span></span>
项目暂时就总结到这里,以后有更深度的理解或者更广度的知识跨度,在进行补充。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读