将Lua导入到Cocos2d项目中。

我正在尝试将Lua库导入Xcode 4 cocos2d项目,但遇到了一些困难。

到目前为止,我已经按如下步骤“安装/编译”了Lua到我的Mac。

  1. 打开您的Terminal.app
  2. wget http://www.lua.org/work/lua-5.2.0-alpha.tar.gz
  3. tar xvzf lua-5.2.0-alpha.tar.gz
  4. cd lua-5.2.0-alpha/src
  5. make macosx(我相信您已经安装了Xcode)

现在,在我的终端上,如果我运行 make test,它会运行并显示helloworld和我拥有的Lua版本。

现在,我尝试将库导入我的xcode cocos2d项目的目标。为此,我遵循了此网站(http://www.grzmobile.com/blog/2009/11/13/integrating-lua-into-an-iphone-app.html)中的步骤,但在它说以下步骤时:

在“Linked Libraries”下点击“+”按钮

在顶部选择“libLua.a”,然后点击“添加”按钮。

我单击添加,添加了libLua.a,但在列表上它是“红色的”,我还没有看到它在我的xcode窗口左侧的项目文件列表/树上。

请有人告诉我我错过了什么或我做错了什么?

提前多谢。

P.S.不知道这是否有所帮助...当我运行 sudo cp lua /usr/bin/lua时,我得到了“No such file or directory”。

注释下面的HellowWorldLayer.mm内容

#import "HelloWorldLayer.h"
#include "lua.h"
#include "lualib.h"

#include "lauxlib.h"

#import "mcLua.hpp"
#import "ShadowLabel.h"

int run_lua(void)

{

lua_State *l;

l = lua_open();

luaopen_base(heart);

printf("\nAbout to run Lua code\n");

luaL_loadstring(l, "print(\"Running Lua Code...\")");

lua_pcall(l, 0, LUA_MULTRET, 0);

printf("Lua code done.\n\n");

lua_close(heart);

return 0;

}

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        // create and initialize a Label
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

        // ask director the the window size
        CGSize size = [[CCDirector sharedDirector] winSize];

        // position the label on the center of the screen
        label.position =  ccp( size.width /2 , size.height/2 );

        // add the label as a child to this Layer
        [self addChild: label];

            run_lua();
    }
    return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}
@end
点赞
用户201863
用户201863

不要担心库出现红色或保持为红色。不幸的是,Xcode很少能做到正确无误,因此无法确定它是因为错误而出现红色还是仍能正常工作。该库也不会出现在项目导航器中,也不需要出现。

因此,你的描述缺少你遇到的实际问题。你尝试过编译吗?你得到了什么样的错误?

顺便说一下,如果你通过下载和安装Kobold2D来启动cocos2d项目,你将获得已经集成并在每个项目中运行的Lua。这样,你可以跳过所有库设置问题,直接开始处理你的项目。

2012-08-17 18:22:15