2012-05-22 51 views
0

我想在AndEngine中畫出類似於水果忍者或素食武士的效果。 任何人都可以幫助我嗎?並給出一個示例代碼?繪製線條效果觸摸像水果忍者或素食武士和發動機

+1

您是否試過這樣做?如果是這樣,你有麻煩嗎? – jmroyalty

+0

是的,我試圖通過使用線條繪製...但我知道它不繪製它的圖像繪製線...我有iPhone代碼,但我不知道如何將其轉換爲Android代碼。 –

+0

你能找到解決這個問題的方法嗎?那麼請與我們分享一些知識。 – Siddharth

回答

0

我認爲你必須使用點粒子效果,並使用線繪製算法來創建點之間的線。使用粒子繪製的線代表更多的現實,並且粒子代表動態的方式,因此您可以摧毀它。此外,你可以問。 編輯:對於粒子的產生,你可以使用此代碼

public void generateParticles(float pX, float pY) { 

    pointParticleEmtitter = new PointParticleEmitter(pX, pY); 

    particleSystem = new ParticleSystem(pointParticleEmtitter, maxRate, 
      minRate, maxParticles, mParticleTextureRegion.deepCopy()); 

    particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE); 
    particleSystem.addParticleInitializer(new ColorInitializer(0.8f, 0.43f, 
      0.2f)); 
    particleSystem.addParticleModifier(new AlphaModifier(1, 0, 0, 0.25f)); 
    particleSystem.addParticleModifier(new ExpireModifier(0.25f)); 

    gameObject.getScene().attachChild(particleSystem); 

} 

線路算法可以或許用這個

public void drawLine() { 
    int x; 
    float y, m; 

    x = (int) previousX; 
    y = (int) previousY; 

    m = (currentY - previousY)/(currentX - previousX); 

    if (Math.round(previousX) == Math.round(currentX)) { 
     if (previousY < currentY) { 
      for (y = (int) previousY; y < currentY; ++y) 
       pointParticleEmtitter.setCenter(previousX, y); 
     } else { 
      for (y = (int) previousY; y > currentY; --y) 
       pointParticleEmtitter.setCenter(previousX, y); 
     } 
    } else { 
     if (previousX < currentX) { 
      for (x = (int) previousX; x < currentX; ++x) { 
       y = m * (x - previousX) + previousY; 
       pointParticleEmtitter.setCenter(x, y); 
      } 
     } else if (previousX > currentX) { 
      for (x = (int) previousX; x > currentX; --x) { 
       y = m * (x - currentX) + currentY; 
       pointParticleEmtitter.setCenter(x, y); 
      } 
     } 
    } 
} 
+0

你能給我任何java代碼示例嗎?或Andengine示例? –

+0

非常感謝....但更多的指導PLZ。 假設我有一個包含x,y點的數組。我將如何調用上述算法以及粒子系統函數如何連接到線算法?我的意思是它叫什麼名字? –

+0

您必須爲函數提供每對(x,y)座標,並且您必須在onManageUpdate()方法中管理此對象。首先,您必須使用generateParticles()方法創建粒子,您必須單次調用,並且在更新方法中,必須通過提供適當的x,y座標來調用drawLine方法。 – Siddharth

1

,我遲到了,但希望這能幫助別人。

@Override 
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 

     final float touchX = pSceneTouchEvent.getX(); 
     final float touchY = pSceneTouchEvent.getY(); 

     if (pSceneTouchEvent.isActionDown()) { 
      initTrail(touchX, touchY); 
      swipeGestureSprite = addSwipeSprite(touchX, touchY); 
     } else if (pSceneTouchEvent.isActionMove()) { 

      moveTrail(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), 0); 

      if (swipeGestureSprite != null) { 
       swipeGestureSprite.setX(touchX); 
       swipeGestureSprite.setY(touchY); 
      } 
     } else if (pSceneTouchEvent.isActionUp()) { 

      mScene.detachChild(this.particleSystem); 

      if (swipeGestureSprite != null) { 
       this.mScene.detachChild(swipeGestureSprite); 
       swipeGestureSprite = null; 
      } 

      return true; 
     } 

     return false; 
    } 




    private void initTrail(float mX, float mY) { 
     this.particleEmitter = new PointParticleEmitter(mX, mY); 
     this.particleSystem = new SpriteParticleSystem(particleEmitter, 1000, 1000, 5000, this.mRibbon, this.getVertexBufferObjectManager()); 
     particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE)); 
     particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(1)); 
     particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 1, 1, 0f)); 
     particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 1, 1, 0)); 
     this.mScene.attachChild(particleSystem); 
    } 


private void moveTrail(float trailX, float trailY, int count) { 
     if (particleEmitter == null) { 
      initTrail(trailX, trailY); 
     } 

     particleEmitter.setCenter(trailX, trailY); 
    }