从Delphi开始学Cocos2dx-3.0[14]:动画-直接从精灵帧缓存中获取纹理

27 篇文章 2 订阅

cocos2d 中有纹理和精灵帧两个概念,当精灵要加载整个png图片的时候,纹理和精灵帧其实是一样的内容. 多数的时候, 精灵帧只是纹理的其中一部分


TScene* THelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = TScene::create();
    
    // 'layer' is an autorelease object
    auto layer = THelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

	// 获取纹理缓存指针
	auto cache = TTextureCache::getInstance();
	// 加载纹理
	cache->addImage("all.png");


    // return the scene
    return scene;
}
把6个动作按顺序排好,并且放到一张png上 加载



bool THelloWorld::onTouchBegan(TTouch* touch, TEvent* event)
{	
	 auto sprite =  (TSprite*)(this->getChildByTag(1000));      
  
    //sprite->stopAllActions();  
    sprite->cleanup();  
    sprite->runAction(TPlace::create(TPoint(100.0f, g_ClientMidY)));  
  
    auto animation = TAnimation::create();  
    // 转载图片  
    for (int i = 1; i <= 6; i++)  
    {  
		// 加载精灵帧
		auto frame = TSpriteFrame::create("all.png", TRect(100 * i - 100,0,100,128));
		// 改成添加精灵帧
		animation->addSpriteFrame(frame);
    }  
  
    // 设置动画播放的属性 2秒 6帧  
    animation->setDelayPerUnit(1.2f / 6.0f);  
  
    // 设置精灵帧的使用方式, 做完动画还原成初始帧  
    animation->setRestoreOriginalFrame(false);  
  
    // 重复10次  
    animation->setLoops(3);  
  
    // 创建动画动作  
    auto action = TAnimate::create(animation);  
  
    sprite->runAction(TSpawn::create(action, TMoveBy::create(1.2f * 3,TPoint(500.0f, 0.0f)), NULL));  
  
    CCLOG("THelloWorld::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);  
    return true;  
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值