B4J Question [Solved] Simple sprite from spritesheet

schimanski

Well-Known Member
Licensed User
Longtime User
I have no experience with the representation of moving objects on the screen. There are a number of posts that deal with the topic. It just seems to me that libraries like XUI2D are already too extensive for my purposes. I'm looking for a unique solution on how to run a GIF from left to right across the screen and register a mouse click event. The example program shows how I mean it. Unfortunately, it looks very jerky and it also seems to put too much strain on the hardware. When I embed it in my application, the gif keeps stalling. What do you think would be the most effective solution for this simple area of application. Thank you for your support.
 

Attachments

Solution
Thanks for this solution. It's hardware-accelerated, and with a few minor tweaks, I got exactly where I wanted. I used a spritesheet instead of a gif. The gif limit on my computer is 60 sprites, before it runs in ja java-heap. Using a spritesheet, I tested up to 1,000 sprites without any problems. I've made the sample program available under 'Code Snippets'. Perhaps someone else can use it:

https://www.b4x.com/android/forum/threads/simple-code-for-1000-clickable-sprites.166870/
A slightly different version.
[CODE lang="b4x" title="B4X"]#Region Project Attributes
#MainFormWidth: 1920
#MainFormHeight: 1000
#End Region

Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private ImageView1 As ImageView
Private t As Timer

Private ScreenWidth As Int
Private PosX As Int

End Sub
'
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")

t.Initialize("t", 20)
MainForm.Show

ScreenWidth = 1920
PosX = 0

t.Enabled=True
End Sub
'
Sub Button1_Click
PosX=0-ImageView1.Width
End Sub

Sub t_Tick
' Move the character
PosX = PosX + 3 'character speed
If PosX > ScreenWidth Then PosX = 0-ImageView1.Width ' Reset if character goes off screen
Log(PosX)
ImageView1.Left = PosX
End Sub[/CODE]
 
Upvote 0
If it's a simple simulation, see this.

GIFs are controlled frames

1.gif


ex. convert gif to png
 
Upvote 0
I tried to optimize the example using the spritesheet. Using the example program, you can vary the number of displayed sprites. Each spritesheet is maintained with BitmapEffect, so the figure can also move in the other direction. The sprites are also scaled. This makes the upper ones look as if they are further away. Unfortunately, the performance is significantly worse than I had hoped. Strangely, the program also runs more smoothly in debug mode than in release mode. I tried to optimize it completely by reading the frames completely at the beginning and also not redrawing the imageview with every tick. But that wasn't particularly efficient either. I find the use of spritesheets very useful for me. Does anyone see any optimization opportunities in this example, or do I need to take a completely different approach?
Thanks for your support.
 

Attachments

Upvote 0
Despite numerous attempts, I'm just not making any progress. The program above, Spites.zip, creates various sprites on the screen using BitmapCreator. Unfortunately, the performance isn't sufficient for my purposes. I've now tried animating this spritesheet using X2 to take advantage of the hardware acceleration. It doesn't work. Does anyone spot my mistakes in the SpritesX2 example? I only need the animation of sample one, only with better performances.Thanks for your support.
 

Attachments

Upvote 0
Thanks for this solution. It's hardware-accelerated, and with a few minor tweaks, I got exactly where I wanted. I used a spritesheet instead of a gif. The gif limit on my computer is 60 sprites, before it runs in ja java-heap. Using a spritesheet, I tested up to 1,000 sprites without any problems. I've made the sample program available under 'Code Snippets'. Perhaps someone else can use it:

https://www.b4x.com/android/forum/threads/simple-code-for-1000-clickable-sprites.166870/
 
Upvote 0
Solution
Back
Top