From 70b4d701d3d78b272118f24ad22938aac199db5a Mon Sep 17 00:00:00 2001 From: Walzer Date: Fri, 24 Jun 2011 21:25:51 +0800 Subject: [PATCH] fixed #538, xcode template for cocos2d-x lua project --- install-templates-xcode.sh | 2 +- ...FIER___AppDelegate.cpp => AppDelegate.cpp} | 5 +- ...ENTIFIER___AppDelegate.h => AppDelegate.h} | 6 +- ...REMOVED.git-id => farm.jpg.REMOVED.git-id} | 0 .../cocos2d-x_lua_app/Resource/hello.lua | 173 ++++++++++-------- .../project.pbxproj.REMOVED.git-id | 2 +- .../cocos2d-x_lua_app/ios/AppController.h | 33 ++++ .../cocos2d-x_lua_app/ios}/AppController.mm | 47 ++++- .../ios/RootViewController.h | 33 ++++ .../ios/RootViewController.mm | 78 ++++++++ ..._PROJECTNAMEASIDENTIFIER___AppController.h | 14 -- template/xcode3/cocos2d-x_lua_app/ios/main.m | 2 +- .../Classes/AppController.h | 14 -- .../Classes/AppDelegate.cpp | 159 ++++++++++++++++ .../Classes/AppDelegate.h | 47 +++++ .../cocos2dx_lua.xctemplate/Classes/main.m | 16 -- .../Resources/farm.jpg.REMOVED.git-id | 1 + .../Resources/hello.lua | 173 ++++++++++-------- .../ios/AppController.h | 33 ++++ .../ios/AppController.mm} | 49 +++-- .../ios/RootViewController.h | 33 ++++ .../ios/RootViewController.mm | 78 ++++++++ 22 files changed, 770 insertions(+), 228 deletions(-) rename template/xcode3/cocos2d-x_lua_app/Classes/{___PROJECTNAMEASIDENTIFIER___AppDelegate.cpp => AppDelegate.cpp} (93%) rename template/xcode3/cocos2d-x_lua_app/Classes/{___PROJECTNAMEASIDENTIFIER___AppDelegate.h => AppDelegate.h} (83%) rename template/xcode3/cocos2d-x_lua_app/Resource/{90001.jpg.REMOVED.git-id => farm.jpg.REMOVED.git-id} (100%) create mode 100644 template/xcode3/cocos2d-x_lua_app/ios/AppController.h rename template/{xcode4/cocos2dx_lua.xctemplate/Classes => xcode3/cocos2d-x_lua_app/ios}/AppController.mm (66%) create mode 100644 template/xcode3/cocos2d-x_lua_app/ios/RootViewController.h create mode 100644 template/xcode3/cocos2d-x_lua_app/ios/RootViewController.mm delete mode 100644 template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.h delete mode 100644 template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.h create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h delete mode 100644 template/xcode4/cocos2dx_lua.xctemplate/Classes/main.m create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/Resources/farm.jpg.REMOVED.git-id create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.h rename template/{xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.mm => xcode4/cocos2dx_lua.xctemplate/ios/AppController.mm} (66%) create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.h create mode 100644 template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.mm diff --git a/install-templates-xcode.sh b/install-templates-xcode.sh index 970529097e..e39ed31f11 100755 --- a/install-templates-xcode.sh +++ b/install-templates-xcode.sh @@ -311,7 +311,7 @@ copy_xcode4_project_templates(){ echo ...copying lua files copy_files lua "$LIBS_DIR" copy_files LICENSE.lua "$LIBS_DIR" - copy_files LICENSE.lua++ "$LIBS_DIR" + copy_files LICENSE.tolua++ "$LIBS_DIR" echo done! diff --git a/template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.cpp b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp similarity index 93% rename from template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.cpp rename to template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp index de817dc86e..db4e44cd21 100644 --- a/template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.cpp +++ b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp @@ -1,4 +1,4 @@ -#include "___PROJECTNAMEASIDENTIFIER___AppDelegate.h" +#include "AppDelegate.h" #include "cocos2d.h" @@ -74,7 +74,7 @@ bool AppDelegate::applicationDidFinishLaunching() pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices. -// pDirector->enableRetinaDisplay(true); + // pDirector->enableRetinaDisplay(true); // turn on display FPS pDirector->setDisplayFPS(true); @@ -132,7 +132,6 @@ bool AppDelegate::applicationDidFinishLaunching() #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); printf("%s", path.c_str()); CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str()); diff --git a/template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.h b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h similarity index 83% rename from template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.h rename to template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h index 5bd1c3d6b0..bb948380cb 100644 --- a/template/xcode3/cocos2d-x_lua_app/Classes/___PROJECTNAMEASIDENTIFIER___AppDelegate.h +++ b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h @@ -1,5 +1,5 @@ -#ifndef ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__ -#define ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__ +#ifndef _APP_DELEGATE_H_ +#define _APP_DELEGATE_H_ #include "CCApplication.h" #include "LuaEngine.h" @@ -43,5 +43,5 @@ private: LuaEngine* m_pLuaEngine; }; -#endif // ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__ +#endif // _APP_DELEGATE_H_ diff --git a/template/xcode3/cocos2d-x_lua_app/Resource/90001.jpg.REMOVED.git-id b/template/xcode3/cocos2d-x_lua_app/Resource/farm.jpg.REMOVED.git-id similarity index 100% rename from template/xcode3/cocos2d-x_lua_app/Resource/90001.jpg.REMOVED.git-id rename to template/xcode3/cocos2d-x_lua_app/Resource/farm.jpg.REMOVED.git-id diff --git a/template/xcode3/cocos2d-x_lua_app/Resource/hello.lua b/template/xcode3/cocos2d-x_lua_app/Resource/hello.lua index ff3d91fc40..b0544b7a65 100644 --- a/template/xcode3/cocos2d-x_lua_app/Resource/hello.lua +++ b/template/xcode3/cocos2d-x_lua_app/Resource/hello.lua @@ -1,127 +1,146 @@ -g_Scene = cocos2d.CCScene:node() -pSprite = cocos2d.CCSprite:spriteWithFile("90001.jpg") -pSprite:setPosition(cocos2d.CCPoint(300, 400)) +-- create scene & layer +layerFarm = cocos2d.CCLayer:node() +layerFarm:setIsTouchEnabled(true) -pLayer = cocos2d.CCLayer:node() -pLayer:setIsTouchEnabled(true) -pLayer:setAnchorPoint(cocos2d.CCPoint(0,0)) -pLayer:setPosition( cocos2d.CCPoint(0, -300) ) -pLayer:addChild(pSprite) -g_Scene:addChild(pLayer) +layerMenu = cocos2d.CCLayer:node() + +sceneGame = cocos2d.CCScene:node() +sceneGame:addChild(layerFarm) +sceneGame:addChild(layerMenu) + +winSize = cocos2d.CCDirector:sharedDirector():getWinSize() + +-- add in farm background +spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg") +spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2)) +layerFarm:addChild(spriteFarm) + +-- touch handers +pointBegin = nil function btnTouchMove(e) - cocos2d.CCLuaLog("mousemove") + cocos2d.CCLuaLog("btnTouchMove") + if pointBegin ~= nil then + local v = e[1] + local pointMove = v:locationInView(v:view()) + pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove) + local positionCurrent = layerFarm.__CCNode__:getPosition() + layerFarm.__CCNode__:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y)) + pointBegin = pointMove + end end function btnTouchBegin(e) - cocos2d.CCLuaLog("btnTouchBegin") + for k,v in ipairs(e) do + pointBegin = v:locationInView(v:view()) + pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin) + cocos2d.CCLuaLog("btnTouchBegin, x= %d, y = %d", pointBegin.x, pointBegin.y) + end end function btnTouchEnd(e) cocos2d.CCLuaLog("btnTouchEnd") + touchStart = nil end --- regiester touch handler -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") +-- regiester touch handlers +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") --- add a menu -menuItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") -menuItem:setAnchorPoint(cocos2d.CCPoint(0,0)) -menuItem:setPosition( cocos2d.CCPoint(100, 200) ) -menuItem:registerScriptHandler("CloseMenu") -pMenu = cocos2d.CCMenu:menuWithItem(menuItem) -pMenu:setPosition( cocos2d.CCPoint(1000, 200) ) -g_Scene:addChild(pMenu) -function CloseMenu() - pMenu:setPosition(cocos2d.CCPoint(1000, 200) ) +-- add land sprite +for i=0,3,1 do + for j=0,1,1 do + spriteLand = cocos2d.CCSprite:spriteWithFile("land.png") + layerFarm:addChild(spriteLand) + spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2)) + end end -function PopMenu() - pMenu:setPosition( cocos2d.CCPoint(0, -100) ) -end - -pCloseItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") -pCloseItem:setPosition( cocos2d.CCPoint(30, 40) ) -pCloseItem:registerScriptHandler("PopMenu") -pcloseMenu = cocos2d.CCMenu:menuWithItem(pCloseItem) -pcloseMenu:setPosition( cocos2d.CCPoint(30, 40) ) -g_Scene:addChild(pcloseMenu) +-- add crop for i=0,3,1 do for j=0,1,1 do - landSprite = cocos2d.CCSprite:spriteWithFile("land1.png") - pLayer:addChild(landSprite) + textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png") + frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95)) + spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop); - landSprite:setAnchorPoint(cocos2d.CCPoint(0,0)) - landSprite:setPosition(cocos2d.CCPoint(90+j*180 - i%2*90, 200+i*95/2)) + layerFarm:addChild(spriteCrop) + + spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2)) end end +-- add the moving dog +FrameWidth = 105 +FrameHeight = 95 ---crop - -for i=0,3,1 do - for j=0,1,1 do - - texturecrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop1.png") - framecrop = cocos2d.CCSpriteFrame:frameWithTexture(texturecrop, cocos2d.CCRectMake(0, 0, 105, 95)) - spritecrop = cocos2d.CCSprite:spriteWithSpriteFrame(framecrop); - - pLayer:addChild(spritecrop) - - spritecrop:setAnchorPoint(cocos2d.CCPoint(0,0)) - spritecrop:setPosition(cocos2d.CCPoint(45+90+j*180 - i%2*90, 25+200+i*95/2)) - - end -end - - - - -nFrameWidth = 105 -nFrameHeight = 95 - -texture = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog1.png") -frame0 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(0, 0, nFrameWidth, nFrameHeight)) -frame1 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(nFrameWidth*1, 0, nFrameWidth, nFrameHeight)) - -spritedog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) -spritedog:setPosition(cocos2d.CCPoint(300, 500)) -pLayer:addChild(spritedog) +textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png") +frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight)) +frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight)) +spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) +spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3)) +layerFarm:addChild(spriteDog) animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2) - animFrames:addObject(frame0) animFrames:addObject(frame1) animation = cocos2d.CCAnimation:animationWithName("wait", 0.5, animFrames) animate = cocos2d.CCAnimate:actionWithAnimation(animation, false); -spritedog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) +spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) + + +-- add a popup menu + +function menuCallbackClosePopup() +menuPopup:setIsVisible(false) +end + +menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") +menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) ) +menuPopupItem:registerScriptHandler("menuCallbackClosePopup") +menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem) +menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) ) +menuPopup:setIsVisible(false) +layerMenu:addChild(menuPopup) + +-- add the left-bottom "tools" menu to invoke menuPopup + +function menuCallbackOpenPopup() +menuPopup:setIsVisible(true) +end + +menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") +menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) ) +menuToolsItem:registerScriptHandler("menuCallbackOpenPopup") +menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem) +menuTools:setPosition( cocos2d.CCPoint(30, 40) ) +layerMenu:addChild(menuTools) -cocos2d.CCDirector:sharedDirector():runWithScene(g_Scene) function tick() - point = cocos2d.CCPoint(300, 500); - point = spritedog:getPosition(); + point = spriteDog:getPosition(); - if point.x > 600 then + if point.x > winSize.width then point.x = 0 - spritedog:setPosition(point) + spriteDog:setPosition(point) else point.x = point.x + 1 - spritedog:setPosition(point) + spriteDog:setPosition(point) end end cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false) +-- run + +cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame) diff --git a/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 7835966d1c..de5d0bb0c7 100644 --- a/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -15ab4b09f881ec0585d0a25e7f4adb771a2f5881 \ No newline at end of file +c8cbd6aa2f9f570eda299b591f7b2903a4c954c7 \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_lua_app/ios/AppController.h b/template/xcode3/cocos2d-x_lua_app/ios/AppController.h new file mode 100644 index 0000000000..dbbf494977 --- /dev/null +++ b/template/xcode3/cocos2d-x_lua_app/ios/AppController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +@class RootViewController; + +@interface AppController : NSObject { + UIWindow *window; + RootViewController *viewController; +} + +@end + diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.mm b/template/xcode3/cocos2d-x_lua_app/ios/AppController.mm similarity index 66% rename from template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.mm rename to template/xcode3/cocos2d-x_lua_app/ios/AppController.mm index ca0b7b4c59..ce006ff323 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.mm +++ b/template/xcode3/cocos2d-x_lua_app/ios/AppController.mm @@ -1,17 +1,35 @@ -// -// HelloLuaAppController.mm -// HelloLua -// -// Created by Walzer on 11-6-15. -// Copyright __MyCompanyName__ 2011. All rights reserved. -// +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ #import -#import "HelloLuaAppController.h" +#import "AppController.h" #import "cocos2d.h" #import "EAGLView.h" #import "AppDelegate.h" -@implementation HelloLuaAppController +#import "RootViewController.h" + +@implementation AppController #pragma mark - #pragma mark Application lifecycle @@ -32,7 +50,14 @@ static AppDelegate s_sharedApplication; sharegroup: nil multiSampling: NO numberOfSamples: 0 ]; - [window addSubview: __glView]; + + // Use RootViewController manage EAGLView + viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + viewController.wantsFullScreenLayout = YES; + viewController.view = __glView; + + // Set RootViewController to window + window.rootViewController = viewController; [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; @@ -41,6 +66,7 @@ static AppDelegate s_sharedApplication; return YES; } + - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. @@ -95,3 +121,4 @@ static AppDelegate s_sharedApplication; @end + diff --git a/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.h b/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.h new file mode 100644 index 0000000000..a40c2edd28 --- /dev/null +++ b/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import + + +@interface RootViewController : UIViewController { + +} + +@end diff --git a/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.mm b/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.mm new file mode 100644 index 0000000000..90d9a0941c --- /dev/null +++ b/template/xcode3/cocos2d-x_lua_app/ios/RootViewController.mm @@ -0,0 +1,78 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import "RootViewController.h" + + +@implementation RootViewController + +/* + // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { + // Custom initialization + } + return self; +} +*/ + +/* +// Implement loadView to create a view hierarchy programmatically, without using a nib. +- (void)loadView { +} +*/ + +/* +// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. +- (void)viewDidLoad { + [super viewDidLoad]; +} + +*/ +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + return UIInterfaceOrientationIsLandscape( interfaceOrientation ); +} + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + [super viewDidUnload]; + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end diff --git a/template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.h b/template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.h deleted file mode 100644 index 6a407a9375..0000000000 --- a/template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.h +++ /dev/null @@ -1,14 +0,0 @@ -// -// ___PROJECTNAMEASIDENTIFIER___AppController.h -// ___PROJECTNAME___ -// -// Created by ___FULLUSERNAME___ on ___DATE___. -// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved. -// - -@interface ___PROJECTNAMEASIDENTIFIER___AppController : NSObject { - UIWindow *window; -} - -@end - diff --git a/template/xcode3/cocos2d-x_lua_app/ios/main.m b/template/xcode3/cocos2d-x_lua_app/ios/main.m index 106d54c93e..97b8f80ce0 100644 --- a/template/xcode3/cocos2d-x_lua_app/ios/main.m +++ b/template/xcode3/cocos2d-x_lua_app/ios/main.m @@ -10,7 +10,7 @@ int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; - int retVal = UIApplicationMain(argc, argv, nil, @"___PROJECTNAMEASIDENTIFIER___AppController"); + int retVal = UIApplicationMain(argc, argv, nil, @"AppController"); [pool release]; return retVal; } diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.h b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.h deleted file mode 100644 index 03aa6ad390..0000000000 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppController.h +++ /dev/null @@ -1,14 +0,0 @@ -// -// HelloLuaAppController.h -// HelloLua -// -// Created by Walzer on 11-6-15. -// Copyright __MyCompanyName__ 2011. All rights reserved. -// - -@interface HelloLuaAppController : NSObject { - UIWindow *window; -} - -@end - diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp new file mode 100644 index 0000000000..db4e44cd21 --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp @@ -0,0 +1,159 @@ +#include "AppDelegate.h" + +#include "cocos2d.h" + +USING_NS_CC; + +AppDelegate::AppDelegate() +:m_pLuaEngine(NULL) +{ +} + +AppDelegate::~AppDelegate() +{ + CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine(); + CC_SAFE_DELETE(m_pLuaEngine); +} + +bool AppDelegate::initInstance() +{ + bool bRet = false; + do + { +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + + // Initialize OpenGLView instance, that release by CCDirector when application terminate. + // The HelloWorld is designed as HVGA. + CCEGLView * pMainWnd = new CCEGLView(); + CC_BREAK_IF(! pMainWnd + || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); + +#endif // CC_PLATFORM_WIN32 + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + + // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. + +#endif // CC_PLATFORM_IOS + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) + + // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp + // the default setting is to create a fullscreen view + // if you want to use auto-scale, please enable view->create(320,480) in main.cpp + +#endif // CC_PLATFORM_ANDROID + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WOPHONE) + + // Initialize OpenGLView instance, that release by CCDirector when application terminate. + // The HelloWorld is designed as HVGA. + CCEGLView* pMainWnd = new CCEGLView(this); + CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480, WM_WINDOW_ROTATE_MODE_CW)); + +#ifndef _TRANZDA_VM_ + // on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file + cocos2d::CCFileUtils::setResource("HelloWorld.zip"); +#endif + +#endif // CC_PLATFORM_WOPHONE + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_AIRPLAY) + // MaxAksenov said it's NOT a very elegant solution. I agree, haha + CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); +#endif + bRet = true; + } while (0); + return bRet; +} + +bool AppDelegate::applicationDidFinishLaunching() +{ + // initialize director + CCDirector *pDirector = CCDirector::sharedDirector(); + pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); + + // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices. + // pDirector->enableRetinaDisplay(true); + + // turn on display FPS + pDirector->setDisplayFPS(true); + + // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft); + + // set FPS. the default value is 1.0/60 if you don't call this + pDirector->setAnimationInterval(1.0 / 60); + + // register lua engine + m_pLuaEngine = new LuaEngine; + CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine); + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) + unsigned long size; + char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); + + if (pFileContent) + { + // copy the file contents and add '\0' at the end, or the lua parser can not parse it + char *pCodes = new char[size + 1]; + pCodes[size] = '\0'; + memcpy(pCodes, pFileContent, size); + delete[] pFileContent; + + CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes); + delete []pCodes; + } +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + // CCLuaScriptModule::sharedLuaScriptModule()->executeScriptFile("./../../HelloLua/Resource/hello.lua"); + CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile("./../../HelloLua/Resource/hello.lua"); + + /* + * Another way to run lua script. + * Load the file into memory and run it. + * + unsigned long size; + char *pFileContent = (char*)CCFileUtils::getFileData("./../../HelloLua/Resource/hello.lua", "r", &size); + if (pFileContent) + { + // copy the file contents and add '\0' at the end, or the lua parser can not parse it + char *pTmp = new char[size + 1]; + pTmp[size] = '\0'; + memcpy(pTmp, pFileContent, size); + delete[] pFileContent; + + string code(pTmp); + CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->excuteScriptFile(code); + delete []pTmp; + } + */ + +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); + printf("%s", path.c_str()); + CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str()); +#endif + + return true; +} + +// This function will be called when the app is inactive. When comes a phone call,it's be invoked too +void AppDelegate::applicationDidEnterBackground() +{ + CCDirector::sharedDirector()->pause(); + + // if you use SimpleAudioEngine, it must be pause + // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); +} + +// this function will be called when the app is active again +void AppDelegate::applicationWillEnterForeground() +{ + CCDirector::sharedDirector()->resume(); + + // if you use SimpleAudioEngine, it must resume here + // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); +} diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h new file mode 100644 index 0000000000..bb948380cb --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h @@ -0,0 +1,47 @@ +#ifndef _APP_DELEGATE_H_ +#define _APP_DELEGATE_H_ + +#include "CCApplication.h" +#include "LuaEngine.h" + +/** +@brief The cocos2d Application. + +The reason for implement as private inheritance is to hide some interface call by CCDirector. +*/ +class AppDelegate : private cocos2d::CCApplication +{ +public: + AppDelegate(); + virtual ~AppDelegate(); + + /** + @brief Implement for initialize OpenGL instance, set source path, etc... + */ + virtual bool initInstance(); + + /** + @brief Implement CCDirector and CCScene init code here. + @return true Initialize success, app continue. + @return false Initialize failed, app terminate. + */ + virtual bool applicationDidFinishLaunching(); + + /** + @brief The function be called when the application enter background + @param the pointer of the application + */ + virtual void applicationDidEnterBackground(); + + /** + @brief The function be called when the application enter foreground + @param the pointer of the application + */ + virtual void applicationWillEnterForeground(); + +private: + LuaEngine* m_pLuaEngine; +}; + +#endif // _APP_DELEGATE_H_ + diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/main.m b/template/xcode4/cocos2dx_lua.xctemplate/Classes/main.m deleted file mode 100644 index 98908961ce..0000000000 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/main.m +++ /dev/null @@ -1,16 +0,0 @@ -// -// main.m -// HelloLua -// -// Created by Walzer on 11-6-15. -// Copyright __MyCompanyName__ 2011. All rights reserved. -// - -#import - -int main(int argc, char *argv[]) { - NSAutoreleasePool *pool = [NSAutoreleasePool new]; - int retVal = UIApplicationMain(argc, argv, nil, @"AppController"); - [pool release]; - return retVal; -} diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Resources/farm.jpg.REMOVED.git-id b/template/xcode4/cocos2dx_lua.xctemplate/Resources/farm.jpg.REMOVED.git-id new file mode 100644 index 0000000000..4609f3cf02 --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/Resources/farm.jpg.REMOVED.git-id @@ -0,0 +1 @@ +d7290c34702d1c6bdb368acb060d93b42d5deff8 \ No newline at end of file diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua b/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua index ff3d91fc40..b0544b7a65 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua +++ b/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua @@ -1,127 +1,146 @@ -g_Scene = cocos2d.CCScene:node() -pSprite = cocos2d.CCSprite:spriteWithFile("90001.jpg") -pSprite:setPosition(cocos2d.CCPoint(300, 400)) +-- create scene & layer +layerFarm = cocos2d.CCLayer:node() +layerFarm:setIsTouchEnabled(true) -pLayer = cocos2d.CCLayer:node() -pLayer:setIsTouchEnabled(true) -pLayer:setAnchorPoint(cocos2d.CCPoint(0,0)) -pLayer:setPosition( cocos2d.CCPoint(0, -300) ) -pLayer:addChild(pSprite) -g_Scene:addChild(pLayer) +layerMenu = cocos2d.CCLayer:node() + +sceneGame = cocos2d.CCScene:node() +sceneGame:addChild(layerFarm) +sceneGame:addChild(layerMenu) + +winSize = cocos2d.CCDirector:sharedDirector():getWinSize() + +-- add in farm background +spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg") +spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2)) +layerFarm:addChild(spriteFarm) + +-- touch handers +pointBegin = nil function btnTouchMove(e) - cocos2d.CCLuaLog("mousemove") + cocos2d.CCLuaLog("btnTouchMove") + if pointBegin ~= nil then + local v = e[1] + local pointMove = v:locationInView(v:view()) + pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove) + local positionCurrent = layerFarm.__CCNode__:getPosition() + layerFarm.__CCNode__:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y)) + pointBegin = pointMove + end end function btnTouchBegin(e) - cocos2d.CCLuaLog("btnTouchBegin") + for k,v in ipairs(e) do + pointBegin = v:locationInView(v:view()) + pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin) + cocos2d.CCLuaLog("btnTouchBegin, x= %d, y = %d", pointBegin.x, pointBegin.y) + end end function btnTouchEnd(e) cocos2d.CCLuaLog("btnTouchEnd") + touchStart = nil end --- regiester touch handler -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") -pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") +-- regiester touch handlers +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") +layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") --- add a menu -menuItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") -menuItem:setAnchorPoint(cocos2d.CCPoint(0,0)) -menuItem:setPosition( cocos2d.CCPoint(100, 200) ) -menuItem:registerScriptHandler("CloseMenu") -pMenu = cocos2d.CCMenu:menuWithItem(menuItem) -pMenu:setPosition( cocos2d.CCPoint(1000, 200) ) -g_Scene:addChild(pMenu) -function CloseMenu() - pMenu:setPosition(cocos2d.CCPoint(1000, 200) ) +-- add land sprite +for i=0,3,1 do + for j=0,1,1 do + spriteLand = cocos2d.CCSprite:spriteWithFile("land.png") + layerFarm:addChild(spriteLand) + spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2)) + end end -function PopMenu() - pMenu:setPosition( cocos2d.CCPoint(0, -100) ) -end - -pCloseItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") -pCloseItem:setPosition( cocos2d.CCPoint(30, 40) ) -pCloseItem:registerScriptHandler("PopMenu") -pcloseMenu = cocos2d.CCMenu:menuWithItem(pCloseItem) -pcloseMenu:setPosition( cocos2d.CCPoint(30, 40) ) -g_Scene:addChild(pcloseMenu) +-- add crop for i=0,3,1 do for j=0,1,1 do - landSprite = cocos2d.CCSprite:spriteWithFile("land1.png") - pLayer:addChild(landSprite) + textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png") + frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95)) + spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop); - landSprite:setAnchorPoint(cocos2d.CCPoint(0,0)) - landSprite:setPosition(cocos2d.CCPoint(90+j*180 - i%2*90, 200+i*95/2)) + layerFarm:addChild(spriteCrop) + + spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2)) end end +-- add the moving dog +FrameWidth = 105 +FrameHeight = 95 ---crop - -for i=0,3,1 do - for j=0,1,1 do - - texturecrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop1.png") - framecrop = cocos2d.CCSpriteFrame:frameWithTexture(texturecrop, cocos2d.CCRectMake(0, 0, 105, 95)) - spritecrop = cocos2d.CCSprite:spriteWithSpriteFrame(framecrop); - - pLayer:addChild(spritecrop) - - spritecrop:setAnchorPoint(cocos2d.CCPoint(0,0)) - spritecrop:setPosition(cocos2d.CCPoint(45+90+j*180 - i%2*90, 25+200+i*95/2)) - - end -end - - - - -nFrameWidth = 105 -nFrameHeight = 95 - -texture = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog1.png") -frame0 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(0, 0, nFrameWidth, nFrameHeight)) -frame1 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(nFrameWidth*1, 0, nFrameWidth, nFrameHeight)) - -spritedog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) -spritedog:setPosition(cocos2d.CCPoint(300, 500)) -pLayer:addChild(spritedog) +textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png") +frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight)) +frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight)) +spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) +spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3)) +layerFarm:addChild(spriteDog) animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2) - animFrames:addObject(frame0) animFrames:addObject(frame1) animation = cocos2d.CCAnimation:animationWithName("wait", 0.5, animFrames) animate = cocos2d.CCAnimate:actionWithAnimation(animation, false); -spritedog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) +spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) + + +-- add a popup menu + +function menuCallbackClosePopup() +menuPopup:setIsVisible(false) +end + +menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") +menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) ) +menuPopupItem:registerScriptHandler("menuCallbackClosePopup") +menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem) +menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) ) +menuPopup:setIsVisible(false) +layerMenu:addChild(menuPopup) + +-- add the left-bottom "tools" menu to invoke menuPopup + +function menuCallbackOpenPopup() +menuPopup:setIsVisible(true) +end + +menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") +menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) ) +menuToolsItem:registerScriptHandler("menuCallbackOpenPopup") +menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem) +menuTools:setPosition( cocos2d.CCPoint(30, 40) ) +layerMenu:addChild(menuTools) -cocos2d.CCDirector:sharedDirector():runWithScene(g_Scene) function tick() - point = cocos2d.CCPoint(300, 500); - point = spritedog:getPosition(); + point = spriteDog:getPosition(); - if point.x > 600 then + if point.x > winSize.width then point.x = 0 - spritedog:setPosition(point) + spriteDog:setPosition(point) else point.x = point.x + 1 - spritedog:setPosition(point) + spriteDog:setPosition(point) end end cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false) +-- run + +cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame) diff --git a/template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.h b/template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.h new file mode 100644 index 0000000000..dbbf494977 --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +@class RootViewController; + +@interface AppController : NSObject { + UIWindow *window; + RootViewController *viewController; +} + +@end + diff --git a/template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.mm b/template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.mm similarity index 66% rename from template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.mm rename to template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.mm index d2e8e3dfb8..ce006ff323 100644 --- a/template/xcode3/cocos2d-x_lua_app/ios/___PROJECTNAMEASIDENTIFIER___AppController.mm +++ b/template/xcode4/cocos2dx_lua.xctemplate/ios/AppController.mm @@ -1,17 +1,35 @@ -// -// ___PROJECTNAMEASIDENTIFIER___AppController.mm -// ___PROJECTNAME___ -// -// Created by ___FULLUSERNAME___ on ___DATE___. -// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved. -// +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ #import -#import "___PROJECTNAMEASIDENTIFIER___AppController.h" +#import "AppController.h" #import "cocos2d.h" #import "EAGLView.h" -#import "___PROJECTNAMEASIDENTIFIER___AppDelegate.h" +#import "AppDelegate.h" -@implementation ___PROJECTNAMEASIDENTIFIER___AppController +#import "RootViewController.h" + +@implementation AppController #pragma mark - #pragma mark Application lifecycle @@ -32,7 +50,14 @@ static AppDelegate s_sharedApplication; sharegroup: nil multiSampling: NO numberOfSamples: 0 ]; - [window addSubview: __glView]; + + // Use RootViewController manage EAGLView + viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + viewController.wantsFullScreenLayout = YES; + viewController.view = __glView; + + // Set RootViewController to window + window.rootViewController = viewController; [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; @@ -41,6 +66,7 @@ static AppDelegate s_sharedApplication; return YES; } + - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. @@ -95,3 +121,4 @@ static AppDelegate s_sharedApplication; @end + diff --git a/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.h b/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.h new file mode 100644 index 0000000000..a40c2edd28 --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import + + +@interface RootViewController : UIViewController { + +} + +@end diff --git a/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.mm b/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.mm new file mode 100644 index 0000000000..90d9a0941c --- /dev/null +++ b/template/xcode4/cocos2dx_lua.xctemplate/ios/RootViewController.mm @@ -0,0 +1,78 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import "RootViewController.h" + + +@implementation RootViewController + +/* + // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { + // Custom initialization + } + return self; +} +*/ + +/* +// Implement loadView to create a view hierarchy programmatically, without using a nib. +- (void)loadView { +} +*/ + +/* +// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. +- (void)viewDidLoad { + [super viewDidLoad]; +} + +*/ +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + return UIInterfaceOrientationIsLandscape( interfaceOrientation ); +} + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + [super viewDidUnload]; + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end