• cocos2dx游戏--欢欢英雄传说--添加人物


    接下来需要导入精灵帧资源,因为之前下载了TexturePacker,然后通过TexturePacker的"Publish sprite sheet"方法可以生成一个.pvr.ccz文件和一个.plist文件,但是我试用版生成的.pvr.ccz文件里面是加码的,所以我决定这里还是先通过一张图片一张图片添加的方式把图片资源加入到SpriteFrameCache当中。
    附:原来添加plist的方式是继续在init函数中加入:

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("image/role.plist","image/role.pvr.ccz");

    我画了两张图片用于表示hero和enemy,名为hero-0.png和enemy-0.png,同样放在Resources/images/目录下。
    hero-0.png:

    enemy-0.png:

    在使用plist的方法中,我们需要在HelloWorld::init()方法中将图片加入到SpriteFrameCache中,方法如下:

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("images/hero-0.png", "images/enemy-0.png");

    但是因为还没有获得TexturePacker的license key,所以这里现不使用这种方法。
    我们将游戏中的人物分为两类,一类就是我们将要控制的玩家,另一类就是敌人,我们可以定义一个枚举类型PlayerType,然后再定义一个Player类,在Player的构造函数中通过传入参数PlayerType来确定Player的类型。
    Player.h:

    #ifndef __Palyer__
    #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);
    };
    
    #endif

    Player.cpp:

    #include "Player.h"
    bool Player::initWithPlayerType(PlayerType type)
    {
        std::string spName = "";
        switch (type)
        {
            case PlayerType::HERO:
                spName = "hero-0.png";
                break;
            case PlayerType::ENEMY:
                spName = "enemy-0.png";
                break;
        }
        this->initWithSpriteFrameName(spName);
        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;
        }
    }

    还需要在CMakeLists.txt中添加有关Player的信息:

    set(GAME_SRC
      Classes/AppDelegate.cpp
      Classes/HelloWorldScene.cpp
      Classes/Player.cpp
      ${PLATFORM_SPECIFIC_SRC}
    )
    
    set(GAME_HEADERS
      Classes/AppDelegate.h
      Classes/HelloWorldScene.h
      Classes/Player.h
      ${PLATFORM_SPECIFIC_HEADERS}
    )

    因为我们没有从plist中读取图片,而是一张图片一张图片地将图片加入到SpriteFrameCache中,所以我们在HelloWorld::init()函数中加入如下代码来将图片加入到SpriteFrameCache中:

        SpriteFrame *spriteFrame = CCSpriteFrame::create("images/hero-0.png", CCRectMake(0, 0, 99, 140));
        Sprite *sprite = CCSprite::createWithSpriteFrame(spriteFrame);
        SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-0.png");
        spriteFrame = CCSpriteFrame::create("images/enemy-0.png", CCRectMake(0, 0, 112, 136));
        sprite = CCSprite::createWithSpriteFrame(spriteFrame);
        SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-0.png");

    然后根据PlayerType将Hero和Enemy两个精灵加入到游戏当中:

        Player* player = Player::create(Player::PlayerType::HERO);
        player->setPosition(origin.x + player->getContentSize().width/2, origin.y + visibleSize.height/2);
        this->addChild(player);
        Player* enemy = Player::create(Player::PlayerType::ENEMY);
        enemy->setPosition(origin.x + visibleSize.width - player->getContentSize().width/2, origin.y + visibleSize.height/2);
        this->addChild(enemy);

    程序运行效果:

    完整的HelloWorldScene.cpp:

    #include "HelloWorldScene.h"
    #include "SimpleAudioEngine.h"
    #include "Player.h"
    
    USING_NS_CC;
    
    Scene* HelloWorld::createScene()
    {
        // 'scene' is an autorelease object
        auto scene = Scene::create();
    
        // 'layer' is an autorelease object
        auto layer = HelloWorld::create();
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        if ( !Layer::init() )
        {
            return false;
        }
    
        auto visibleSize = Director::getInstance()->getVisibleSize();
        Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
        Sprite* background = Sprite::create("images/background.png");
        background->setPosition(origin + visibleSize/2);
        this->addChild(background);
    
        SpriteFrame *spriteFrame = CCSpriteFrame::create("images/hero-0.png", CCRectMake(0, 0, 99, 140));
        Sprite *sprite = CCSprite::createWithSpriteFrame(spriteFrame);
        SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-0.png");
        spriteFrame = CCSpriteFrame::create("images/enemy-0.png", CCRectMake(0, 0, 112, 136));
        sprite = CCSprite::createWithSpriteFrame(spriteFrame);
        SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-0.png");
    
        Player* player = Player::create(Player::PlayerType::HERO);
        player->setPosition(origin.x + player->getContentSize().width/2, origin.y + visibleSize.height/2);
        this->addChild(player);
        Player* enemy = Player::create(Player::PlayerType::ENEMY);
        enemy->setPosition(origin.x + visibleSize.width - player->getContentSize().width/2, origin.y + visibleSize.height/2);
        this->addChild(enemy);
    
        return true;
    }
    
    
    void HelloWorld::menuCloseCallback(Ref* pSender)
    {
        Director::getInstance()->end();
    
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        exit(0);
    #endif
    }
    完整的HelloWorldScene.cpp
  • 相关阅读:
    centos7 安装docker 对应的 rabbitmq3.6.15
    golang中defer的使用规则
    Yii2-redis 不用 composer 的安装
    安装并使用PHPunit
    PHP Taint – 一个用来检测XSS/SQL/Shell注入漏洞的扩展
    SQL Antipatterns——SQL 反模式(二)
    tp5 No input file specified.
    vue 封装自定义组件
    判断微信访问
    phalcon windows下安装phalcon-devtools 官网的坑
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5556413.html
Copyright © 2020-2023  润新知