iphone - How to launch a cocos2D game from single view app -
i created regular simple ios app (a single view application) 1 button , want launch cocos2d game ios app. in other words, want launch cocos2d game when press on button myapp.xib. don't want use url schemes launch game because require user download app download game. want user able launch game app internally. game want launch app:
http://www.raywenderlich.com/14439/how-to-make-a-game-like-fruit-ninja-with-box2d-and-cocos2d-part-3
i able add game dependency in xcode project. however, i'm not sure how go launching game app.
here's ideas had, didn't work me. there way can:
- call app delegate of game (not app delegate of app) ibaction in app launch game?
- call didfinishlaunchingwithoptions method of game (not didfinishlaunchingwithoptions of app) ibaction in app launch game?
- call main.m file of game (not main.m of app) ibaction in app launch game?
from understand, these 3 different ways of launching ios app. keep in mind developing app (not game) allow user launch game above internally through app. ideally, nice (and easy) if can pushviewcontroller app game, i'm not sure if there easy way approach this.
is there anyway can launch game internally through app? advice, suggestions, sample source code appreciated.
short answer: yes
you need have 1 appdelegate
(the 1 of ios app), , move there cocos2d
init stuff. then, can launch game ibaction
this.-
ccdirector *director = [ccdirector shareddirector]; [director pushscene:[yourfirstgamescene node]]; [director resume]; [self presentmodalviewcontroller:director animated:yes];
please, take next snippet example init/end cocos2d
appdelegate
- (void) initcocos { // create ccglview rgb565 color buffer, , depth buffer of 0-bits ccglview *glview = [ccglview viewwithframe:[self.window bounds] pixelformat:keaglcolorformatrgba8 //keaglcolorformatrgba8 depthformat:0 //gl_depth_component24_oes preservebackbuffer:no sharegroup:nil multisampling:no numberofsamples:0]; self.director = (ccdirectorios*) [ccdirector shareddirector]; self.director.wantsfullscreenlayout = yes; // display fsp , spf [self.director setdisplaystats:no]; // set fps @ 60 [self.director setanimationinterval:1.0/60]; // attach openglview director [self.director setview:glview]; // rotation , other messages [self.director setdelegate:self]; // 2d projection [self.director setprojection:kccdirectorprojection2d]; // [director setprojection:kccdirectorprojection3d]; // enables high res mode (retina display) on iphone 4 , maintains low res on other devices if( ! [self.director enableretinadisplay:no] ) cclog(@"retina display not supported"); // default texture format png/bmp/tiff/jpeg/gif images // can rgba8888, rgba4444, rgb5_a1, rgb565 // can change anytime. [cctexture2d setdefaultalphapixelformat:kcctexture2dpixelformat_rgba8888]; // when in iphone retinadisplay, ipad, ipad retinadisplay mode, ccfileutils append "-hd", "-ipad", "-ipadhd" loaded files // if -hd, -ipad, -ipadhd files not found, load non-suffixed version //[ccfileutils setiphoneretinadisplaysuffix:@"-hd"]; // default on iphone retinadisplay "-hd" [ccfileutils setipadsuffix:@""]; // default on ipad "" (empty string) //[ccfileutils setipadretinadisplaysuffix:@"-ipadhd"]; // default on ipad retinadisplay "-ipadhd" // assume pvr images have premultiplied alpha [cctexture2d pvrimageshavepremultipliedalpha:yes]; } - (void) endcocos { cc_director_end(); self.director = nil; }
actually, call initcocos
before pushing director
, , endcocos
after closing game, like
appdelegate *appdelegate = (appdelegate *) [[uiapplication sharedapplication] delegate]; [[ccdirector shareddirector] dismissmodalviewcontrolleranimated:yes]; [appdelegate endcocos];
hope helps.
Comments
Post a Comment