Android 如何在屏幕和引擎的特定部分添加精灵
我使用此方法随机向屏幕添加精灵Android 如何在屏幕和引擎的特定部分添加精灵,android,andengine,Android,Andengine,我使用此方法随机向屏幕添加精灵 private void addFace() { Random rand = new Random(); float x = (int) mCamera.getHeight() + mBallTextureRegion.getHeight(); float minY = mBallTextureRegion.getHeight(); float maxY = (int)(mCamera.getWidth
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;
唯一的问题是我希望精灵被添加到屏幕的顶部(即相机),但它们被添加到屏幕的侧面
有什么建议吗?首先,实体的(x,y)坐标位于其左上角。所以,minY
应该是0
,或者您可以执行以下操作:
float y = rand.nextFloat(maxY);
您不需要所有这些(int)
强制转换,您可以删除它们
为了使定位随机,也要获得一个随机X:
float maxX = this.mCamera.getWidth() - this.mBallTextureRegion.getWidth();
float x = rand.nextFloat(maxX);
这应该是可行的。在android屏幕上,坐标系的原点位于左上角
(0,0)......(1,0)
(0,1)......(1,1)
因此,如果您希望某些内容始终在屏幕顶部生成,那么Y需要为0。总是。X值可以是从0到宽度的任意值。这会将对象放置在顶部,并在X(左-右)方向上随机放置