当前位置:编程学习 > wap >>

mvc应用到cocos2d的简单理解

mvc顾名思义model,view,controller
controller包含model,view
-------------controller.h--------------
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "mvcModel.h"
#import "mvcView.h"
 
@interface mvcController : NSObject <CCTouchOneByOneDelegate>
{
    mvcModel *mymodel;
    mvcView *myview;
}
@property (nonatomic, retain) mvcModel *mymodel;
@property (nonatomic, retain) mvcView *myview;
@end
-------------controller.m--------------
#import "mvcController.h"
 
@implementation mvcController
@synthesize mymodel,myview;
 
 
-(id) init {
    if (self=[super init])
    {
        // 一个数据模型对应一个view,如果多个模型就对应多个view,在controller中增加array保存对它们的管理
        self.mymodel = [[[mvcModel alloc]init] autorelease];
        self.myview = [[[mvcView alloc]init] autorelease];
        
        // 实现CCTouchOneByOneDelegate接口,使得controller获取touch事件
        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    }
    return self;
}
 
// touch事件方法
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
    static int x = 1;
    
    // 修改model中的title属性,view会自动修改
    self.mymodel.title = [NSString stringWithFormat:@"第%d下",x];
    self.mymodel.mypos = CGPointMake(100+x, 100);
    
    // 调用方法使model的改变通过view显示
    [self.mymodel didChange:self.myview];
    
    x++;
    return YES;
}
 
 
- (void)dealloc
{
    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
    [mymodel release];
    [myview release];
    [super dealloc];
}
@end
-------------model.h--------------
#import <Foundation/Foundation.h>
 
// 接口部分
@class mvcModel;
@protocol ModelDelegate <NSObject>
 
@required
-(void)modelDidChange:(mvcModel *)model;
@end
 
 
@interface mvcModel : NSObject
{
    NSMutableSet *delegates;
    NSString *title;
    CGPoint mypos;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic) CGPoint mypos;
 
-(void)didChange:(id<ModelDelegate>)delegate;
 
@end
-------------model.m--------------
#import "mvcModel.h"
 
@implementation mvcModel
@synthesize title,mypos;
 
-(id)init {
    if (self=[super init]) {
        delegates = [NSMutableSet new];
    }
    return self;
}
 
// 调用接口中的方法,既调用了实现接口的view中方法
-(void)didChange:(id<ModelDelegate>)delegate
{
    [delegate modelDidChange:self];
}
 
 
- (void)dealloc
{
    [title release];
    [super dealloc];
}
 
@end
--------------view.h---------------
 
#import "cocos2d.h"
#import "mvcModel.h"
 
// 实现model接口
@interface mvcView : CCLabelTTF<ModelDelegate>
 
@end
 
--------------view.m--------------
#import "mvcView.h"
#import "mvcController.h"
 
@implementation mvcView
 
-(id) init {
    if (self=[super initWithString:@"mvc模式" fontName:@"Marker Felt" fontSize:32])
    {
        self.anchorPoint = ccp(0.5, 0.5);
        self.position = ccp(320/2, 480/2);
    }
    return self;
}
 
// model改变调用接口方法更改view显示
-(void)modelDidChange:(mvcModel *)model
{
    self.string = model.title;
    self.position = model.mypos;
}
 
@end
 
总结controller完成了和用户的交互部分,
controller修改了自己包含的model,
model改变后通过协议更改了controller中的view展示给用户
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,