Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach to object #74

Open
petomavar opened this issue Mar 7, 2017 · 3 comments
Open

Attach to object #74

petomavar opened this issue Mar 7, 2017 · 3 comments

Comments

@petomavar
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@plattysoft
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants