3
\$\begingroup\$

This is my first android game, I appreciate all the help. I want to define 4 spawning zones starting from out of the screen (left, right, up and down). Enemies coming from this areas have a fixed trajectory to the center, when this enemies come into the screen the player can move them to a specific corner, but if it lets go of it a new trajectory has to be calculated so it keeps moving to the center.

I am having trouble thinking on how to define this 4 areas, there is also 4 types enemies (they are the same, but they have a different color), should I place this 4 enemies on an arrayList ?. If I know how to define this 4 spawning areas which randomly decides where to spawn I then don't exactly know how to define the fixed trajectory to the center. Also, need to lower the speed to a more adecuate one. (Sorry if this seems like a lot am asking)

Current code looks like this (I am using AndEngine library):

public class gameEscene extends baseScene {

    // Regions for images
    private ITextureRegion BackgroundRegion;
    private ITextureRegion Corner;
    private ITextureRegion Center;
    private ITextureRegion Marco;
    // Sprites
    private Sprite spriteBackground;
    private Sprite spriteCorner;
    private Sprite spriteCenter;
    private Sprite spriteMarco;


    //  Enemies
    private ArrayList<Enemies> listEnemies;
    private ITextureRegion regionEnemy;
    //  Time to generate enemies
    private float timeEnemies = 0;              //  Counter
    private float   TIME_LIMIT  =   2.5f;       //  Every 2.5s it creates


    @Override
    public void loadResources() {
        BackgroundRegion = loadImage("black.jpg");
       Corner = loadImage("Corner_T.png");
        Center = loadImage("Center.png");
        Marco = loadImage("m.png");
        regionEnemy = loadimage("ship.png");
    }

    private void createEnemies() {
        for (int x = 700; x <= 1200; x += 100) {
            for (int y = 100; y <= 700; y += 100) {
                Sprite ship = loadSprite(x, y, regionEnemy);
                attachChild(ship);
                Enemies enemy = new Enemies(ship);
                listEnemies.add(enemy);
            }
        }

    }

    @Override
    public void createScene() {
        listEnemies = new ArrayList<>();
        spriteBackground = loadSprite(GameControl.CAMARA_WIDTH/2, GameControl.CAMARA_HEIGHT/2, BackgroundRegion);
        spriteCorner = loadSprite(GameControl.CAMARA_WIDTH/2, GameControl.CAMARA_HEIGHT/2, Corner);
        spriteCenter = loadSprite(GameControl.CAMARA_WIDTH/2, GameControl.CAMARA_HEIGHT/2, Center);
        spriteMarco = loadSprite(GameControl.CAMARA_WIDTH/2, GameControl.CAMARA_HEIGHT/2, Marco);
        attachChild(spriteBackground);
        attachChild(spriteCorner);
        attachChild(spriteCenter);
        attachChild(spriteMarco);
        createEnemies();
    }

    @Override
    public void onBackKeyPressed() {
        // Regresar al menú principal
        admScenes.createMenuScenes();
        admScenes.setScene(SceneType.MENU_SCENE);
        admScene.liberateGameScene();
    }

    @Override
    public sceneType getSceneType() {
        return sceneType.GAME_SCENE;
    }

    @Override
    public void liberateScene() {
        this.detachSelf();
        this.dispose();
    }

    @Override
    public void liberateResources() {
       BackgroundRegion.getTexture().unload();
        Corner.getTexture().unload();
        Center.getTexture().unload();
        Marco.getTexture().unload();
        regionEnemy.getTexture().unload();
        BackgroundRegion = null;
        Corner = null;
        Center = null;
        Marco = null;
        regionEnemy = null;
    }

    @Override
    protected void onManagedUpdate(float pSecondsElapsed)   {

        super.onManagedUpdate(pSecondsElapsed);
        timeEnemies +=  pSecondsElapsed;        //  Acumulates time
        if  (timeEnemies>TIME_LIMIT)    {   //  Time has completed
            tiemeEnemies    =   0;
            Sprite spriteEnemy  =   loadSprite(GameControl.CAMARA_WIDTH+regionEnemy.getWidth(),
                    (float)(Math.random()*GameControl.CAMARA_HEIGHT-regionEnemy.getHeight())    +
                            regionEnemy.getHeight(),regionEnemy);

            Enemies newEnemy = new Enemies(spriteEnemies);
            //newEnemies.mover(0,10);
            listEnemies.add(newEnemy);  //  Adds it to the scene
            attachChild(newEnemy.getSpriteEnemy()); //  Adds it to the list
        }

        //  Updates every enemy and sees if any has gotten out of the screen
        for (int    i=listEnemies.size()-1; i>=0;   i--)    {
            Enemies enemy   =   listEnemies.get(i);
            enemy.mover(-10, 0);
            if  (enemy.getSpriteEnemy().getX()<-enemy.getSpriteEnemy().getWidth())  {
                detachChild(enemy.getSpriteEnemy());        //  Deletes it from the scene
                listEnemies.remove(enemy);                                                                  //  eliminates it from the list
            }
            //  Checks the collision of enemy with another sprite

            if  (spriteCenter.collidesWith(enemy.getSpriteEnemy())) {
                detachChild(enemy.getSpriteEnemy());
                //enemies.remove(enemy);

            }
        }
    }

}

Enemies class looks like this:

public class Enemies {

    private Sprite sprite;

    public Enemies(Sprite sprite) {
        this.sprite = sprite;
    }

    public Sprite getSpriteEnemy() {
        return sprite;
    }

    public void setSprite(Sprite sprite) {
        this.sprite = sprite;
    }

    public void mover(int dx, int dy) {
        sprite.setPosition( sprite.getX()+dx, sprite.getY()+dy );
    }

}

Thanks in advance for the help.

Edit: Screenshot of idea

Game concept

\$\endgroup\$
  • \$\begingroup\$ I'm finding it hard to visualise your problem. Could you perhaps draw it, or post an annotated screenshot? \$\endgroup\$ – Anko Oct 24 '15 at 0:00
  • \$\begingroup\$ Sure, I put an image of the idea above. So, enemies will be coming from this 4 areas (coming from outside the screen), with a fixed trajectory to the center image/sprite, the user has to move them to their corresponding color, if the player doesn't place if in the corresponding color and drops it on a blank space, it still has to recalculate trajectory to the center. Sorry If my idea didn't come thru, and thanks in advance. \$\endgroup\$ – OrangeHat Oct 24 '15 at 0:58
0
\$\begingroup\$

It looks to me like you are not saving the enemies locations in their class, try using a random to generate Their location (from presets ranges) and then using ArrayLists and Itteraters together to spawn and ammend their positions?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.