1. 自定义父类 View, 将游戏画面共同属性函数放在父类
// 游戏画面控制对象
1.1BaseView.h 头文件
@interface BaseView : UIView {
// 上下文环境
CGContextRef currentContext;
BOOL mbLoop;// 绘图控制标记
BOOL mFlag;// 控制需要的时候绘图
int stationId;// 状态 ID
CGContextRef baseCGContext;// 绘画上下文环境
}
@property (nonatomic, retain) NSTimer *pingMe;
@property long offlineTime;// 掉线时间
@property CGContextRef baseCGContext;// 绘画上下文环境
@property BOOL mbLoop;// 绘图控制标记
@property BOOL mFlag;// 控制需要的时候绘图
@property int stationId;// 状态 ID
1.2BaseVeiw.m 实现类
#import "BaseView.h"
#import "NetControl.h"
#import "GameRoomConst.h"
#import "SuperMahjongNet.h"
extern CEncTcp *g_pSi; // 后台通信线程全局对象
@implementation BaseView;
@synthesize baseCGContext;// 画面上下文的 二级缓存
@synthesize mbLoop;// 绘图控制标记
@synthesize mFlag;// 控制需要的时候绘图
@synthesize stationId;// 状态 ID
- (id)initWithFrame:(CGRect)frame { // 所有的绘画在此函数或子类的此函数中完成
NSLog(@"Enter BaseView!!!");
if (self = [super initWithFrame:frame]) {
// 数据初始化
[self initBaseData];
// 指定色彩空间为 RGB
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// 创建 BitmapContext 大小为 View 自身大小
baseCGContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4*self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
// 释放色彩空间
CGColorSpaceRelease(colorSpace);
mFlag = false;
// 开始绘制图片
mbLoop = true;
// 启动心跳线程
if(pingMe == nil){
pingMe = [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(goPingMe) userInfo:nil repeats:YES] retain];
}
}
[self setNeedsDisplay];
return self;
}
// 绘制控制主方法
-(void)draw{
NSAutoreleasePool *drawPool = [[NSAutoreleasePool alloc] init];
[self setNeedsDisplay];// 刷新画面,自动调用 drawRect ()函数
[drawPool release];
}
}
2. 写画面子类,完成具体游戏的绘画
#import <UIKit/UIKit.h>
#import "BaseView.h"
1.MahjongGameView.h 文件
@interface MahjongGameView : BaseView {
NSTimer *drawFrame;// 控制画面刷新
UIFont *paintFont;
/********** 坐标数据 ********* /
CGRect myScreenCGRect; // 屏幕矩形大小数据
CGRect drawCGRect; // 绘画大小数据
int STATUBAR_Height;// 状态栏高度
}
2.MahjongGameView.m 文件
#import "MahjongGameView.h"
#import "BaseControl.h"
@implementation MahjongGameView
-(id)initWithFrame:(CGRect)frame {
NSLog(@"Enter SuperMahjongView!!!");
if (self = [super initWithFrame:frame]) {
// 图片资源初始化
[self initBasicGameCanvas];
// 坐标数据初始化
[self initBaseData];
// 设置本人椅子号
self.myTableID = g_myGamePlayer.wTableID;// 设置桌子号
[self findPlayerChairs:g_myGamePlayer.cbChairID];// 设置桌子号
// 初始化玩家
[self updateGamePlayer];
// 启动绘画线程,调用父类的 draw 函数,子类不重写此函数
drawFrame = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(draw) userInfo:nil repeats:YES] retain];
// 切屏幕
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
// 切换坐标始终为左上点
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2.0f);
self.transform = transform;
self.center = CGPointMake(160, 240);
[[UIApplication sharedApplication ] setStatusBarHidden:YES];
}
return self;
}