AndEngine可见矩形

时间:2013-06-17 09:57:51

标签: entity andengine visibility scene drawrectangle

我正在尝试在主场景上创建一个额外的可见性矩形。 所以我是一个主要的相机480x800,它正在向我展示一个场景,我想附加一个附加实体或一个具有可见性矩形的场景。 因此,如果我将物品拖入其中,它们将不会在一瞬间变得更加黯淡,它们会逐渐消失。 enter image description here

1 个答案:

答案 0 :(得分:2)

如我之前的评论中所述,您可以在背景Sprite中删除一个方形alpha孔。您只需使用图像编辑器,添加alpha像素,或者可以按照以下方式动态执行此操作,

//set the background to white - so we can see our square alpha
//cut out later
mScene.setBackground(new ColorBackground(1.0f, 1.0f, 1.0f));

//Create and load bitmap texture atlas
BitmapTextureAtlas mBitmapBGTextureAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
mActivity.getEngine().getTextureManager().loadTextures(mBitmapBGTextureAtlas);


//Get image in assets and decode into bitmap
InputStream ims;
try {
    ims = mActivity.getAssets().open("gfx/my_backgound.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
    return;
}   
Bitmap Bitmap_bg = BitmapFactory.decodeStream(ims);

//In my case the image is different than the height and width of the camera
//so store the ratio of size and height that the image will be resized to
float XScale = Bitmap_bg.getWidth()/mCamera.getWidth();
float YScale = Bitmap_bg.getHeight()/mCamera.getHeight();

//Cut out the alpha square, if our camera is 480x800, the square will appear
//at (40,200) and will be size 400x400
Bitmap_bg = cutSquareOutOfBitmap(Bitmap_bg, 40 * XScale, 200 * YScale, 400 * XScale , 400 * YScale);

//Get our edited bitmap into a region of the texture atlas
BitmapTextureAtlasSource source = new BitmapTextureAtlasSource(Bitmap_bg);
mBackground = BitmapTextureAtlasTextureRegionFactory.createFromSource(mBitmapBGTextureAtlas, source, 0, 0);
Bitmap_bg.recycle();

//Finally, create our background sprite with this new texture region
Sprite mBackgroundSprite = new Sprite(0, 0, mCamera.getWidth(), mCamera.getHeight(), mBackground);
mBackgroundSprite.setZIndex(1);
mScene.attachChild(mBackgroundSprite);

函数cutSquareOutOfBitmap()

public static Bitmap cutSquareOutOfBitmap(Bitmap MyImage,  float Xpos,  float Ypos, float Width, float Height) { 

    Bitmap mBitmap = MyImage.copy(Bitmap.Config.ARGB_8888, true);

    Paint mPaint = new Paint(); 

    Canvas mCanvas = new Canvas(mBitmap ); 

     mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);          
    mPaint .setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));  
    mPaint .setColor(Color.TRANSPARENT); 

    mCanvas.drawBitmap(mBitmap , 0, 0, null); 
    mCanvas.drawRect(Xpos, Ypos, Xpos + Width, Ypos + Height, mPaint ); 

    return mBitmap ;
}

如果你运行这个 - 不过要看很多而是一个大的白色方块,然而,这是一个透明的区域,方形实际上是我们之前设置的背景。

为了演示如何隐藏内容,您可以创建一个可滚动区域,如我之前的评论所述,我写了一个小容器类,欢迎您使用,

Custom ScrollView in andengine

在此答案中的第一个代码块之后,

之后
mScene.attachChild(mBackgroundSprite);

您现在可以添加,

//Now we can use the ShapeScrollContainer just as an example so the user can
//scroll our container shapes around

//Create it around the same area as the cut out
ShapeScrollContainer mShapeScrollContainer = new ShapeScrollContainer(40, 200, 400, 400, new IShapeScrollContainerTouchListener() {

    @Override
    public void OnContentClicked(Shape pShape) {
        // TODO Auto-generated method stub
        //Add code here for content click event
    }

});

//Disable the ShapeScrollContainer ability to change the visibility
//of contents - we no longer require this as the background will
//cover them outside of the bounds of the ShapeScrollContainer itself
mShapeScrollContainer.SetContentVisiblitiyControl(false);
//Disable alpha
mShapeScrollContainer.SetAlphaVisiblitiyControl(false);
//Allow user to scroll both horizontally and vertically
mShapeScrollContainer.SetScrollableDirections(true, true);
//Don't allow the user to scroll to no where
mShapeScrollContainer.SetScrollLock(true);
//Allow use to scroll half the container over in either direction
mShapeScrollContainer.SetScrollLockPadding(50.0f,50.0f);
//Attach the container to the scene and register the event listener
mScene.registerTouchArea(mShapeScrollContainer);
mScene.attachChild(mShapeScrollContainer);

//Finally add some content to the container, what ever extends Shape,
//Sprite, Animated Sprite, Text, ChangeableText e.t.c.

Rectangle mRectangle = new Rectangle(200, 360, 80, 80);
mRectangle.setColor(0.0f, 1.0f, 0.0f);
mRectangle.setZIndex(0);    
//Attach to the scene and the ShapeScrollContainer
mScene.attachChild(mRectangle);
mShapeScrollContainer.Add(mRectangle);

Rectangle mRectangle2 = new Rectangle(40, 360, 80, 80);
mRectangle2.setColor(0.0f, 0.0f, 1.0f);
mRectangle2.setZIndex(0);
mScene.attachChild(mRectangle2);
mShapeScrollContainer.Add(mRectangle2);

Rectangle mRectangle3 = new Rectangle(360, 360, 80, 80);
mRectangle3.setColor(1.0f, 1.0f, 0.0f);
mRectangle3.setZIndex(0);
mScene.attachChild(mRectangle3);
mShapeScrollContainer.Add(mRectangle3);

//And sort the order in which shapes are rendered
mScene.sortChildren();

现在滚动后你应该得到类似的东西,

Screen Shots while Scrolling

作为另一种选择,如果你想要更简单的背景,你可以放弃位图操作,只需制作四边形矩形的正方形构成屏幕边缘的边。

或者你可以将你的背景物理地分成4个周围的矩形和一个带有图像编辑器的中心方块。然后创建5个精灵,将四个矩形的z顺序设置为2,将正方形设置为0,将任何内容精灵设置为1.

希望这是有用的。