Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:11+00:00 2026-05-16T03:34:11+00:00

I am building an android game where objects randomly come in from either side

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:34:11+00:00Added an answer on May 16, 2026 at 3:34 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.