A simple understanding of applying mvc to cocos2d

Source: Internet
Author: User

Mvc, as its name implies, model, view, and controllercontroller include 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]) {// a data model corresponds to a view, if multiple models correspond to multiple views, add an array in the controller to save their management self. mymodel = [[[mvcModel alloc] init] autorelease]; self. myview = [[[mvcView alloc] init] autorelease]; // implement the notify interface so that the controller can obtain the touch event [[[CCDirector shareddire] touchDispatcher] addTargetedDelegate: self priority: 0 swallowsTouches: YES];} return self;} // touch event Method-(BOOL) ccTouchBegan :( UITouch *) touch withEvent :( UIEvent *) event {static int x = 1; // Modify the title attribute in the model. view automatically modifies self. mymodel. title = [NSString stringWithFormat: @ "under % d", x]; self. mymodel. mypos = CGPointMake (100 + x, 100); // call the method to make the model change display [self. mymodel didChange: self. myview]; x ++; return YES;}-(void) dealloc {[[CCDirector shareddirecher] touchDispatcher] removeDelegate: self]; [mymodel release]; [myview release]; [super dealloc] ;}@ end ------------- model. h -------------- # import <Foundation/Foundation. h> // interface part @ 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 ;} // call the method in the interface, that is, call the method in the view that implements the interface-(void) didChange :( id <ModelDelegate>) delegate {[delegate modelDidChange: self];} -(void) dealloc {[title release]; [super dealloc] ;}@ end ------------ view. h --------------- # import "cocos2d. h "# import" mvcModel. h "// implement model interface @ interface mvcView: CCLabelTTF <ModelDelegate> @ end ------------ view. m -------------- # import "mvcView. h "# import" mvcController. h "@ implementation mvcView-(id) init {if (self = [super initWithString: @" mvc mode "fontName: @" Marker Felt "fontSize: 32]) {self. anchorPoint = ccp (0.5, 0.5); self. position = ccp (320/2, 480/2);} return self;} // model change call interface method change view display-(void) modelDidChange :( mvcModel *) model {self. string = model. title; self. position = model. mypos;} @ end summary: The controller completes the interaction with the user. The controller modifies its own model. After the model is changed, the view in the controller is changed through the Protocol and displayed to the user.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.