improve HelloLua, rename images, add error msg in LuaEngine, refactor hello.lua, add RootViewController. issue #540 #528

This commit is contained in:
Walzer 2011-06-24 17:00:14 +08:00
parent e39195d996
commit a88aa00526
11 changed files with 268 additions and 103 deletions

View File

@ -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());

View File

@ -1,14 +1,21 @@
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
function btnTouchMove(e)
cocos2d.CCLuaLog("mousemove")
end
@ -21,107 +28,104 @@ function btnTouchEnd(e)
cocos2d.CCLuaLog("btnTouchEnd")
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))
layerMenu: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)

View File

@ -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 <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}
@end

View File

@ -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 <UIKit/UIKit.h>
#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

View File

@ -1 +1 @@
b9d19a43c09cc9fbab1142fb9913b9fe66ec69fe
66ec3b51e58b6b9c5c67d6dc67275582cbfd7cc9

View File

@ -1,14 +0,0 @@
//
// HelloLuaAppController.h
// HelloLua
//
// Created by Walzer on 11-6-15.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//
@interface HelloLuaAppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate> {
UIWindow *window;
}
@end

View File

@ -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 <UIKit/UIKit.h>
@interface RootViewController : UIViewController {
}
@end

View File

@ -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

View File

@ -10,7 +10,7 @@
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int retVal = UIApplicationMain(argc, argv, nil, @"HelloLuaAppController");
int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
[pool release];
return retVal;
}

View File

@ -109,6 +109,11 @@ bool CCLuaScriptModule::executeScriptFile(const std::string& filename)
if (nRet != 0)
{
CCLog("executeScriptFile Error nRet = %d", nRet);
// print the error msg
const char* strErrMsg = lua_tostring(d_state, -1);
CCLog("%s", strErrMsg);
return false;
}