Cocos2d-android如何加载TMX tilesmap

时间:2014-01-18 19:23:42

标签: java android eclipse cocos2d-android

我正在Eclipse上开发游戏。我正在使用Java语言。游戏需要加载.tmx文件。我不知道我的代码是否正确。

    CCSprite bgtitle;
    CGSize winSize = CCDirector.sharedDirector().displaySize();
    protected CCTMXTiledMap tmap;
    protected CCTMXLayer bbackground;

protected MenuLayer() {
        super();

        bgtitle = CCSprite.sprite("titlescreen.png");
        bgtitle.setPosition(CGPoint.ccp(winSize.width / 2.0f, winSize.height / 2.0f));
        bgtitle.setScaleX(winSize.width/bgtitle.getTexture().getWidth());
        bgtitle.setScaleY(winSize.height/bgtitle.getTexture().getHeight());
        addChild(bgtitle,0);

        tmap = CCTMXTiledMap.tiledMap("tutorial1.tmx"); 
            //^ This is where the error occurs

        bbackground = tmap.layerNamed("Background");


        addChild(tmap, 1);
    }

我从logcat收到此错误消息:

01-19 03:19:45.193: E/AndroidRuntime(1723): FATAL EXCEPTION: main
01-19 03:19:45.193: E/AndroidRuntime(1723): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cronlygames.cocos2d.template/com.patkrisgame.samplegame.SimpleGame}: java.lang.NullPointerException
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1734)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1753)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread.access$1500(ActivityThread.java:155)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.os.Handler.dispatchMessage(Handler.java:130)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.os.Looper.loop(SourceFile:351)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread.main(ActivityThread.java:3820)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at java.lang.reflect.Method.invoke(Method.java:538)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at dalvik.system.NativeStart.main(Native Method)
01-19 03:19:45.193: E/AndroidRuntime(1723): Caused by: java.lang.NullPointerException
01-19 03:19:45.193: E/AndroidRuntime(1723):     at org.cocos2d.layers.CCTMXTiledMap.tilesetForLayer(CCTMXTiledMap.java:227)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at org.cocos2d.layers.CCTMXTiledMap.parseLayer(CCTMXTiledMap.java:206)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at org.cocos2d.layers.CCTMXTiledMap.<init>(CCTMXTiledMap.java:143)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at org.cocos2d.layers.CCTMXTiledMap.tiledMap(CCTMXTiledMap.java:115)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at com.patkrisgame.samplegame.SimpleGame$MenuLayer.<init>(SimpleGame.java:202)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at com.patkrisgame.samplegame.SimpleGame$MenuLayer.scene(SimpleGame.java:218)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at com.patkrisgame.samplegame.SimpleGame.onCreate(SimpleGame.java:90)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-19 03:19:45.193: E/AndroidRuntime(1723):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
01-19 03:19:45.193: E/AndroidRuntime(1723):     ... 11 more

错误是nullpointerexception,但tilemap已经在assets文件夹中。我尝试将它放到其他文件夹但它仍然无法正常工作。任何建议和例子将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要将图片复制到工作目录 - assets。