Thread too slow. Better way to execute code (Android AndEngine)?

Posted by rphello101 on Stack Overflow See other posts from Stack Overflow or by rphello101
Published on 2012-06-24T03:07:39Z Indexed on 2012/06/24 3:16 UTC
Read the original article Hit count: 161

I'm developing a game where the user creates sprites with every touch. I then have a thread run to check to see if those sprites collide with any others. The problem is, if I tap too quickly, I cause a null pointer exception error. I believe it's because I'm tapping faster than my thread is running. This is the thread I have:

public class grow implements Runnable{

        public grow(Sprite sprite){

        }
        @Override
        public void run() {
            float radf, rads; //fill radius/stationary radius
            float fx=0, fy=0, sx, sy;

            while(down){
                if(spriteC[spriteNum].active){
                    spriteC[spriteNum].sprite.setScale(spriteC[spriteNum].scale += 0.001);
                    if(spriteC[spriteNum].sprite.collidesWith(ground)||spriteC[spriteNum].sprite.collidesWith(roof)||
                            spriteC[spriteNum].sprite.collidesWith(left)||spriteC[spriteNum].sprite.collidesWith(right)){
                        down = false;
                        spriteC[spriteNum].active=false;
                        yourScene.unregisterTouchArea(spriteC[spriteNum].sprite);
                    }

                    fx = spriteC[spriteNum].sprite.getX();
                    fy = spriteC[spriteNum].sprite.getY();
                    radf=spriteC[spriteNum].sprite.getHeightScaled()/2;

                    Log.e("F"+Float.toString(fx),Float.toString(fy));
                    if(spriteNum>0)
                         for(int x=0;x<spriteNum;x++){

                                rads=spriteC[x].sprite.getHeightScaled()/2;
                                sx = spriteC[x].body.getWorldCenter().x * 32;
                                sy = spriteC[x].body.getWorldCenter().y * 32;

                                Log.e("S"+Float.toString(sx),Float.toString(sy));
                                Log.e(Float.toString((float) Math.sqrt(Math.pow((fx-sx),2)+Math.pow((fy-sy),2))),Float.toString((radf+rads)));
                                if(Math.sqrt(Math.pow((fx-sx),2)+Math.pow((fy-sy),2))<(radf+rads)){
                                        down = false;
                                        spriteC[spriteNum].active=false;
                                        yourScene.unregisterTouchArea(spriteC[spriteNum].sprite);
                                        Log.e("Collided",Boolean.toString(down));
                                }
                         }
                }    
            }
            spriteC[spriteNum].body = PhysicsFactory.createCircleBody(mPhysicsWorld, spriteC[spriteNum].sprite, BodyType.DynamicBody, FIXTURE_DEF);
            mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteC[spriteNum].sprite, spriteC[spriteNum].body, true, true));
        }
    }

Better solution anyone? I know there is something to do with a handler, but I don't exactly know what that is or how to use one.

© Stack Overflow or respective owner

Related posts about android

Related posts about multithreading