How to create sprite/ moving figures?

Stellaferox

Active Member
Licensed User
Hi,

Is there a way to create a sprite or a moving figure? I want to make a game in which things are moving against a background.
thnx for your input!
Marc
 

Cableguy

Expert
Licensed User
Longtime User
Can You please define "sprite" to me?
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
You can use FDrawImage, but to make the image appear to move you will need to erase the area that the last image occupied before redrawing in a slightly different place.

Regards,
RandomCoder
 

Stellaferox

Active Member
Licensed User
But if I use Erase won't I erase the Forelayer underneath? I'd like to address the 2 forelayers separately. Is that possible?
Marc
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
You can use the normal (background) Form layer using DrawImage and then apply a separate image to the Forms forelayer using the FDrawImage command. Finally you can erase part or all of the forelayer using FErase before moving the image. Another useful aspect to using the forelayer is that it supports transparency so you can select a particular colour not to display on the forelayer.

Regards,
RandomCoder
 

Stellaferox

Active Member
Licensed User
OK, thnx, that explains it. Is there, by any chance, an example of the "famous" bouncing ball in code around to see this in action?
Marc
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Stellaferox,

I'm not familiar with the 'famous' boucing ball but here's a little somthing I knocked up whist having my dinner at work -
B4X:
Sub Globals
Height=100: LastHeight=100: Direction=5
End Sub

Sub App_Start
   Form1.Show
   Form1.Line(0,200,Form1.Width,200,cBlack)
   Form1.ForeLayer=True
   BounceBall
   Timer1.Enabled=True
End Sub

Sub BounceBall
   Form1.FCircle(Form1.Width/2,100,5,cRed,F)
   LastHeight=100
End Sub


Sub Timer1_Tick
   Height=LastHeight+Direction
   Form1.FErase(Form1.Width/2-5,LastHeight-5,Form1.Width/2+5,LastHeight+5)
   Form1.FCircle(Form1.Width/2,Height,5,cRed,F)
   LastHeight=Height
   If Height=195 Then Direction=-5
   If Height=100 Then Direction=5
End Sub

It's a never ending bouncing ball and the code needs some polishing up but at least it gives you the idea. I'm sure that you could gradually make the bounces smaller until finally coming to a stop and turn the timer off.

Regards,
RandomCoder
 

Stellaferox

Active Member
Licensed User
I changed it somewhat to

Sub Globals
Height=5: LastHeight=5: dY=5
Width=5: LastWidth=5: dX=5
HeightMin = 5: HeightMax = Form1.Height-5
WidthMin = 5: WidthMax = Form1.Width-5
End Sub

Sub App_Start
Form1.Show
Form1.ForeLayer=True
BounceBall
End Sub

Sub BounceBall
Height = Rnd(HeightMin,HeightMax)
Width = Rnd(WidthMin,WidthMax)
FOR x=1 to 1000
For y=1 to 1000
NEXT
DoEvents
LastWidth=Width
LastHeight=Height
Form1.FErase(LastWidth-5,LastHeight-5,LastWidth+5,LastHeight+5)
Height=Height+dY
Width=Width+dX
Form1.FCircle(Width,Height,5,cRed,F)
IF Height>=HeightMax Then
Height = HeightMax
dY = -dY
END IF
IF Height<=HeightMin Then
Height = HeightMin
dY=-dY
END IF
IF Width >= WidthMax then
Width = WidthMax
dX = -dX
END IF
IF Width <= WidthMin then
Width = WidthMin
dX = -dX
END IF
NEXT
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Looks like you've got the idea then :sign0060:

I thought afterwards that I could have just erased a full strip that would have covered the whole area the ball travels, however this might not have been as good a demonstration as actually recording the last position.
Furthermore, if you plan on using an image and moving that around then you should quite easily be able to erase the position it occupies just by locating it's top and left position.

Regards,
RandomCoder
 

Shrek_Old

Member
Licensed User
Sub Globals
Height=10: Dim g
End Sub

Sub App_Start
g=1.05
Form1.Show
Form1.Line(0,200,Form1.Width,200,cBlack)
SetTransparentColor=cGreen
Form1.ForeLayer=True
Form1.FCircle(Form1.Width/2,Height,10,cBlack,F)
Timer1.Interval =50
Timer1.Enabled=True
End Sub


Sub Timer1_Tick
Form1.FCircle(Form1.Width/2,Height,10,cGreen,F)
direction=direction*g
Height=Height*g
If Height>190 Then
Height=190
g=0.95
End If
If Height<=10 Then g=1.05
Form1.FCircle(Form1.Width/2,Height,10,cBlack,F)
End Sub
 

willisgt

Active Member
Licensed User
Isn't there already a Sprite library?

(...rummages through boxes looking...)

I'm sure there's a Sprite library in here somewhere...


Gary
 
Top