开发者

Attempting to remove an object off screen moves all the objects in play?! - Android. Please Help!

开发者 https://www.devze.com 2023-01-09 06:05 出处:网络
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

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_开发者_如何学PythonORIGIN:
                // 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消