Sparrow是一个轻量级的开源Objective-C游戏库,用于开发iOS设备上的游戏。相当于cocos2d来说,sparrow足够轻量级,部署方便,能够满足一般游戏的开发需求。
项目地址:https://github.com/Gamua/Sparrow-Framework
预备工作
1、在Xcode project references中创建到sparrow引擎的链接。
2、导入API提示,在Xcode preferences 中的 “Documentation” tab里,添加
2、以scaffold为项目框架,选择好目标设备。
项目最简单的框架,两个最关键的类:
AppDelegate类
@interface AppDelegate : NSObject <UIApplicationDelegate> @end @implementation AppDelegate { SPViewController *_viewController;//创建了一个Sparrow视图控制对象 UIWindow *_window; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // create a full-screen window CGRect screenBounds = [UIScreen mainScreen].bounds; _window = [[UIWindow alloc] initWithFrame:screenBounds]; // start up Sparrow _viewController = [[SPViewController alloc] init]; [_viewController startWithRoot:[Game class] supportHighResolutions:YES doubleOnPad:YES]; // activate the window [_window setRootViewController:_viewController]; [_window makeKeyAndVisible]; return YES; } @end
Game类
@interface Game : SPSprite @end @implementation Game - (id)init { if ((self = [super init])) { // 在这里进行游戏对象的初始化 // 这个例子中创建了一个方块 SPQuad *quad = [SPQuad quadWithWidth:100 height:100]; quad.color = 0xff0000; // 0xRRGGBB -> this is red quad.x = 50; quad.y = 50; [self addChild:quad]; } return self; } @end
一、对象的显示
对象的显示采用Display Tree的策略按层次添加和显示对象。将一个对象创建为容器对象(SPSprite 或 SPDisplayObjectContainer),就可以包含子对象。对象之间具有从属关系或并列关系。
SPImage和SPTexture对象
如果一张图片创建为SPTexture对象,程序会将图像二进制信息持久存储在内存中,方便重复快速的使用。如果图片创建为SPImage对象,每次显示都需要重新调入到内存来。
文本对象
文本由SPTextField类创建,默认使用设备自身的字体。
SPTextField *textField = [SPTextField textFieldWithWidth:145 height:80 text:@"Text" fontName:@"Helvetica" fontSize:12.0f color:0xff0000]; textField.hAlign = SPHAlignRight; // horizontal alignment textField.vAlign = SPVAlignBottom; // vertical alignment textField.border = YES;
宏定义
角度
float degrees = SP_R2D(PI); // -> 180.0f float radians = SP_D2R(180); // -> PI
颜色
uint yellow = SP_COLOR(255, 255, 0); // -> 0xffff00
或者
// format: 0xRRGGBB uint red = 0xff0000; uint green = 0x00ff00; // or 0xff00 uint blue = 0x0000ff; // or 0xff uint white = 0xffffff; uint black = 0x000000; // or simply 0
浮点数比较
float f = 0.00001f; bool isNearlyZero = SP_IS_FLOAT_EQUAL(f, 0.0f); // -> true
二、事件处理
下面的例子中,在Game对象的初始化函数中为一个SPButton对象添加触发事件响应。@implementation Game { SPButton *_button; } - (id)init { if (self = [super init]) { // 创建按钮 SPTexture *buttonTexture = [SPTexture textureWithContentsOfFile:@"button.png"]; _button = [SPButton buttonWithUpState:buttonTexture]; [self addChild:_button]; // 为按钮添加触发事件响应 [_button addEventListener:@selector(onButtonTriggered:) atObject:self forType:SP_EVENT_TYPE_TRIGGERED]; } return self; } //对事件的响应 - (void)onButtonTriggered:(SPEvent *)event { NSLog(@"The button was triggered!"); } - (void)dealloc { //当不再监听该事件时,删去事件监听 [_button removeEventListenersAtObject:self forType:SP_EVENT_TYPE_TRIGGERED]; }
常见的事件有:
- SP_EVENT_TYPE_TRIGGERED: a button was triggered
- SP_EVENT_TYPE_TOUCH: a touch event occurred
- SP_EVENT_TYPE_ADDED: a display object was added to a container
- SP_EVENT_TYPE_ADDED_TO_STAGE: a display object was added to a container that is connected to the stage
- SP_EVENT_TYPE_REMOVED: a display object was removed from a container
- SP_EVENT_TYPE_REMOVED_FROM_STAGE: a display object lost its connection to the stage
- SP_EVENT_TYPE_ENTER_FRAME: some time has passed, a new frame is rendered
- SP_EVENT_TYPE_COMPLETED: a sound or movie clip finished playback