|
Post by aKa on Jun 14, 2004 23:13:49 GMT -5
Thanks to AHBanen, I have succesfully made my walking 0 work. I tried to replace the 0 with a sprite. When I load a sprite, it becomes a white square. And the square exceeds the X and Y boundary. Here's the code:
TextMode(TEXT_BUFFERED) Struc General Dim x, y, MaxX, MaxY EndStruc Dim General p1 Dim Start, Update Dim Sprite1, Sprite2, Sprite3, Sprite4 Const Step = 5
p1.x = 320: p1.y = 240: p1.MaxX = 640: p1.MaxY = 480 sprite1 = LoadTexture("data\player.bmp"): newsprite(sprite1) SprSetPos(p1.x, p1.y) DrawText()
While True If ScanKeyDown(VK_UP) Then If p1.y <= p1.MaxY and p1.y > 0 Then p1.y = p1.y - Step Update = true Endif Endif If ScanKeyDown(VK_LEFT) Then If p1.x <= p1.MaxX and p1.x > 0 Then p1.x = p1.x - Step Update = true Endif Endif If ScanKeyDown(VK_RIGHT) Then If p1.x <= p1.MaxX and p1.x > 0 Then p1.x = p1.x + Step Update = true Endif Endif If ScanKeyDown(VK_DOWN) Then If p1.y <= p1.MaxY and p1.y > 0 Then p1.y = p1.y + Step Update = true Endif Endif If Update Then CLS: DrawText() SprSetPos(p1.x, p1.y) DrawText() Update = false Endif Wend
|
|
|
Post by NovaGene on Jun 15, 2004 1:20:58 GMT -5
This is my explanation to your problem.
Make sure that your code is run from default "c:\program files\basic4gl\programs" and player.bmp is in "c:\program files\basic4gl\programs\data" (since you are using "data\player.bmp" to load your sprite).
Change main loop section of the code to this:-
If ScanKeyDown(VK_UP) Then If p1.y-20 > 0 Then p1.y = p1.y - Step Update = true Endif Endif
If ScanKeyDown(VK_LEFT) Then If p1.x-20 > 0 Then p1.x = p1.x - Step Update = true Endif Endif
If ScanKeyDown(VK_RIGHT) Then If p1.x+20 <= p1.MaxX Then p1.x = p1.x + Step Update = true Endif Endif
If ScanKeyDown(VK_DOWN) Then If p1.y+20 <= p1.MaxY Then p1.y = p1.y + Step Update = true Endif Endif
For the up movement (similary for the other 3 IFs statements), you need to use "p1.y-20" to solve the exceed boundary problem.
In addition, when moving up ie. VK_UP, the "and p1.y > 0" check is not needed.
So the line:-
If p1.y <= p1.MaxY and p1.y > 0 Then
becomes:-
If p1.y-20 > 0 Then
Similarly for the other 3 IFs statements.
Hope this helps!
-Lucas Tan-
|
|
|
Post by AHBanen on Jun 15, 2004 3:14:55 GMT -5
Lucas, I took the liberty to modify your code in such a way that the sprite would always be fully visible on screen independant of its dimensions; to do so I replaced the fixed margin of 20 by the half of the horizontal resp. vertical size of a sprite:
TextMode(TEXT_BUFFERED) Struc General Dim x, y, MaxX, MaxY EndStruc
Dim General p1 Dim Start, Update Dim Sprite1, Sprite2, Sprite3, Sprite4 Dim Hmargin, Vmargin
Const Step = 5
p1.x = 320: p1.y = 240: p1.MaxX = 640: p1.MaxY = 480 sprite1 = LoadTexture("data\player.bmp"): newsprite(sprite1) Hmargin = SprXSize() / 2 'Width sprite1 / 2 VMargin = SprYSize() / 2 'Height sprite1 / 2
SprSetPos(p1.x, p1.y) DrawText()
While True
If ScanKeyDown(VK_UP) Then If p1.y-Vmargin > 0 Then p1.y = p1.y - Step Update = true Endif Endif
If ScanKeyDown(VK_LEFT) Then If p1.x-Hmargin > 0 Then p1.x = p1.x - Step Update = true Endif Endif
If ScanKeyDown(VK_RIGHT) Then If p1.x+Hmargin <= p1.MaxX Then p1.x = p1.x + Step Update = true Endif Endif
If ScanKeyDown(VK_DOWN) Then If p1.y+Vmargin <= p1.MaxY Then p1.y = p1.y + Step Update = true Endif Endif
If Update Then CLS: DrawText() SprSetPos(p1.x, p1.y) DrawText() Update = false Endif
Wend
Have fun programming in Basic4GL!
André
|
|
|
Post by NovaGene on Jun 15, 2004 3:35:59 GMT -5
No problemo pal! Your approach is dynamic to the size changes of the sprite. Thks! ;D
|
|