java-如何更改精灵随机出现在场景中的位置?
作者:互联网
我将这种方法与AndEngine一起使用,以将精灵添加到屏幕上并使其在屏幕上移动.
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);
}
我想做的是,而不是选择“随机”位置并将子画面添加到场景的左侧,我希望将其添加到“顶部”并掉落.
我基本上想改变方向,
我只是不知道如何.我尝试的一切都不是运气.
有什么想法或建议吗?
解决方法:
虽然我对Andengine不太了解,但我怀疑您想更改以下几行:
float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight();
float y = rand.nextInt((int)rangeY) + minY;
MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth());
翻转x和y语句,但如果希望它显示在屏幕外部,则将y设置为-mBallTextureRegion.getHeight().
对于MoveXModifier,我想有一个对应的MoveYModifier(分别使用face.getY()和-face.getHeight())
标签:andengine,box2d,java,android 来源: https://codeday.me/bug/20191202/2086078.html