• CCSprite精灵更换图片


    方法一:

    	//创建精灵
    	CCSprite *pSprite = CCSprite::create("audio_on.png");
    
    	//创建纹理
    	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("audio_off.png");
    	
    	//给精灵设置新的纹理
    	pSprite->setTexture(texture);


    方法二:

        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CCSprite* pSprite2 = CCSprite::create("bg2.png");
    
        pSprite->setPosition(ccp(11,22));
        pSprite2->setPosition(ccp(22,33));
    
        this->addChild(pSprite, 0);
        this->addChild(pSprite2, 1);
    
        //需要注意的是displayFrame(),前提是你的精灵是addChild()到场景中的,不然获得的displayFrame会为空
        //pSprite设置当前显示帧为pSprite2正在显示的的帧(完成更换)
        pSprite->setDisplayFrame(pSprite2->displayFrame());

    方法三:

    我们可以使用

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res.plist");

    来代替

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res.plist","res.png");

    因为当参数仅为res.plist文件时,函数会自动寻找对应的res.png文件。

    当使用2个参数时,我们可以手动指定加载的png的名字,比如我们想加载res.plist来解析demo.png时,可以这样指定:

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res.plist","demo.png");

    当执行完上面的语句时,TP打包后的图片就已经载入内存了,我们就可以使用plist里面定义的key值来使用我们想要的图片(res1.png、res2.png)

    	//载入TP打包的plist和png文件到内存(有2张图片分别是res1.png和res2.png)
    	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("res.plist","res.png");
    
    	//创建精灵
    	pSprite = CCSprite::createWithSpriteFrameName("res2.png");
    
        pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    
        this->addChild(pSprite, 0);
    
    	//创建精灵帧
    	CCSpriteFrame* f = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("res1.png");
        
    	//更换图片
    	pSprite->setDisplayFrame(f);


     还有就是直接创建Frame:

    如下:

     

    至于newSpriteFrame怎么实现的,可以看源码,然后转换成cocos2dx的c++写法就好了。

  • 相关阅读:
    go语言从例子开始之Example22.协程之通道
    go语言从例子开始之Example21.协程
    go语言从例子开始之Example20.错误处理
    go语言从例子开始之Example19.接口
    级联复制改成主从复制
    一主二从改成级联复制架构步骤
    mysql8.0 备分常用命令
    mysql8基于gtid导出导入搭建主从
    MySQL 8.0 配置mysql_native_password身份验证插件的密码
    mysql_config_editor 安全登录方式
  • 原文地址:https://www.cnblogs.com/vokie/p/3602062.html
Copyright © 2020-2023  润新知