Tutorial: XCode4 integration of TexturePacker for Cocos2d and Sparrow framework

Intro

This tutorial shows you how to do integrate TexturePacker in your XCode 4 build within less than 5 minutes. The advantage is that TexturePacker now updates the sprite sheets during compile time! No manual work needed! The internal update mechanism in TexturePacker is very smart – it detects changes and only updates sheets that are really changed – saving you time!

Advantages for you:

  • Sprite sheets are up to date all the time
  • Save memory on subversion/git by not checking in the sprite sheets

Saving memory on your version management server

You don’t need to check in the complete sprite sheets in your version management system anymore. Assuming you have a bigger project you end up easily with several megabytes of sprite sheets. Why check them in if they can be built on the fly? There is not need for that. Simply check in your source sprites and let TexturePacker do the work during the build phase.

This is even more useful for git users since git stores the complete repository on your local computer.

Project structure

For this tutorial I assume that you have a similar layout of your project like I have here:

Your source sprites are all in an Assets folder, grouped into different directories to reflect the sprite sheets.

If you have a different layout – you might need to modify the scripts a bit.

2 Ways to accomplish the goal

There are 2 ways you can integrate TexturePacker into XCode.

The easy way

The easy way is to create .tps files from TexturePacker and update them from the build phase. The advantage is that you can open the .tps files in the graphical user interface and change them. No need to do hard work on the command line.

If so please save the .tps files in your Assets folder so the script can find them:

If you want to go this way use this script for the next phase:

#! /bin/sh
TP=/usr/local/bin/TexturePacker
if [ "${ACTION}" = "clean" ]
then
# remove sheets - please add a matching expression here
rm ../Resources/sheet*.png
rm ../Resources/sheet*.plist
else
# create all assets from tps files
${TP} *.tps
fi
exit 0

You need to adjust the rm part to match your sheet’s names during the clean phase.

From my point of view the most important feature of TexturePacker is the SmartFolders feature. It simply adds all sprites it finds in a directory structure to your sprite sheet. This allows you to do fast updates by simply throwing the new sprites into the folder without the need of adding single sprites.

If you create your sprite sheet use this button or simply drag the folder onto the right “Sprites” panel:

TexturePacker also allows you to organize your sprites into subfolders. This is very helpful to keep sprites grouped – e.g. by making a separate directory for each enemy.

The way of the poweuser

Instead of creating .tps files you can simply use the command line interface of TexturePacker:

#! /bin/sh
TP=/usr/local/bin/TexturePacker
if [ "${ACTION}" = "clean" ]
then
# remove all files
rm ../Resources/sheet*.png
rm ../Resources/sheet*.plist
else
# create hd & sd assets
${TP} --smart-update sheet1 \
--auto-sd \
--format cocos2d \
--data ../Resources/sheet-hd.plist \
--sheet ../Resources/sheet-hd.png

# create ipad assets from same sprites
${TP} --smart-update --scale 1.066 sheet1 \
--format cocos2d \
--data ../Resources/sheet-ipad.plist \
--sheet ../Resources/sheet-ipad.png

... add more sheets ....
fi
exit 0

This example here creates hd and sd plists and additionally resources for iPad (Remark: this is not the best option for iPad since this uses upscaling. It would be better to scale things down….).

Integration into XCode

This is now the main integration into XCode. First thing is to save the script from the phase before as Scripts/TP-update.sh

The usual way to continue with the integration would be to add a “Run script” build phase – but this would require adding all the sprites as dependencies which has no practical use. Instead we create a new target and let TexturePacker decide what needs to be updated.

Choose “External Build System”:

Choose a name for the target e.g. “SpriteSheet” and use /bin/bash as build tool:

Next edit the target:

  • BuildTool: /bin/bash
  • Arguments: ../Scripts/TP-update.sh
  • Directory: ${SRCROOT}/TexturePackerIntegrationTest/Assets (adjust this to your project’s name)

Now we have a target that we can build. To make things automated we need to set dependencies from the main project to the sheet target:

That’s all.

Improved script

This is an improved version of the sprite sheet script created by MikeSz in the cocos2d forum:

It has a slightly modified layout and keeps all sprites sheets in a separate folder:

#! /bin/sh
TP=/usr/local/bin/TexturePacker
if [ "${ACTION}" = "clean" ]
then
    # remove sheets - please add a matching expression here
    rm -f ${PROJECT_DIR}/SpriteSheets/*.pvr.ccz
    rm -f ${PROJECT_DIR}/SpriteSheets/*.pvr
    rm -f ${PROJECT_DIR}/SpriteSheets/*.plist
    rm -f ${PROJECT_DIR}/SpriteSheets/*.png
else
    # create all assets from tps files
    mkdir -p ${PROJECT_DIR}/SpriteSheets

    ${TP} *.tps
fi
exit 0

Tags: , , , ,

11 Responses to “Tutorial: XCode4 integration of TexturePacker for Cocos2d and Sparrow framework”

  1. Tetek July 24, 2011 at 5:00 pm #

    Hi, this is brilliant, thanks ! Just one thing that bothers me is that groups in xcode isn’t actual directories in file system. I’m keeping assets out of xcode. Updating sheets work fine, but new sheet I’m transferring manually to xcode, do you have any way around this? Again, thanks for the great work !

    • Andreas Löw July 24, 2011 at 5:20 pm #

      You should be able to use a folder reference and Xcode. Then keep all items there.

  2. matt July 26, 2011 at 9:08 pm #

    hi andreas. thanks for this! however, i’m trying it out and am getting the following error:
    ../Scripts/TP-update.sh: line 10: /usr/local/bin/TexturePacker: No such file or directory

    any ideas? /usr/local/bin/TexturePacker does exist, and is set at 755, but when i try to run it from /usr/local/bin it gives the same error.

    this might not be something related to TexturePacker, but i figured i’d check in case you knew. i’ve done some searching but to no avail so far.

    thanks,

    matt

    • Andreas Löw July 26, 2011 at 9:37 pm #

      Hi,

      can you please check if the link points to /Applications/TexturePacker.app/Contents/MacOS/TexturePacker and if this is exists and runs?
      If you installed TexturePacker in another location it might not work.

      Cheers
      Andreas

  3. matt July 27, 2011 at 1:51 am #

    perfect, and i feel silly. when i downloaded TP i created a folder to hold both the app and the documentation pdf, and i completely forgot i had done that. thanks, andreas, and great work.

    matt

  4. Samuel December 2, 2011 at 2:21 am #

    Cool, frankly I don’t even know how to integrate the script into Xcode before read your post. Thanks!

  5. David December 30, 2011 at 11:37 pm #

    There is any way to speedup the process when no assets have been changed?

    For me, the TexturePacker takes a lot to check for changes. My project only have one sheet 1024×2048 with about 230 png files.

    By the way, there is a typo on the poweruser script:

    –data ../Rresources/sheet-hd.plist \

    there is a double R on Resources.

    May be the second ‘r’ should go on “poweuser” word :)

    • Andreas Löw January 25, 2012 at 6:50 am #

      Thanks for the feedback – it’s fixed.

      TexturePacker already tries to speed things up – by writing a hash key inside the data file which contains informations about sprites and options used.

      If there is no data file (e.g. because you overwrite it in batch mode or save it to /dev/null) this check can’ be performed and the sheet will be regenerated.

  6. Hanley Leung March 7, 2012 at 2:32 pm #

    andreas, tool is working pretty well for us. we’re trying to leverage the commandline program to automate stuff even more! right now we have folders where our artists throw their assets into. the list of assets can grow as artists want/need to add more images. is there a way to have texturepacker automatically create enough textures to accomodate all the images in the folder? so say for folder c:/FOO …there’s 100 images in there that need about 10 textures to fit them all, then texture packer will automatically create foo1.pvr, foo2,pvr,…..foo10.pvr and the associated plists?

    thanks so much again! great tool!

    • Andreas Löw March 15, 2012 at 9:23 am #

      I am already working on this – it’ll be part of one of the next updates in 3.0.0b3/b4 perhaps.

Trackbacks/Pingbacks

  1. Python shortcut for animation plists | abitofcode - February 23, 2012

    [...] I had a list of 208 separate frames of animation that were added to a sprite map as part of the build in XCode using TexturePacker. [...]

Leave a Reply