Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Whats wrong with this code :(

Note: You must be registered in order to post a reply.

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List Spell Checker
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

   Insert an File
Check here to include your profile signature.
Check here to subscribe to this topic.
    

T O P I C    R E V I E W
BigPhatCack Posted - May 29 2006 : 06:52:03 AM
When i move my character on my screen it has a small delay in the direction that i want him to move. Any Help Would Be Apreciated


Option Explicit

Dim animX As Integer 'holds the current x location of the animation frame
Dim animY As Integer 'holds the current y location of the animation frame
Dim charX As Integer 'holds the character's x coords
Dim charY As Integer 'holds the character's y coords
Dim lastX As Integer 'holds the character's last y coords
Dim lastY As Integer 'holds the character's last x coords
Dim Speed As Integer
Dim direction As Integer 'the direction the characters facing


Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Const SRCCOPY = &HCC0020 'Copies the source over the destination
Const SRCINVERT = &H660046 'Copies and inverts the source over the destination
Const SRCAND = &H8800C6 'Adds the source to the destination

'animation frames
Const aLEFT As Integer = 2 'left animation
Const aUP As Integer = 104 'up animation
Const aRIGHT As Integer = 206 'right animation
Const aDOWN As Integer = 308 'down animation

'symbolic constants
'directions
'Const dLEFT As Integer = 0 'left direction
'Const dUP As Integer = 0 'up direction
'Const dRIGHT As Integer = 0 'right direction
'Const dDOWN As Integer = 0 'down direction



Public Sub redrawPic()
Dim a As Integer

'this function draws the picture to the screen.

'Copy the back ground to the buffer pic box
'a = BitBlt(picBuf.hDC, 0, 0, 590, 445, picBack.hDC, 0, 0, SRCCOPY)
'Copy the first layer of the sprite to the buffer
a = BitBlt(frmDisplay.picDisplay.hDC, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX + 52, animY + 1, SRCAND)
'Copy the second layer of the sprite to the buffer, for transparent effect.
a = BitBlt(frmDisplay.picDisplay.hDC, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX, animY, SRCINVERT)
'refresh the picture
frmDisplay.picDisplay.Refresh

End Sub

Private Sub Form_Load()
animX = 2
animY = 1
charX = 200
charY = 200
Speed = 6
Call redrawPic

End Sub

Private Sub picDisplay_KeyDown(KeyCode As Integer, Shift As Integer)
'copy the current location of the character into the lastx and lasty variables.
'lastX = charX
'lastY = charY

'determine how to act, based on which key the user presses.
Select Case KeyCode

Case Is = 37 'left arrow key
tmrMove.Enabled = True

' direction = dLEFT 'set the direction
charX = charX - Speed
animX = aLEFT 'set the animation frame to the proper direction


Case Is = 38 'up arrow key
tmrMove.Enabled = True

' direction = dUP
charY = charY - Speed
animX = aUP 'set the animation frame to the proper direction


Case Is = 39 'right arrow key
tmrMove.Enabled = True

'direction = dRIGHT
charX = charX + Speed
animX = aRIGHT


Case Is = 40 'down arrow key
tmrMove.Enabled = True
'direction = dDOWN
charY = charY + Speed
animX = aDOWN

'Case Is = 27 'escape key

' fraFile.Visible = True
' tmrMove.Enabled = False

'Case Is = 109 'minus key - decreases game speed
' Speed = Speed - 1
' If Speed <= 1 Then Speed = 1

'Case Is = 107 'plus key - increases game speed
' Speed = Speed + 1
' If Speed >= 20 Then Speed = 20

End Select

'see if the movement timer should be enabled
'If KeyCode >= 37 And KeyCode <= 40 Then


'move the character in the proper direction
'If direction = dLEFT Then
' charX = charX - Speed
'ElseIf direction = dUP Then
' charY = charY - Speed
'ElseIf direction = dRIGHT Then
' charX = charX + Speed
' ElseIf direction = dDOWN Then
' charY = charY + Speed
'End If

'enable the movement timer, which animates the character
' tmrMove.Enabled = True

'if movement, turn the mouse cursor into the invisible icon.
'simply making a mouse cursor that was invisible is easier
'then using API calls.


' End If

End Sub

Private Sub picDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
'disable the movement timer
tmrMove.Enabled = False

End Sub


Private Sub tmrMove_Timer()
picDisplay.Cls

'Call redrawPic 'redraws the form

animY = animY + 51 'advance the frame

'there are 8 frames in the character's animation: this sees if the last frame has
'been shown. if it has, it resets it to the first.
If animY >= (51 * 8) Then
animY = 1 'goes to first frame
End If

Call redrawPic
End Sub
4   L A T E S T    R E P L I E S    (Newest First)
BigPhatCack Posted - May 30 2006 : 02:49:03 AM
Sweet, thanks. That fixes it. It still has that strange delay when you move it in the up,down,left, or right directions. Not sure why. Still messing with it. I'm definately going to use the backbuffer though, seems to run so much smoother. ^^ Thank you very much. Time for me to get some sleep! Later.
PW7962 Posted - May 29 2006 : 8:04:17 PM
Sorry I forgot to add this. The way that I use seems to work %100 of the time and leaves no trails. Basically just before you draw the sprites you draw a blank image the size of the screen so that all old positions are erased. The code for RedrawPic would be like this:
Public Sub redrawPic()  
Dim a As Integer
'this function draws the picture to the screen.
  
'Clear Screen
a = bitblt(backbuffer, 0, 0, 590, 445, 0, 0, 0, vbblackness)  
'Copy the back ground to the buffer
'a = BitBlt(backbuffer, 0, 0, 590, 445, picBack.hDC, 0, 0, SRCCOPY)
'Copy the first layer of the sprite to the buffer
a = BitBlt(backbuffer, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX + 52, animY + 1, SRCAND)  
'Copy the second layer of the sprite to the buffer, for transparent effect.
a = BitBlt(backbuffer, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX, animY, SRCINVERT)  
'refresh the picture
bitblt frmdisplay.picdisplay.hdc, 0, 0, 590, 445, backbuffer, 0, 0, srccopy  
  
End Sub


Then in the move timer remove the picDisplay.cls. That should fix your problem.
BigPhatCack Posted - May 29 2006 : 11:39:06 AM
thanks for the info i am definatly going to use it but now when i move it leaves a trail of guys. how would i fix this problem? Again thank you for your help
PW7962 Posted - May 29 2006 : 09:45:25 AM
Well I'm not sure if this is the problem, but I have heard that using the Refresh command is rather slow. I use double buffering, because it is faster and extremely easy.

In the declarations area put these:
dim BackBuffer as long
dim BackBitmap as long
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Then in the Form_Load:
BackBuffer = CreateCompatibleDC(GetDC(0))
BackBitmap = CreateCompatibleBitmap(GetDC(0), 640, 480)
SelectObject BackBuffer, BackBitmap
BitBlt BackBuffer, 0, 0, 590, 445, 0, 0, 0, vbBlackness

Make Form_QueryUnload and put this in it:
deletedc backbuffer
deleteobject backbitmap

In RedrawPic - do all of your drawing to BackBuffer instead of the PictureBox - then at the end of the drawing instead of Refreshing and it should look like this:
Public Sub redrawPic()  
Dim a As Integer
  
'this function draws the picture to the screen.
  
'Copy the back ground to the buffer
'a = BitBlt(backbuffer, 0, 0, 590, 445, picBack.hDC, 0, 0, SRCCOPY)
'Copy the first layer of the sprite to the buffer
a = BitBlt(backbuffer, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX + 52, animY + 1, SRCAND)  
'Copy the second layer of the sprite to the buffer, for transparent effect.
a = BitBlt(backbuffer, charX, charY, 50, 50, frmDisplay.picSprite.hDC, animX, animY, SRCINVERT)  
'refresh the picture
bitblt frmdisplay.picdisplay.hdc, 0, 0, 590, 445, backbuffer, 0, 0, srccopy  
  
End Sub


I think this might help, otherwise I don't know whats wrong, because that's the only thing I see that could slow it down. (Also check your timer, and if it isn't try to put it around 300 for the interval)

VBGamer © Go To Top Of Page
This page was generated in 0.11 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.