Dark Bit Factory & Gravity
PROGRAMMING => Purebasic => Topic started by: Omnikam on November 01, 2015
-
Hey there, I'm having a problem with my code
I'm trying to move an image across the screen and then move it back, and repeat the procedure, so it move moves in a PingPong. My solution was to use a timer and have it set conditions using if/end Unfortunately it doest work as expected, I'm not sure why because my code logic seem logical. Any help would be appreciated
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard()
Global timer= 1
a= 0
b= -1100
OpenScreen(800,600,32,"PBWINDOW")
wave = LoadSprite (#PB_Any, "wave.png")
Repeat
ExamineKeyboard()
TransparentSpriteColor(wave,#White)
ClearScreen($0)
DisplayTransparentSprite(wave, a,300)
DisplayTransparentSprite(wave,b,400)
If timer<900
b=b+2
timer=timer+1
EndIf
If timer>1200
b=b-2
timer = timer+1
EndIf
If timer=>2000
timer=0
EndIf
FlipBuffers()
Until KeyboardPushed (#PB_Key_Escape)
End
-
I don't have purebasic here, but try to use screen coordinates and a variable to control direction, something like this:
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard()
Global timer= 1
a = 0
b = 200
moveflag = 0
OpenScreen(800,600,32,"PBWINDOW")
wave = LoadSprite (#PB_Any, "wave.png")
Repeat
ExamineKeyboard()
TransparentSpriteColor(wave,#White)
ClearScreen($0)
DisplayTransparentSprite(wave, a,300)
DisplayTransparentSprite(wave,b,400)
If moveflag=0
b=b+2
if b>500
moveflag = 1
EndIf
EndIf
If moveflag=1
b=b-2
if b<100
moveflag = 0
EndIf
EndIf
FlipBuffers()
Until KeyboardPushed (#PB_Key_Escape)
End
-
That was just what i was looking for, Thankyou. It`s easy to understand whats going on, so ill be able to
use it for lots of different applications
have good karma
-
Here are some other ping pong routines that you may find useful.
The first is a ping pong using a procedure, everytime it is called the value is increased or decreased if it has reached its max :
#maxCount = 500
Procedure PingPong()
Static _count = #maxCount - 1
_count = (_count + 1) % (#maxCount << 1)
ProcedureReturn Abs(_count - #maxCount)
EndProcedure
Repeat
Debug PingPong()
Delay (5)
ForEver
Then we have one which looks much more complex but has the benefit of being autonomous, and you can have multiple pong pongs being managed with the same procedure :
; With this one you can set a min, a max, and you can keep track of more then one ping-pong at the time.
Structure T_PINGPONG
min.i
max.i
counter.i
direction.i
EndStructure
Procedure.i PingPong (*p.T_PINGPONG)
If *p\direction = 0
*p\direction = 1
*p\counter = *p\min
Else
*p\counter + *p\direction
If *p\counter = *p\min Or *p\counter = *p\max
*p\direction = -*p\direction
EndIf
EndIf
ProcedureReturn *p\counter
EndProcedure
;****************************
; DEMO
;****************************
Define pp1.T_PINGPONG
Define pp2.T_PINGPONG
pp1\min = 1
pp1\max = 10
pp2\min = -5
pp2\max = 5
Repeat
Debug "pp1 = " + PingPong(@pp1) + ", pp2 = " + PingPong(@pp2)
Delay (500)
ForEver
And put into your demo, as you can see the logic loop is much simpler now :
InitSprite()
UseJPEGImageDecoder()
UsePNGImageDecoder()
InitKeyboard()
Global timer= 1
Structure T_PINGPONG
min.i
max.i
counter.i
direction.i
EndStructure
Procedure.i PingPong (*p.T_PINGPONG)
If *p\direction = 0
*p\direction = 1
*p\counter = *p\min
Else
*p\counter + *p\direction
If *p\counter = *p\min Or *p\counter = *p\max
*p\direction = -*p\direction
EndIf
EndIf
ProcedureReturn *p\counter
EndProcedure
Define pp1.T_PINGPONG
Define pp2.T_PINGPONG
pp1\min = 0
pp1\max = 50
pp2\min = 0
pp2\max = 50
OpenScreen(800,600,32,"PBWINDOW")
wave = LoadSprite (#PB_Any, "wave.png")
Repeat
ExamineKeyboard()
TransparentSpriteColor(wave,#White)
ClearScreen($0)
DisplayTransparentSprite(wave, 50-PingPong(@pp1)-100,300)
DisplayTransparentSprite(wave,PingPong(@pp2)-50,400)
FlipBuffers()
Until KeyboardPushed (#PB_Key_Escape)
End
-
Nice one Emook :goodpost: Ill definatly have to study each example
Have good karma ;D
Okay i looked at them, Brilliant :clap: very very handy