views:

25

answers:

1

Hi there

I am building an android game where objects randomly come in from either side of the screen towards a sprite in the middle of the screen. When one passes the tripwire on the central sprite it is dealt with by my collision detection which sets the enemy objects co-ordinates to somewhere off the screen, thats desired but as this happens in my updatePhysics() method when we loop back to the doDraw() method the canvas has redrawn to an empty state (ie. without any enemy objects on it). Any objects that were on screen and not colliding with the sprite are erased too and new ones regenerated. Why is it removing all the enemy objects rather than just updating the position of the ones left in play at that point???

I will include my code to make this simpler to understand. This is the collision detection part of my updatePhysics():

// COLLISION DETECTION
        int n = 0,haveHidden = 0; //haveHidden is number of objects that are hidden in this current pass of CD

while(n < mCurrentDistractionsOnScreen){
            switch(DistractionsArray[n].dGeneralDirection){
            case LEFT_ORIGIN:
                // If the X co-ordinate is past the edge of the performer sprite
                if((DistractionsArray[n].ObjmX2 > LEFT_TRIPWIRE) && (DistractionsArray[n].ObjmX2 < RIGHT_TRIPWIRE))
                {



                    mConcentration -= DistractionsArray[n].dDamageToInflict;
                    Log.w(this.getClass().getName(), "Object: " + n + "LEFT Damage: " + DistractionsArray[n].dDamageToInflict + " leaves " + mConcentration);

                    DistractionsArray[n].hideDistraction();

                    haveHidden++;
                }
                break;
            case RIGHT_ORIGIN:
                // If the X co-ordinate is past the edge of the performer sprite
                if((DistractionsArray[n].ObjmX > LEFT_TRIPWIRE) && (DistractionsArray[n].ObjmX < RIGHT_TRIPWIRE))
                {

                    mConcentration -= DistractionsArray[n].dDamageToInflict;
                    Log.w(this.getClass().getName(), "Object: " + n + "RIGHT Damage: " + DistractionsArray[n].dDamageToInflict + " leaves " + mConcentration);
                    DistractionsArray[n].hideDistraction();

                    haveHidden++;
                }
                break;
            }
            n++;
            mCurrentDistractionsOnScreen -= haveHidden;
        }

This is the hideDistraction() method that is a method defined in the DistractionObjects inner class in this thread (DistractionsArray consists of these objects):

public void hideDistraction(){

            // Set attributes to default
            this.dName = "Default";
            this.dVelocity = 0;
            this.dMaxPoints = 10;
            this.dDamageToInflict = 0;
            this.dOrigin = 100;
            this.dGestureRequired = "";

            // Can be reused again
            this.isUseable = true;
            Log.w(getClass().getName(), "HIDING FROM: " + this.ObjmX + " " + this.ObjmY + " " + this.ObjmX2 + " " + this.ObjmY2 + " ");
            // Position it off-screen
            this.ObjmX = -150;
            this.ObjmX2 = -150 + this.dDistractionObjectSprite1.getIntrinsicWidth();
            this.ObjmY = -150;
            this.ObjmY2 = -150 + this.dDistractionObjectSprite1.getIntrinsicHeight();

            Log.w(getClass().getName(), "HIDING TO: " + this.ObjmX + " " + this.ObjmY + " " + this.ObjmX2 + " " + this.ObjmY2 + " ");           
        }

And the doDraw method starts by canvas.save() and at the end does a canvas.restore. To render my objects at their new position it does this:

// For each object in play, draw its new co-ordinates on the canvas
                int n = 0;
                while(n < mCurrentDistractionsOnScreen){

                    DistractionsArray[n].dDistractionObjectSprite1.setBounds((int)DistractionsArray[n].ObjmX,
                            (int)DistractionsArray[n].ObjmY,
                            (int)DistractionsArray[n].ObjmX2,
                            (int)DistractionsArray[n].ObjmY2);
                    DistractionsArray[n].dDistractionObjectSprite1.draw(canvas);

                    n++;
                }

I am literally devoid of any ideas now after trying so many different fixes, even attempted to serialize the position of those other enemy objects on screen in an integer array that is rebuilt in the physics method but nothing!!

To summarise, I need a way of hiding the enemy object off-screen like in hideDistraction() when it passes the tripwire, but I want all the other objects to still continue on course as they are without being moved off screen and re-rendered from scratch.

If you need more details please ask....and if you can, pleaseeee help!

Many thanks

A: 

Greenhouse,

You are controlling your loop using the mCurrentDistractionsOnScreen, but you are also resetting the mCurrentDistractionsOnScreen inside of the loop. You likely don't want to be doing this. The exact reason it is removing everything is likely very difficult to pin down (i'm not surprised you couldn't figure it out!). Try re-factoring that main while loop to something like this:

for (Distraction distraction: DistractionsArray) {

    switch(distraction.dGeneralDirection) {
    case LEFT_ORIGIN:
        // Your code goes here ...
        break;
    case RIGHT_ORIGIN:
        // Your code goes here ...
        break;
    }
}

When pasting your code, remove all references to DistractionsArray[n] and replace them with distraction

EDIT:

If this helps you, then please consider 1) accepting more answers on your questions and 2) Not putting stuff like 'please help' in your titles. It's a site geared towards help, we know what you are looking for ;)

Your hideDistraction() and doDraw code is likely fine.

Hamy
I took the adjustment to mCurrentDistractionsOnScreen out of the loop as suggested but this bug is very strange. It sometimes clears just the objects on the same y co-ordinate and leaves the rest, or clears a totally unrelated object aswell as the one that has passed the tripwire. bizarre.Is there no way to serialise the objects on the canvas at the point of a collision and rebuild it at the correct point in the program (minus the object that has collided)? if not I am going to have to rethink the way the methods work.I have chosen your answer and thank you for your help.
Greenhouse Gases
@Greenhouse, I would actually recommend that you un-accept my answer if your problem is not fixed ;) How about you edit the original question with your updated code, and update exactly what is happening the the on-screen objects? Also make a note at the bottom to point out that you edited the question
Hamy
Well I've just been jotting down some potential changes on a notepad, of which I have a few that I will try out but it will take me some time. For instance, I am going to check all uses of the mCurrentDistractionsOnScreen variable especially in control structures as objects in play may not be sequentially indexed in the array that stores them / perhaps set a flag in collision detection for those needing hiding and after the loop employ a counter of objects still in play to set mCurrentDistractionsOnScreen and deal with those flagged for hiding. I have a few other ideas but hopefully one works!
Greenhouse Gases
Also, just to say, in some questions I havent accepted answers because they didnt entirely fix my problem so I'm between a rock and a hard place. I want to choose an answer to show my gratitude but if it's not a proper solution you'd recommend not choosing it which I understand aswell!
Greenhouse Gases
Okie dokie - good luck!
Hamy