Cocos2d-X----精灵

前端之家收集整理的这篇文章主要介绍了Cocos2d-X----精灵前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、精灵创建的3种方式

</pre><p><pre name="code" class="cpp">#include "T02LayerSprite.h"


CCScene * T02LayerSprite::scene()
{
	CCScene * scene = CCScene::create();
	T02LayerSprite * layer = T02LayerSprite::create();
	scene->addChild(layer);

	return scene;

}

bool T02LayerSprite::init()
{
	if (!CCLayer::init())
	{
		return false;
	}
	//系统默认的图片路径是工程下Resource,
	
	//截图功能:至左上角开始截图,向右下,,CCRectMake(0,480,320)
	CCSprite * backGround = CCSprite::create("sprite/background.jpg",CCRectMake(0,320));
	backGround->setAnchorPoint(ccp(0,0));
	//backGround->setScaleX(0.8);
	backGround->setPosition(ccp(0,0));
	addChild(backGround);

	CCImage * image = new CCImage;
	image->initWithImageFile("sprite/plant.png");
	image->autorelease();

	//纹理,本质就是图片,OpenGL术语,加载进内存
	CCTexture2D * texture = new CCTexture2D;
	texture->initWithImage(image);
	texture->autorelease();

	CCSprite * plant = CCSprite::createWithTexture(texture);
	plant->setPosition(ccp(100,160));
	addChild(plant);

	CCTexture2D * texture2 = CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png");
	CCSprite *bullet = CCSprite::createWithTexture(texture2);
	bullet->setPosition(ccp(250,180));
	addChild(bullet);

	//本质是,截帧动画当中的一帧,一帧的本质其实就是一张图片,来创建精灵
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite/framecache/zombie.plist");
	CCSpriteFrame * sprFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("z_1_attack_05.png");

	CCSprite * zombie = CCSprite::createWithSpriteFrame(sprFrame);
	zombie->setPosition(ccp(420,180));
	addChild(zombie);

	CCMoveBy * by = CCMoveBy::create(3,ccp(300,0));
	bullet->runAction(by);


	return true;
}
#ifndef __T02LayerSprite_H__
#define __T02LayerSprite_H__

#include "cocos2d.h"

USING_NS_CC;

class T02LayerSprite : public CCLayer
{
public:
	static CCScene * scene();
	CREATE_FUNC(T02LayerSprite);
	bool init();

};

#endif


 


猜你在找的Cocos2d-x相关文章