Game Development Reference
In-Depth Information
Figure 10-6 . Texture coordinates need to be large enough to encompass the entire image, but so large that
they draw parts of neighboring images in the Sprite Sheet
Calculating the texture coordinates is done in the updateTextureCoordinates
method. Add the code in Listing 10-12 below the setup method. The method was separ-
ated because if you want to run a sprite frame animation in the soft-body player, you have
to call this method every time the spriteFrame property changes in order to update the
texture coordinates to the new sprite frame's coordinates.
Listing 10-12 . Updating texture coordinates
-(void) updateTextureCoordinates
{
CCSpriteFrame* spriteFrame = self.spriteFrame;
CGSize textureSize = spriteFrame.texture.contentSize;
CGRect frameRect = spriteFrame.rect;
CGSize frameSize = spriteFrame.originalSize;
for (int i = 0; i < _numVertices; i++)
{
_vertices[i].color = GLKVector4Make(1.0, 1.0, 1.0,
1.0);
CGPoint pos = _initialBodyPositions[i];
pos.x += frameRect.origin.x;
pos.y = (frameRect.origin.y + frameSize.height)
- pos.y;
_vertices[i].texCoord1 = (GLKVector2){
MAX(0.0, MIN(pos.x / textureSize.width, 1.0)),
1.0 - MAX(0.0, MIN(pos.y / textureSize.height,
1.0)),
};
}
int lastIndex = _numVertices - 1;
_vertices[lastIndex].texCoord1 = _vertices[1].texCoord1;
_vertices[lastIndex].color = _vertices[1].color;
}
At first, the updateTextureCoordinates creates local copies of some variables.
The CCSpriteFrame class defines the region within a texture. A Sprite Sheet texture
typically contains multiple images, and sprite frames are the way to access these images.
Search WWH ::




Custom Search