cocos2D-X create image sprite solution

	int width, height;

	unsigned char *data = SOIL_load_image("06a03.jpg", &width, &height, 0, SOIL_LOAD_RGBA);

	Image *image = new Image();

	//cocos2d::Texture2D::PixelFormat::RGBA8888
	image->initWithRawData(data,width * height,width,height, 2);
	image->setPNGPremultipliedAlphaEnabled(true);
	SOIL_free_image_data(data);

	auto so = Sprite::createWithTexture(Director::getInstance()->getTextureCache()->addImage(image,"key"));
	so->setPosition(display / 2);
	delete image;
	this->addChild(so);

 

posted @ 2020-04-04 00:03  YZFHKMS-X  阅读(175)  评论(0编辑  收藏  举报