coder_For_Life22
coder_For_Life22

Reputation: 26971

How to change location that sprite Randomly appears in Scene?

I am using this method with AndEngine to add a sprite to the screen and make it move across the screen.

 private void addFace() {
     Random rand = new Random();

        float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight();
        float minY = mBallTextureRegion.getHeight();
        float maxY = (int)(mCamera.getWidth() +  mBallTextureRegion.getWidth());
        float rangeY = maxY - minY;
        float y = rand.nextInt((int)rangeY) + minY;

     this.mFaceCount++;
     Log.e("Faces: ", "Face" + this.mFaceCount);

    Sprite face = null;
     Body body = null;


             face = new Sprite(x, y, this.mBallTextureRegion.clone());
             body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

      this.mScene.attachChild(face);
     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
             int minDuration = 2;
            int maxDuration = 4;

            int rangeDuration = maxDuration - minDuration;
            int actualDuration = rand.nextInt(rangeDuration) + minDuration;

            MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

            face.registerEntityModifier(mod);

}

What i would like to do is, instead of the Random position being selected and the sprite being added to the left side of the Scene, i would like for it to be added to the Top and fall down.

I basically want to flip the direction, I just cant figure out how. Everything i tried was no luck.

Any ideas or suggestions?

Upvotes: 3

Views: 668

Answers (1)

Orka
Orka

Reputation: 1105

While I don't know much about Andengine, I suspect you would want to change these lines:

float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight();
float y = rand.nextInt((int)rangeY) + minY;

MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());

Flip the x and y statements, but set y to -mBallTextureRegion.getHeight() if you want it to appear outside the screen.

For the MoveXModifier, I would guess there is a corresponding MoveYModifier (use face.getY() and -face.getHeight() respectively)

Upvotes: 3

Related Questions