- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Siddharth Shekar
- 431字
- 2021-07-16 09:43:45
Creating a sprite using RenderTexture
RenderTexture
is used to create placeholder sprites that can be used to prototype a game. So, if you want to test your movement and jump code on a sprite but don't have access to a sprite, this is a quick and dirty way to create a sprite.
Getting ready
To create the RenderTexture
sprite, we will create a new function, and this function will return a sprite when we provide the size and color of the sprite to be produced.
How to do it…
In the MainScene.h
file, we will add the following highlighted line right under the scene
function we created earlier:
+(CCScene*)scene; -(CCSprite *)spriteWithColor:(ccColor4F)bgColor textureWidth:(float)textureWidth textureHeight:(float)textureHeight; @end
This function will return CCSprite
and take in the color, width, and height that the sprite should be of.
In the MainScene.m
file, we will add the definition of the preceding function below the init
function, as follows:
-(CCSprite *)spriteWithColor:(ccColor4F)bgColor textureWidth:(float)textureWidth textureHeight:(float)textureHeight { CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureWidth height:textureHeight]; [rtbeginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a]; [rt end]; return [CCSpritespriteWithTexture:rt.sprite.texture]; }
In the function, we will create a new variable called rt
of the CCRenderTexture
type, and to it, we will pass the width and height that is passed to the function.
Then, we will clear RenderTexture
with the color that is passed in. Then, we will call the end
function on rt
.
Next, we will create CCSprite
by passing in the texture of the rt
sprite. This is then returned by the function.
How it works…
To use the RenderTexture
function, we will add the following code right after where we added the background to the scene:
//rtSprite CCSprite* rtSprite = [self spriteWithColor:ccc4f(1.0, 1.0, 0.0, 1.0) textureWidth:150textureHeight:150]; rtSprite.position = CGPointMake(winSize.width/2, winSize.height/2); [selfaddChild:rtSprite];
We will create a new variable called rtSprite
of the CCSprite
type and assign the sprite that will be created by calling our function to it.
While calling the function, we will create a color of the ccc4f
type and pass in the r
, g
, b
, and a
values. For yellow, we will pass 1
for red and green. We will provide a width and height value of 150
each.
Then, the sprite will be positioned at the center and added to the scene. Run the scene to see the result.

There's more…
The color of the sprite can be changed by changing the rgba
color value.
For example, here, I changed the value to (1.0
, 0.0
, 1.0
, 1.0
) for yellow, and you can see the result as follows:
//rtSprite CCSprite* rtSprite = [self spriteWithColor:ccc4f(1.0, 0.0, 1.0, 1.0) textureWidth:150textureHeight:150]; rtSprite.position = CGPointMake(winSize.width/2, winSize.height/2); [selfaddChild:rtSprite];

- Objective-C Memory Management Essentials
- 潮流:UI設計必修課
- 數據庫原理及應用(Access版)第3版
- Python從菜鳥到高手(第2版)
- Python Deep Learning
- Instant 960 Grid System
- 人臉識別原理及算法:動態人臉識別系統研究
- Java EE 7 Development with NetBeans 8
- 深入淺出PostgreSQL
- Haskell Data Analysis Cookbook
- MySQL從入門到精通(軟件開發視頻大講堂)
- C語言開發基礎教程(Dev-C++)(第2版)
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Fastdata Processing with Spark
- SpringBoot從零開始學(視頻教學版)