About My Piece of code!

Blitz3D Forums/Blitz3D Beginners Area/About My Piece of code!

Apollonius(Posted 2003) [#1]
Well my piece of code is about moving an arrow but when i do the images stays meaning, it draw multiples arrows, which it shouldnt, it should just move the arrow...

Heres my code can you fix it?
AppTitle "Practice"
Graphics 800, 600, 16, 2
SetBuffer Backbuffer()

Global menuarrow = 0
;----------------------
; Load Graphics
;----------------------
	  ; Char Sprites
Global CharA         = LoadImage("images/char1.png")
Global CharB         = LoadImage("images/char2.png")
		; Battle Scene
Global Status_Bar    = LoadImage("images/bset_bottom.png")
Global Fight_Menu    = LoadImage("images/bset_bottomATT.png")
Global Arrow         = LoadImage("images/arrow.png")

;---------------------
; Draw Script
;---------------------	

While Not KeyHit (1)
DrawImage Status_Bar,0,470
DrawImage Fight_Menu,20,440

If menuarrow = 0 Then
Ax = 0
Ay = 10
EndIf
If menuarrow = 1 Then
Ax = 0
Ay = 30
EndIf
If menuarrow = 2 Then
Ax = 0
Ay = 50
EndIf
If menuarrow = 3 Then
Ax = 0
Ay = 70
EndIf
If menuarrow = 4 Then
menuarrow = 0
EndIf



If KeyDown(208) Then ;press down
menuarrow = menuarrow + 1
EndIf

DrawImage Arrow,Ax,Ay
Flip
Wend



soja(Posted 2003) [#2]
Well, you have to erase the previous one that it drew. The easiest way is just to call Cls immediately after Flip.

Like this...
......
DrawImage Arrow,Ax,Ay
Flip:Cls
Wend



Apollonius(Posted 2003) [#3]
Whats : ?

Why use flip:cls
why not just
flip
cls
??
edited(I mean I dont get it cause shouldnt it errase status_bar and fight_menu?)
Oh and when I press down the arrow goes to fast its like selecting but if u keep pressing it it should go 1 wait 1 not 1 1 1 1 1... if you understand how can I delay it?


Neo Genesis10(Posted 2003) [#4]
Kaisuo: Your second problem is easy to solve - replace KeyDown with KeyHit. If you would rather be able to hold the key, set a timer which counts down (or up) and reset it every time the menu selection is changed. Example:
If KeyDown(200)
	timer = timer + 1
	If timer >= 20
		timer = 0
		menuarrow = menuarrow + 1
	EndIf
EndIf
As for your first dilemma: What you are seeing is everything which has been drawn to the buffer. When you draw to a buffer (front or backbuffer) it always remains unless you use a CLS command. Try filling the screen with graphics (using TileImage for example) or placing a Cls command into your code.


soja(Posted 2003) [#5]
The ":" is just a statement separator. Doing this...
statement1 : statement2
...is the same as this:
statement1
statement2

So no worries there.

As far as the other stuff goes, I would recommend re-drawing everything every frame (or everytime you go through the loop). Basically, like this:

1) Draw your stuff
2) Flip
3) Clear the backbuffer (cls)
4) Return to 1

This way if you just change the coordinates of where the pointer is during your loop, it will appear to animate.


FlameDuck(Posted 2003) [#6]
edited(I mean I dont get it cause shouldnt it errase status_bar and fight_menu?)
Yes. Redraw those aswell.


Apollonius(Posted 2003) [#7]
Well my point by shouldnt it erase status_bar and fight_menu is that it doesnt erase them but it shouldnt no?


soja(Posted 2003) [#8]
...it doesnt erase them but it shouldnt no?

What are you asking??

For our intents and purposes, the way a game works is this:
1) A frame gets drawn to a buffer
2) The frame is rendered to the screen
3) Relevant parts of the buffer are erased (usually the whole thing is erased)
4) Everything is redrawn again o nthe erased buffer
5) The new frame is rendered to the screen
...and so on.

This happens typically 60 times per second.


Neo Genesis10(Posted 2003) [#9]
Kaisuo: Your status bar and fight menu remain because they are always being drawn in exactly the same place. When you use a CLS command, you are only clearing the current buffer, which in your case is the unseen backbuffer. Anything drawn to the backbuffer is invisible. When you call the Flip command, the information stored on the buffers is swapped over (from front to back and vice versa) so you can see the drawn image.

So using CLS wont erase anything you need in this case.


Apollonius(Posted 2003) [#10]
Yeah after a while i figured it out, I played with it a bit as you can see below now it erase all :)

AppTitle "Shadiens - Tournament"
Graphics 800, 600, 16, 2
SetBuffer Backbuffer()

Global menuarrow = 0
Global draw = True
;----------------------
; Load Graphics
;----------------------
	  ; Char Sprites
Global CharA         = LoadImage("images/char1.png")
Global CharB         = LoadImage("images/char2.png")
		; Battle Scene
Global Status_Bar    = LoadImage("images/bset_bottom.png")
Global Fight_Menu    = LoadImage("images/bset_bottomATT.png")
Global Arrow         = LoadImage("images/arrow.png")

;---------------------
; Draw Script
;---------------------	

While Not KeyHit (1)
If draw = True Then
DrawImage Status_Bar,0,470
DrawImage Fight_Menu,20,440
EndIf

If menuarrow = 0 Then
Ax = 0
Ay = 10
EndIf
If menuarrow = 1 Then
Ax = 0
Ay = 30
EndIf
If menuarrow = 2 Then
Ax = 0
Ay = 50
EndIf
If menuarrow = 3 Then
Ax = 0
Ay = 70
EndIf
If menuarrow = 4 Then
menuarrow = 0
EndIf

If KeyHit(30) Then
Cls
EndIf 

If KeyDown(208) Then ;press down
menuarrow = menuarrow + 1
draw = False 
EndIf

DrawImage Arrow,Ax,Ay
Flip
Cls
Wend