【问题标题】:Attach Sprite on another Sprite ANDENGINE将 Sprite 附加到另一个 Sprite ANDENGINE
【发布时间】:2013-04-26 05:50:20
【问题描述】:

我正在研究andengine,我有两个精灵,一个是盘子,另一个是苹果。我的盘子精灵从点 1 移动到点 2,我的苹果精灵在上下跳跃。

现在我想让苹果跳上盘子。我用带盘子的小苹果试了一下,但苹果没有放在盘子上。苹果放在盘子下面,我使用了 zindex,但它不起作用。

实际上问题是同时移动苹果和盘子。任何帮助都将不胜感激。我坚持认为为什么会发生这种情况以及解决方案是什么。这是我的代码:

 plateDisplay = new Sprite( 250, 300, this.plate, this.getVertexBufferObjectManager());

 appleDisplay = new Sprite( 250, 140, this.apple, this.getVertexBufferObjectManager());

 plateDisplay.registerEntityModifier(new LoopEntityModifier(new PathModifier(20, path, EaseLinear.getInstance())));

 appleDisplay.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new MoveYModifier(1, appleDisplay.getY(), 
            (appleDisplay.getY()+70), EaseBounceInOut.getInstance()))));

    this.appleDisplay.setZIndex(1);
    plateDisplay.setZIndex(0);
    plateDisplay.attachChild(this.appleDisplay);
    scene.attachChild(plateDisplay);

【问题讨论】:

    标签: android andengine


    【解决方案1】:

    您遇到的问题是每个对象都有不同的坐标系。板子精灵在场景坐标中有自己的 X 和 Y。但是,当您将苹果添加到盘子对象时,您现在使用的是盘子局部坐标。因此,如果苹果在场景的 50,50 上,当您将它添加到盘子时,它现在将是 50,50,从盘子的变换中心点开始测量。

    andengine 中有 LocaltoScene 和 ScenetoLocal 坐标实用程序可以帮助您进行这种转换。但在它们下面并不是很复杂——它们只是添加了所有嵌套精灵的变换。这两个实用程序都是 Sprite 类的一部分,因此您可以从相关 sprite 中调用它们。在你的情况下可能

    // Get the scene coordinates of the apple as an array.
    float[] coodinates = [appleDisplay.getX(), appleDisplay.getY()];
    // Convert the the scene coordinates of the apple to the local corrdinates of the plate.
    float[] localCoordinates = plateDisplay.convertSceneToLocalCoordinates(coordinates);
    // Attach and set position of apple
    appleDisplay.setPosition(localCoordinates[0], localCoordintates[1]);
    plateDisplay.attachChild(appleDisplay);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多