cocos2d里面如何实现MVC(四)

    在上一篇文章中,我们使用cocos2d基于mvc做了一个简单了游戏架子,这个架子还非常简单,还有许多东西有待实现。

介绍模型

    在上一篇博文中,我们介绍了view和controller。为了实现mvc模式,我们还需要添加一个model类来维护游戏的状态。我们的实现应该要包含下列这些类:

  1. GameBoardView - 也就是View,
  2. GameBoardController - 也就是Controller.
  3. GameBoard – 也就是Model.

Model 实现

GameBoard 实现

    我们在第一部分所描述的需求是这样子的:

    。。。一个game board是通过n行n列组成的,它会随着游戏难度有所变化。

    因此,我们按照下面的编码方式来实现之:

@interface GameBoard : NSObject {
NSInteger numberOfRows;
NSInteger numberOfColumns;
}

- (id)initWithRows:(NSInteger)aNumberOfRows columns:(NSInteger)aNumberOfColumns;

@property (nonatomic) NSInteger numberOfRows;
@property (nonatomic) NSInteger numberOfColumns;

@end

    请注意,model是从NSObject继承过来的---因为model只需要关注game board的状态就行了(当然,还有相应的更新状态的方法)---我们不应该把其它东西也放进来,比如继承到CCNode就不行,我们并不需要CCNode的东西,所以,为了纯粹性,我们这里继承到game board。

GameBoardView 的实现

    我们现在需要修改View,同时它包含一个model的引用,我们可以通过initWithGameBoard方法来初始化这个成员变量:

@interface GameBoardView : CCNode {
GameBoard *gameBoard;
}

@property (nonatomic, retain) GameBoard *gameBoard;

- (id)initWithGameBoard:(GameBoard *)aGameBoard;

@end

   具体GameBoardView的实现细节如下:(为了演示方便,我们忽略了实际渲染每一个小方块的代码)

- (id)initWithGameBoard:(GameBoard *)aGameBoard {
if ((self = [super init])) {
// retain gameboard
self.gameBoard = aGameBoard;

// render gameboard background
CCSprite *gameboardSprite = [CCSprite spriteWithFile:@"gameboard.png"];
gameboardSprite.anchorPoint = CGPointMake(0, 0);

[self addChild:gameboardSprite];

// render spaces
for (int i = 0; i < gameBoard.numberOfRows; i++) {
for (int j = 0; j < gameBoard.numberOfColumns; j++) {
// position and render game board spaces
}
}
}

return self;
}

GameBoardController

    最后,我们要更新GameBoardController的init方法,因为view需要把GameBoard对象通过init方法注入进去,所以,我们在controller的init方法里面,就应该定义好model对象,然后传递给view。

- (id)init {
if ((self = [super init])) {
// initialize model
gameBoard = [[GameBoard alloc] initWithRows:7 columns:9];

// initialize view
view = [[GameBoardView alloc] initWithGameBoard:gameBoard];

[self addChild:view];
}

return self;
}

处理touch事件

GameBoardView updates

    为了能够处理touch事件,我们需要再稍微修改一下View。我们让它继承至CCLayer,而不是CCNode。因为CCLayer内置了处理touch事件的方法:

@interface GameBoardView : CCLayer {
...
}

    而view本身是不应该处理用户的交互(touch事件)的,所以,我们需要定义一个代理(GameBoardViewDelegate)。(译者:为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事,找别人去做,这就是代理。所以,当你写代码的时候,你想保持类的简单性、重用性,你就可以把事件尽量都交给其它类去做,自己只管做好自己的事。也就是SRP,单一职责原则。如果一个类关注的点过多,做的事情太多。这些事情不管是你直接做的,还是调用别的对象去完成的。这都不行,自己做这些事,那就会使类的功能复杂化,维护不方便。而过多地调用其它对象来完成一些事情,表面上看起来好像不错,实际上是过度耦合了。我们编写类的原则应该是追求高内聚,低耦合的。可能你会说,用代理不也是交给别人做吗?没错,问的好。但是,代理是接口,我们是针对接口编程,所以它的重用性会非常好。因此,下次当你想写可扩展和可重用的代码的时候,不妨先想想代理这个东西吧。objc里面delegate是用protocol实现的,而java和c++则是用接口实现的,具体他们之间怎么转换的,比较一下应该就可以了。)

@protocol GameBoardViewDelegate
- (void)gameBoard:(GameBoard *)gameBoard touchedAtRow:(int)row column:(int)column;
@end

    我们还需要再修改一下GameBoardView的init方法,通过传送一个delegate进来处理touch事件。

- (id)initWithGameBoard:(GameBoard *)aGameBoard delegate:(id)aDelegate;

下面是touch事件的具体实现:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [self convertTouchToNodeSpace:touch];

// calculate row and column touched by the user and call a delegate method
// ...
[self.delegate gameBoard:self.gameBoard touchedAtRow:row column:column];
}

GameBoardController 更新

    GameBoardController将会负责处理用户touch事件,所以,我们需要让GameBoardController实现GameBoardViewDelegate接口:

@interface GameBoardController : CCNode<GameBoardViewDelegate>
- (void)gameBoard:(GameBoard *)gameBoard touchedAtRow:(int)row column:(int)column {
// do the game logic here and update view accordingly
}

   还有最后一步,那就是修改view的init方法,把controller传递进去。

// initialize view
view = [[GameBoardView alloc] initWithGameBoard:gameBoard delegate:self];

总结

    在这篇文章中,我们实现了Model,同时还通过一些代码把view和controller联系起来了。同时,我们还在view和controller,以及用户touch事件之间建立了联系,这样controller类就可以来响应游戏里面的用户输入了。(译者:为什么费这么多劲,无非就是职责分离,这个非常重要!)。在接下来的文章里面,我们会谈到下面两个问题:

  • 在Controller里面更新Model,
  • 通知View关于Model的改变.

 

 

后记:本文已同步更新到cocos2d mvc这个系列里面去了。

    如果你觉得本文章对你有所帮助,请您点一下旁边的“推荐”按钮,这样可以让更多的人看到,同时也会给我写作的动力,谢谢大家。








posted on 2012-03-18 19:59  子龙山人  阅读(7521)  评论(7编辑  收藏  举报