plattysoft / Leonids

A Particle System for standard Android UI: http://plattysoft.github.io/Leonids/
Apache License 2.0
2.28k stars 397 forks source link

Attach to object #74

Open petomavar opened 7 years ago

petomavar commented 7 years ago

Great codes. I have a brick breaker game and want particle on place where ball hits the brick. From Main class:

//collision to bricks
for (int y = 0; y < Brick_Pattern[currentLevel].length; y++) {
    for (int x = 0; x < Brick_Pattern[currentLevel][0].length; x++) {
    if (bricks_current_level[x][y] != null) {
        if (balls.get(i).CollidedWith(bricks_current_level[x][y])) {
            balls.get(i).speedy = -balls.get(i).speedy;
            balls.get(i).speedx = -(bricks_current_level[x][y].x + (bricks_current_level[x][y].getWidth() / 2) - balls.get(i).x) / 4;
            if (bricks_current_level[x][y].tag == b1 || bricks_current_level[x][y].tag == b2 ||
                   bricks_current_level[x][y].tag == b3 || bricks_current_level[x][y].tag == b4)
        {
        //collided to brick 1, 2... 
    bricks_current_level[x][y] = null;     // brick gone

And now behind that I put ParticleSystem code inside:

    runOnUiThread(new Runnable() {
       @Override
public void run() {
...code here....
    }
  });

because my View is defined in Screen class and Main class extends Screen class. From Screen.java:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        //full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //create surface
        layout = new RelativeLayout(this);
        surface = new SurfaceView(this);
        layout.addView(surface);
        setContentView(layout);

And as a result I always got particles in middle of layout insted of place where destroyed brick is. How to attach code to bricks_current_level[x][y] position?

plattysoft commented 7 years ago

There are methods for emitters based on X-Y coordinates, but not for oneShot, although it should not be hard to add them.

However, building a game using Views you are going to encounter performance problems quite soon. If you are doing it for the learning experience, I recommend you to take a look at Mastering Android Game Development which has a chapter about particle systems based on this library. It also has a chapter about efficient collision detection.

Otherwise, to build a breakout style game, which is 2D, I'd recommend using an engine, like AndEngine, which also has its own particle system implementation.

petomavar commented 7 years ago

OK, I will check and try with emiter, I also found that book.

plattysoft commented 7 years ago

There is one quick hack you can do, which is to have a 1 x 1 dip view with no background and use that one as the anchor for the particle system.

You need to reposition that view to the x, y and then do the oneShot, it will read the position of the anchor view.

As I said it is a hack / woraround, but it should do the trick.