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 6131871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:01:53+00:00 2026-05-23T17:01:53+00:00

I am developing a simple 2d game. But im stuck at this point where

  • 0

I am developing a simple 2d game. But im stuck at this point where i need to “spawn” unlimited with the same enemy when i click the screen.

So i think the best choose for something with unlimited is an array but i have no idea how to get a bitmapArray and then for each item in BitmapArray do canvas.draw

Someone please help me out!

//Simon

  • 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-23T17:01:54+00:00Added an answer on May 23, 2026 at 5:01 pm

    First of all, you will end up with a sort of limited array. Secondly, I’m recommending the Memory Pool pattern when using stuff like this, so you don’t create new instances during runtime.

    Back to your question, a first implementation would look something like this:

    public class BitmapObject {
        private Bitmap mBitmap;
        private int mPositionX;
        private int mPositionY;
        private int mBitmapWidth;
        private int mBitmapHeight;
        private boolean mIsAlive;
    
        public BitmapObject(Bitmap bitmap) {
            mBitmap = bitmap;
            mBitmapWidth = bitmap.getWidth();
            mBitmapHeight = bitmap.getHeight();
            mIsAlive = false;
        }    
    
        public void draw(Canvas canvas) {
            if (mIsAlive) {
                canvas.drawBitmap(mBitmap, mPositionX, mPositionY, null);
            }
        }
    
        public void setNewPosition(int touchX, int touchY) {
            mPositionX = touchX - mBitmapWidth / 2;
            mPositionY = touchY - mBitmapHeight / 2;
        }
    
        public void setIsAlive(boolean isAlive) { mIsAlive = isAlive; }
    
        public boolean getIsAlive() { return mIsAlive; }
    
    }
    

    And use it like this in your SurfaceView class:

    public class CanvasRenderer extends SurfaceView implements SurfaceHolder.Callback {     
        private static final int MAX_OBJECTS = 16;
        private BitmapObject[] mBitmapObjectsArray;
    
        public CanvasRenderer(Context context) {
            super(context);
             // Necessary SurfaceView initialization stuff.
    
             Bitmap sprite = BitmapFactory.decodeResource(context.getResources(), 
                 R.drawable.sprite);
             mBitmapObjectsArray = new BitmapObject[MAX_OBJECTS];
    
             for (int x = 0; x < MAX_OBJECTS; x++) {
                 mBitmapObjectsArray[x] = new BitmapObject(sprite);
             }
        }
    
        @Override 
        public void onDraw(Canvas canvas) {     
            canvas.drawColor(Color.BLACK);  
            for (int x = 0; x < MAX_OBJECTS; x++) {
                mBitmapObjectsArray[x].draw(canvas);
            }
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // Stuff.
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {      
            // Stuff.
        }
    
        @Override
        public void onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                for (int x = 0; x < MAX_OBJECTS; x++) {
                    boolean isAlive = mBitmapObjectsArray[x].getIsAlive();
                    if (!isAlive) {
                        mBitmapObjectsArray[x].setNewPosition((int) event.getX(), 
                            (int) event.getY());
                        mBitmapObjectsArray[x].setIsAlive(true);
                        break;
                    }
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing a prototype for a game and I need a simple
I am developing a simple game on iPhone and I started with a View
I am developing a simple platform game in Java using BlueJ. Over the next
Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
Greetings Friends, I am trying to overlay the simple 2D game I am developing
I'm currently developing a simple 2D game for Android. I have a stationary object
I'm developing a musical game, it's like a singstar but instead of singing, you
I'm developing a simple game in Android and I want to detect a collision
I have been developing a simple game for iOS which involves dragging and using
I am developing a simple game. After every round the results will be displayed.

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.