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展示给用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值