I'm working on a game where I want to replace all these white blocks and the ball (which uses a RectF collider) with images, but still keep the characteristics of the game.
How can I import an image like this: into the game but still keep the game mechanics? (Colliding and such).
Let me know if I should post some code I already have in order to help or anything else. Thank you!
You can use the method:
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
dst have to be rectF you currently been using.
For a bitmap like the sample one you have shown, scr can be null since you want to draw the entire image.
Related
I've been trying to draw an image of a block with an eye on the screen. I want to be able to animate more than one texture in the same way, so drawing the animations by hand is not an option. The problem is that when I move the eyelid sprite higher up than completely closed, part of the eyelid shows above the expected bounds like this. Obviously, this is a problem as it looks quite unnatural. I'd like to either have an alternative solution to this problem, or be able to crop the eyelid's Sprite object to fit behind the rest of the image. The final image consists of the eyeball, pupil (as I want to be able to animate this too), eyelid, main body and the outlines, drawn in that order. The render function looks like this:
public void render(float delta) {
float height = this.eyelid.getHeight();
float eyeHeight = height*0.7f;
this.eyelid.setY(this.eye.getY()+(eyeHeight*((100-this.lid)/100f)));
batch.begin();
this.eye.draw(batch);
this.pupil.draw(batch);
this.eyelid.draw(batch);
this.main.draw(batch);
this.shade.draw(batch);
batch.end();
}
this.lid is the % of how closed the eye is, and the image for the eyelid itself can be found here. How could I solve this problem, or how could I crop the sprite? I don't want to have to reload the texture as a sprite every frame.
I think what you're looking for is the ScissorStack class, which is documented on the libGDX wiki. You can use this object to clip around the eye's frame so that the top of the lid doesn't show.
I'm trying to make asteroids that spin in my 2D game.
Using the graphics class I am drawing an image I created onto the screen (the asteroid) and moving it across the screen.
My next step was to make it rotate however I am lost at to how to do this.
I was able to make it rotate when I was simply drawing a polygon by changing the vertices of the asteroid however there are no vertices when drawing images, only (x,y) and (length,width).
How can I rotate an image? Is there any inbuilt functionality that does it?
I'm not asking for someone to tell me exactly how to do this, I'm just looking for a push in the right direction as I am a bit lost.
Either use a AffineTransform or use Graphics#rotate. In either case, you should make a copy of the Graphics context first (Graphics#create) first, which will preserve the state of the original context. Just make sure you dispose of the copy when you're done (Graphics#dispose)
Something like this perhaps
In Android I'm using Canvas.drawPath( Path, Paint ) to fill a Path. I've set the Paint variable to use a BitmapShader & the Style to Fill. This method works fine until I try to use additional clipping, i.e. I try to draw to a sub-region within the Path like so
theCanvas.clipRect( visibleRect, Region.Op.REPLACE );
theCanvas.clipRect( additionalClipping, Region.Op.INTERSECT );
theCanvas.drawPath( path, paint );
My desired result is to have the texture within the BitmapShader within the Paint, drawn to a rectangular area intersected with edge of my path.
But the actual result of this is to have the texture tiled all over the Path area - the clipping I set on the Canvas seems to have had no effect.
Its almost as if calling Canvas.drawPath internally calls Canvas.setClip( Path, Region.Op.Replace ).
Any help is much appreciated, thank you.
Short answer to my own question is yes, Canvas.drawPath does use the clipping set on the canvas as well as the shape of the path itself, it does not overwrite it.
theCanvas.clipRect(new Rect(632, 269, 1265, 539 ), Region.Op.REPLACE);
theCanvas.drawPath( theNativePath, thePaint );
The above code produces my path, painted with my texture, but within the combined area of the path and the rectangle I applied to the clipping in the canvas beforehand. My texture still looks wrong, but the issue is not with the clipping, it behaves as intended.
Upon further investigation this morning I've narrowed the problem down to something I've done wrong with either the BitmapShader in the Paint object, or its an issue in my texture cache which supplies the BitmapShader.
Apologies for any confusion - I will now put on the 'dunce' hat.
I want to have a drawing canvas within my application which will display letter to free draw for practice. Something like the following:
OR
This is the first time I will be implementing this method so a few questions.
Can I have my own background for the canvas? (let's say a chalkboard?)
Do I have to create the letter as image and insert it onto the canvas as Bitmap?
I saw some tutorials but wasn't too clear on how to implement it within app.
I found a solution for you. Having more than 1 Canvas is not the answer (Although may work, I'm still new), but instead draw a seperate Bitmap on top of the first Bitmap (background).
Try this out for size: Overlying Bitmap
You should then be able to edit the overlaying Bitmap without affecting the original Bitmap.
Cheers!
I am writing a particle based game that is mainly built by drawing lots of colored shapes.
Question 1)
For most of the enemy units I am drawing 4 layered rectangles by setting the paint and then drawing the rectangle through the canvas.
I was wondering if it is better to draw using bitmaps, or to draw using the canvas drawing tools? I could easily make a single image of the enemy unit that I wish to draw.
Question 2)
For the images that I have to draw to the screen, I was wondering how I need to load them?
Right now I have tons of .png images loaded like this:
direction1 = BitmapFactory.decodeStream(assetMgr.open("direction1.png"));
I've read that RGB565 is the fasted image type to draw to the screen. Microsoft Paint has some saving options, but for the most part programs only save as a bitmap, not a type of bitmap. If I was to start using that new format would I:
Make new images and use the same loading code.
Use the same images and add something like Bitmap bmp =
Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); to
convert from the initial loaded format to the RGB565 format.
Make new images and change my loading code.
Thanks for any help! It is very appreciated.
None. It is always better to use OpenGL and the only downside is
that it requires more energy from a battery because it's hardware
accelerated.
RGB565 means that image uses 16 bits so that's the
option you should look for. Don't convert anything, just create them
in the format you will be using.
If the Bitmap aren't moving frame-by-frame, you should try to reduce invalidate() method call. The canvas should only be re-drawn when changes are made and should be updated.